Operating System - HP-UX
1833748 Members
2885 Online
110063 Solutions
New Discussion

Re: How to insert a string to the head of every line of a file

 
SOLVED
Go to solution
yyghp
Super Advisor

How to insert a string to the head of every line of a file

I want to insert a string ( for example: "error:" ) to the head of each line of a file, and have to get the right output in a single line command ( for example: $ cat file | sed ... ) .
How can I insert such column to the file?
Should I use "sed"?
Thanks!
12 REPLIES 12
A. Clay Stephenson
Acclaimed Contributor

Re: How to insert a string to the head of every line of a file

Here's a very simple way:

#!/usr/bin/sh

typeset COMMENT=${1}
shift
typeset X=""
while read X
do
echo "${COMMENT} ${X}
done
exit 0

Use it like this:

clay.sh "error:" < infile > outfile
If it ain't broke, I can fix that.
yyghp
Super Advisor

Re: How to insert a string to the head of every line of a file

Thanks A. Clay, but I need a single-line command for this, such as a line with "sed".
Kent Ostby
Honored Contributor

Re: How to insert a string to the head of every line of a file

yyqhp --

awk '{print "error: ",$0}' < inputfile > outputfile

"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Jeff_Traigle
Honored Contributor
Solution

Re: How to insert a string to the head of every line of a file

Lots of was to do this with one line, but the sed way is:

sed 's/^/Comment: /' file
--
Jeff Traigle
A. Clay Stephenson
Acclaimed Contributor

Re: How to insert a string to the head of every line of a file

Create the shell script and save it as a file. Chmod 750 my.sh.

Now execute it as a one-limer:
my.sh "This is a comment" < infile > outfile

NOTE: I cannot believe I have to explain this!

----------------------------------------

Another one-liner approach using awk:

awk '{ print "error:", $0 }' < infile > outfile

It's also possible with Perl or sed but I've done enough.
If it ain't broke, I can fix that.
yyghp
Super Advisor

Re: How to insert a string to the head of every line of a file

Hi Jeff,

How can I use a variable to replace "Comment" in your "sed" command?

sed 's/^/Comment: /' file

Because different line should have different header.
Thanks!
Jeff_Traigle
Honored Contributor

Re: How to insert a string to the head of every line of a file

Change to double quotes and replace the specific text with the variable:

sed "s/^/${DUMMY}/" testfile
--
Jeff Traigle
yyghp
Super Advisor

Re: How to insert a string to the head of every line of a file

it should be like

sed 's/^/'$Comment': /' file

Thanks!
Sandman!
Honored Contributor

Re: How to insert a string to the head of every line of a file

How about awk?

# awk '{str="error:";if(NR==1)printf("%s\n%s\n",str,$0);else print $0}' inp
Sandman!
Honored Contributor

Re: How to insert a string to the head of every line of a file

>I want to insert a string ( for example: "error:" ) to the head of each line of a file

Misunderstood your post, thought you wanted a string inserted on the top of every file...and here's the sed construct for that:

# sed 's/\(.*\)/error: \1/p' inp
Arturo Galbiati
Esteemed Contributor

Re: How to insert a string to the head of every line of a file

Hi,
string="error:"
awk -v S=$string '{print S $0}' infile >outfile

HTH,
Art
Sylvain CROUET
Respected Contributor

Re: How to insert a string to the head of every line of a file

How do you determine which header should be inserted? Is it related to the line content?