sed replace end of line

2.2k Views Asked by At

I'm trying to add a bunch of 0s at the end of a line. The way the line is identified is that it is followed by a line which starts with "expr1"

in Vim what I do is:

s/\nexpr1/ 0 0 0 0 0 0\rexpr1/

and it works fine. I know that in ubuntu \n is what is normally used to terminate the line but whenever I do that I get a ^@ symbol so \r works fine for me. I thought I'd use this with sed but it hasn't really worked. here is what I normally write:

sed "s/\nexpr1/ 0 0 0 0 0 0\rexpr1/" infile > outfile
3

There are 3 best solutions below

0
On

The end-of-line marker is $. Try this:

s/$/ 0 0 0 0 0 0/

Depending on your environment, you might need to escape the $.

0
On
awk '{$0=$0" 0 0 0 0 0 "}1' file > tmp && mv tmp file

ruby -i.bak -ne '$_=$_.chomp!+" 0 0 0 0 0\n";print' file
1
On
awk '$(NF + 1) = " 0 0 0 0 0 0"' infile > outfile