Paul - The Programmer

simple & stupid
Enable Adobe Flash Player GPU acceleration on Linux
Why Google Chrome GPU acceleration does not work for me

Add prefix and append suffix to each line of text file

paul posted @ Wed, 02 Mar 2011 08:43:36 +0800 in linux with tags awk sed vim , 15867 readers

I need to update one text file to add prefix and suffix to each line. For instance, let's say the text file I need to update is like:

line-number-one
line-number-two
line-number-three

I want the file be updated as below.

prefix line-number-one suffix
prefix line-number-two suffix
prefix line-number-three suffix

Here are my solutions.

To update with vim:

:%s/.*/prefix & suffix/g

To update with sed:

$ sed 's/\(.*\)/prefix $1 suffix/' sample-file.txt

To update with awk:

$ awk '{ printf( "prefix %s suffix\n", $1 ); }' sample-file.txt

 

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.

Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter