Tutorial :python or bash - adding “ at beginning of line and ”, at end of line


I have text file with something like

first line  line nr 2  line three  


And i want to generate

"first line",  "line nr 2",  "line three",  

I wonder how to do this in python or maybe in bash if it's easier/quicker. I know there is different code for opening file and different for reading only one line in python(?) but i'm not sure which option to use in this case and, more importantly, how to add these characters. Any advice would help.


A number of easy ways to do it...

A simple perl oneliner:

perl -pi -e 's/^(.*)$/\"$1\",/g' /path/to/your/file  

To explain a bit, the regex ^(.*)$ grabs everything (the (.*)) between the start of the line (^) and the end of the line ($), then uses the $1 match group variable to reconstruct it with the quotes and comma.


sed 's/.*/"&",/'  


For the reference, in case someone wants to do the same thing using python. There is a handy module fileinput that could be used like this:

import fileinput  import sys, os    for line in fileinput.input(inplace=True):      sys.stdout.write('"%s",%s' % (line.rstrip(os.linesep), os.linesep))  

Then run this as a script:

python myscript.py file1 file2 file3  

That will change the files inplace for you.


Be a true unix geek: use sed!

sed 's/^/"/; s/$/",/;' < your_text_file  

If you want to escape existing double quotes with backslashes, use 's/"/\\"/g; s/^/"/; s/$/",/;' as the pattern.

sed is ideally suited for this type of task. Check out a ludicrously long list of examples.


there is no need to construct regular expression(with backreferencing) for this task. Its an expensive operation since you are not going to change something in the line. Easiest way is just to print them out.

    awk '{print "\042"$0"\042,"}' file   

Results on operation on a big file:

$ head -5 file  this is line  this is line  this is line  this is line  this is line  $ wc -l < file  9545088    $ time  awk '{print "\042"$0"\042,"}' file  >/dev/null    real    0m15.574s  user    0m15.327s  sys     0m0.172s    $ time sed 's/.*/"&",/' file > /dev/null    real    0m31.717s  user    0m31.465s  sys     0m0.157s    $ time perl -p -e 's/^(.*)$/\"$1\",/g'  file >/dev/null    real    0m36.576s  user    0m36.006s  sys     0m0.360s  


In Bash:

while read line      do      echo "\"${line}\","  done < inputfile  



for line in open("file"):    line=line.strip()    print '"%s",'  % line  


sh + awk are nice here too...

!/bin/sh  for FILE in "$@"  do     awk '{print "\" $0 "\","}' < $FILE > $FILE.tmp     mv $FILE.tmp $FILE  done  


In vi:


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »