Take the 2-minute tour ×

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.

share|improve this question
What do you want to do with that output? To put it in a list variable in your programming language? To print it to the screen? To write it back to file? –  oggy Nov 6 '09 at 17:08
either write back to file or print to screen... i'll later want to copy and paste this somewhere else so both writing back to the same file or printing to screem are fine as far as i can efficiently copy and paste it later 'manually' –  Phil Nov 6 '09 at 17:12
add comment

9 Answers

up vote 4 down vote accepted

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.

share|improve this answer
thanks! upvoted –  Phil Nov 6 '09 at 17:11
...where it outputs it? to this file or screen? –  Phil Nov 6 '09 at 17:15
It gets edited inline, so the file should change. –  jsoverson Nov 6 '09 at 17:15
perl -pli -e '$_=qq{"$_"}' would also do the job. –  mob Nov 6 '09 at 17:31
add comment
sed 's/.*/"&",/'
share|improve this answer
I think you want to move the comma after the quote mark; other than that, neat answer! –  Gordon Davisson Nov 6 '09 at 20:23
sed 's/.*/"&",/' input_file | xclip –  J.F. Sebastian Nov 7 '09 at 0:11
add comment

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.

share|improve this answer
That's the spirit, why use a gnu when you could use a python! +1 for the very handy fileinput module. –  RedGlyph Nov 6 '09 at 17:22
add comment

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.

share|improve this answer
There's no need for redirection, sed accepts the filename as an argument. –  Dennis Williamson Nov 6 '09 at 17:53
add comment

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

$ 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
share|improve this answer
add comment

In Bash:

while read line
    echo "\"${line}\","
done < inputfile
share|improve this answer
add comment


for line in open("file"):
  print '"%s",'  % line
share|improve this answer
add comment

sh + awk are nice here too...

for FILE in "$@"
   awk '{print "\" $0 "\","}' < $FILE > $FILE.tmp
   mv $FILE.tmp $FILE
share|improve this answer
awk will accept the input file as a command line argument without redirection. –  Dennis Williamson Nov 6 '09 at 18:05
add comment

In vi:

share|improve this answer
add comment

Your Answer


