Add prefix and suffix to every line in a .txt file
Simple sed
approach:
sed 's/^/I am a /; s/$/ 128... [}/' file.txt
^
- stands for start of the string/line$
- stands for end of the string/line
The output:
I am a fruit, like 128... [}
I am a bike, like 128... [}
I am a dino, like 128... [}
Alternatively, with Awk
you could do:
awk '{ print "I am a", $0, "128... [}" }' file.txt
sed 's/.*/PREFIX&SUFFIX/' infile
will do, assuming PREFIX
and SUFFIX
don't contain any special characters.
\&/
(backslash, ampersand and delimiter) are special when in the right hand side of a s
ubstitution. The special meaning of those characters can be suppressed by escaping them (preceding them by a backslash) e.g. to prepend A//BC
and to append XY\Z&&
one would run:
sed 's/.*/A\/\/BC&XY\\Z\&\&/' infile
Perl:
$ perl -lne 'print "I am a $_ 128... [}"' file
I am a fruit, like 128... [}
I am a bike, like 128... [}
I am a dino, like 128... [}
More Perl:
$ perl -pe 's/^/I am a /; s/$/ 128... [}/' file
I am a fruit, like 128... [}
I am a bike, like 128... [}
I am a dino, like 128... [}
And a bit more Perl:
$ perl -lpe '$_="I am a $_ 128... [}"' file
I am a fruit, like 128... [}
I am a bike, like 128... [}
I am a dino, like 128... [}
For all of these, you can use -i
to make the change in the original file:
$ perl -i -lne 'print "I am a $_ 128... [}"' file
$ perl -i -pe 's/^/I am a /; s/$/ 128... [}/' file
$ perl -i -lpe '$_="I am a $_ 128... [}"' file