Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Append text to each line in a file useing sed or awk? 1

Status
Not open for further replies.

new2unix

Programmer
Feb 5, 2001
143
US
Hi,

I have a data file that looks like the following:

1|2|3|4|5|
1|2|3|||
1|2|3|||6

Can I somehow use sed or awk, or other command to add extra pipe character to the end of each line?

I tried sed -f script.sed test1.dat > test2.dat with the following lines in script.sed:

a|||

But the resulting test2.dat had the "|||" appended to a separate line, possibly after the new line character after earch line:

1|2|3|4|5|
|||
1|2|3|||
|||
1|2|3|||6
|||

What I really wanted was:

1|2|3|4|5||||
1|2|3||||||
1|2|3|||6|||

Thanks,

Mike
 
Try...
[tt]
sed 's/$/|||/g' test1.dat > test2.dat
[/tt]
The dollar sign represents the end-of-line.

Hope this helps.

 
Is it possible to also use sed and some combination of regular expression to replace the fourth "|" with "| |"? That's one space between the two pipe. For example:

"1|2|3|||6" would become "1|2|3| |||6" with an extra pipe in the new string.

Thanks,

Mike
 
Actually in your example, that's the third pipe you're replacing. Try the following...
[tt]
sed 's/|/| |/3' test1.dat > test2.dat
[/tt]
This will search for the third pipe, and replace it with a pipe-space-pipe. This would give you...
[tt]
$ cat test1.dat
1|2|3|4|5|
1|2|3|||
1|2|3|||6
$ sed 's/|/| |/3' test1.dat
1|2|3| |4|5|
1|2|3| |||
1|2|3| |||6
[/tt]
Hope this helps.

 
Thanks Sam. I knew I was missing that bit of information where I could add a /3 or /4... Time to hit the book again.

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top