24

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... [}
RomanPerekhrest
  • 29,703
  • 3
  • 43
  • 67
Terkey-Juice
  • 463
  • 1
  • 5
  • 10

4 Answers4

37

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
RomanPerekhrest
  • 29,703
  • 3
  • 43
  • 67
  • 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 05: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 05:50
  • @GordonDavisson Hi. Yes, a literal `^`. And I always keep backups of things. Always. Thank you friend – Terkey-Juice Mar 31 '18 at 06: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 01 '18 at 03:39
  • Yes, the sed processes each line twice (making copies) while the awk only once. – OrangeDog Apr 01 '18 at 10:14
  • It should be noted that this method doesn't support variable substitution. If you wanted to expand variables you would have to use double quotes `" "` rather than single quotes `' '`. This would change the functionality of the `$` operator. If you also want variable substitution, use the following method: `sed "s/.*/${var1}&${var2}/"` – azizbro Jul 02 '20 at 02:21
10
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
don_crissti
  • 79,330
  • 30
  • 216
  • 245
  • I like this method because it supports variable substitution for the prefix and suffix. E.g. `sed "s/.*/${var1}&${var2}/"` – azizbro Jul 02 '20 at 02:19
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 
terdon
  • 234,489
  • 66
  • 447
  • 667
  • How do you do it without the space? e.g. this line works `$ echo xxx|perl -lne 'print "abc $_ kljlkj "'` It puts that text before and after xxx. But suppose I want to do that without space. This `$ echo xxx|perl -lne 'print "abc$_kljlkj "'` doesn't work it just prints 'abc'. – barlop May 05 '20 at 23:02
  • 1
    @barlop you just need to separate the `$_` from any subsequent characters so that perl can recognize the variable name. Any of these would work `print "abc$_"; print "kljlkj "` or `print "abc " . $_ . "kljlkj "` or `print "abc${_}kljlkj"`. – terdon May 06 '20 at 07:54
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.

CSM
  • 2,090
  • 1
  • 10
  • 7