If you have it, the lam (laminate) utility can do it, for example:
$ lam filename -s "string after each line"
I prefer using awk
.
If there is only one column, use $0
, else replace it with the last column.
One way,
awk '{print $0, "string to append after each line"}' file > new_file
or this,
awk '$0=$0"string to append after each line"' file > new_file
Sed is a little ugly, you could do it elegantly like so:
hendry@i7 tmp$ cat foo
bar
candy
car
hendry@i7 tmp$ for i in `cat foo`; do echo ${i}bar; done
barbar
candybar
carbar
I prefer echo
. using pure bash:
cat file | while read line; do echo ${line}$string; done
Pure POSIX shell and sponge
:
suffix=foobar
while read l ; do printf '%s\n' "$l" "${suffix}" ; done < file |
sponge file
xargs
and printf
:
suffix=foobar
xargs -L 1 printf "%s${suffix}\n" < file | sponge file
Using join
:
suffix=foobar
join file file -e "${suffix}" -o 1.1,2.99999 | sponge file
Shell tools using paste
, yes
, head
& wc
:
suffix=foobar
paste file <(yes "${suffix}" | head -$(wc -l < file) ) | sponge file
Note that paste
inserts a Tab char before $suffix
.
Of course sponge
can be replaced with a temp file, afterwards mv
'd over the original filename, as with some other answers...
Source: Stackoverflow.com