4

I am trying to append and prepend text to every line in a .txt file.

I want to prepend: I am a

I want to append: 128... [}

to every line.

Inside of a.txt:

fruit, like
bike, like
dino, like

Upon performing the following command:

$ cat a.txt|sed 'I am a ',' 128... [}'

it does not work how I want it to. I would really want it to say the following:

I am a fruit, like 128... [}
I am a bike, like 128... [}
I am a dino, like 128... [}
11

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
  • thanks, mate. is it safe to do this? sed 's/^/^.{128} /; s/$/$/' I want to add ^.{128} to the beginning and $ to the end. – Terkey-Juice Mar 31 '18 at 5:26
  • @Terkey-Juice Do you want to actually add a literal "^" to the beginning of each line? If so, that should do it. But you should always have a backup of files that you're going to try something on, because things will go wrong sometimes. – Gordon Davisson Mar 31 '18 at 5:50
  • @GordonDavisson Hi. Yes, a literal ^. And I always keep backups of things. Always. Thank you friend – Terkey-Juice Mar 31 '18 at 6:12
  • If the lines/files get big enough to notice, then the awk solution should be more efficient. – OrangeDog Mar 31 '18 at 16:35
  • @OrangeDog Big enough to notice? Do you mean if it runs too slow, the awk solution would be faster? – Terkey-Juice Apr 1 '18 at 3:39
3
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 substitution. 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
2

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 
1

Alternative sed

sed -e 's/^\(.*\)$/I am a \1 128 [{/

It searches for everything .* between the start ^, and end of line $, and places it in a group \( \). It then replaces it with the prefix, the (first) group \1, and the suffix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.