How to add suffix to lines using sed

January 16, 2011 at 12:34:15
Specs: Windows XP, Athlon x2 5200+ / 2G
Hello again,
I am using on Windows XP many tools that originally I used in Linux: cat, sed, awk, head, tail, ls, wget.., and others. I found all those programs in the package UnxUtils
I am using "sed" many times just because it's so useful for scripting.
However I have a problem: I don't know how to add a suffix to lines using sed.
the following script:

cat file.txt | sed -e "s/$/suffix/"

will add the suffix AFTER each line so the result will look like:
I found a partial solution: cat file.txt | sed -e "s/.$/suffix/" - it will add the suffix to the lines but will "eat" the last character from each line. That''s ok just as long as all the lines end in the same character (for example with a space)


See More: How to add suffix to lines using sed

Report •

January 16, 2011 at 20:15:45
use awk
gawk '{print $0" suffix" }' file

by the way, you should really use those from GNU (see my sig), not those from UnxUtils..(they are outdated)

GNU win32 packages | Gawk

Report •

January 16, 2011 at 21:49:08
thanks for your kind reply
You are right gnuwin32 has much better versions of those commands, and I already updated the sed and grep programs.

meanwhile I found the solution with sed:

cat file.txt | sed -e "s/\(.*\)/\1suffix/"

- the \1, \2, etc. are the identifiers for regexp matches created with constructions like \(.*\)

It's good to have solutions with both sed and awk

Report •

January 16, 2011 at 22:21:28
well if you want to use sed

sed "s/.*/&suffix/" file

There's no need to use back references (and that's what they are called, not "identifiers" ).
And lose the cat command. Its useless.

GNU win32 packages | Gawk

Report •

Related Solutions

January 17, 2011 at 00:56:06
Cool, thanks

you are right, but still "cat" is the best command-line program in Windows for joining mpegs:

cat 1.mpg 2.mpg 3.mpg > out.mpg


Report •

January 17, 2011 at 01:48:47
that's right, cat's main purpose is for "concatenating" files. There is no reason to use it together with tools like sed/ awk/ grep etc because all these tools are capable of reading files themselves.

GNU win32 packages | Gawk

Report •

Ask Question