insert a single quote is text file with sed
I have a text file with a column of numbers that looks like
12459497 12459500 12459502 . . . Desired format '12459497','12459500','12459502' . . . Here is what I have been able to do . . . $ sed -e 's/.*/'&',/' -e 's/\r//' format.txt > formated.txt [1] 1380 sed: -e expression #1, char 5: unterminated `s' command bash: ,/: No such file or directory [1]+ Exit 1 sed -e 's/.*/' $ Make the change to user ( instead of ' . . . $ sed -e 's/.*/(&),/' -e 's/\r//' format.txt > formated.txt and now I have (12459497), (12459500), (12459502), I was hoping -e expression #2 would delete the carriage return or EOL, for I need one line. How can I insert a single quote mark at the beginning and end of each line? How can I then delete the EOL marker and make it one line? |
I think you want to substitute the beginning of the line (^) with ' and the end ($) of the line with ', ...
Code:
sed -e s/^/\'/ -e s/$/\',/ |
If you have Python, here's an alternative
Code:
#!/usr/bin/python Code:
# ./test.py |
Quote:
Code:
$ echo "foo" | sed "s/o/'/g" |
Hi,
Using sed and tr: sed "s/\(.*\)/'\1'/" infile | tr '\n' ',' Only thing that is a bit ugly, the last , on the line: Quote:
Using perl would make it 'perfect': perl -ne 'chomp ; print "'\''$_'\''" ; if (eof()) { print "\n"; } else { print "," }' infile Quote:
|
Hi.
A variation. Use an additional substitution to get rid of the trailing comma, and add an echo to provide a trailing newline: Code:
#!/bin/sh Code:
% ./s2 |
Or awk:
Code:
awk 'NR==1{rec=sp$0sp;next} |
Done
Thank you all for your help.
Ghosdog74, your python script was almost right on mark except for inserting " instead of '. My modification is: Code:
#!/usr/bin/python |
All times are GMT -5. The time now is 12:59 PM. |