tags:

views:

240

answers:

9

I have text file with something like

first line
line nr 2
line three

etc

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.

+1  A: 

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.

Josh Wright
thanks! upvoted
Phil
...where it outputs it? to this file or screen?
Phil
It gets edited inline, so the file should change.
jsoverson
`perl -pli -e '$_=qq{"$_"}'` would also do the job.
mobrule
+6  A: 

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.

Nadia Alramli
That's the spirit, why use a gnu when you could use a python! +1 for the very handy fileinput module.
RedGlyph
+5  A: 

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.

ʞɔıu
There's no need for redirection, `sed` accepts the filename as an argument.
Dennis Williamson
+7  A: 
sed 's/.*/"&",/'
pixelbeat
I think you want to move the comma after the quote mark; other than that, neat answer!
Gordon Davisson
J.F. Sebastian
A: 

sh + awk are nice here too...

!/bin/sh
for FILE in "$@"
do
   awk '{print "\" $0 "\","}' < $FILE > $FILE.tmp
   mv $FILE.tmp $FILE
done
fortran
`awk` will accept the input file as a command line argument without redirection.
Dennis Williamson
+1  A: 

In Bash:

while read line
    do
    echo "\"${line}\","
done < inputfile
Dennis Williamson
+4  A: 

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
ghostdog74
A: 

In vi:

:%s/^\(.*\)$/"\1",/g
AJ
+1  A: 

Python

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