LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   insert a single quote is text file with sed (http://www.linuxquestions.org/questions/programming-9/insert-a-single-quote-is-text-file-with-sed-552389/)

eln01 05-08-2007 04:29 PM

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?

dxqcanada 05-08-2007 05:31 PM

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/$/\',/

ghostdog74 05-08-2007 07:00 PM

If you have Python, here's an alternative
Code:

#!/usr/bin/python
data = open("file").readlines()
data = ['"' + i.strip() + '"'  for i in data]
print ','.join(data)

output:
Code:

# ./test.py
"12459497","12459500","12459502"


nmh+linuxquestions.o 05-09-2007 11:14 AM

Quote:

Originally Posted by eln01
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?

quoting a single quote might look like:
Code:

$ echo "foo" | sed "s/o/'/g"
f''

As for merging an unknown number of lines, I think you use something other than sed - but I would be delighted to hear of a good way to make it (sed) work.

druuna 05-09-2007 11:37 AM

Hi,

Using sed and tr:

sed "s/\(.*\)/'\1'/" infile | tr '\n' ','

Only thing that is a bit ugly, the last , on the line:
Quote:

$ cat infile
12459497
12459500
12459502

$ sed "s/\(.*\)/'\1'/" infile | tr '\n' ','
'12459497','12459500','12459502',


Using perl would make it 'perfect':

perl -ne 'chomp ; print "'\''$_'\''" ; if (eof()) { print "\n"; } else { print "," }' infile
Quote:

$ perl -ne 'chomp ; print "'\''$_'\''" ; if (eof()) { print "\n"; } else { print "," }' infile
'12459497','12459500','12459502'
The last , is gone and there is a proper eol after the last entry is printed.

makyo 05-09-2007 01:31 PM

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

# @(#) s2      Demonstrate creation of csv file.

FILE=${1-data1}

sed -e "s/^/'/" -e "s/$/',/" -e '$s/.$//'  $FILE |
tr -d '\n'
echo

exit 0

Producing:
Code:

% ./s2
'12459497','12459500','12459502'

cheers, makyo

radoulov 05-09-2007 04:09 PM

Or awk:
Code:

awk 'NR==1{rec=sp$0sp;next}
{rec=rec","sp$0sp}END{print rec}' sp="'" infile


eln01 05-10-2007 11:02 AM

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
data = open("file").readlines()
data = ['\'' + i.strip() + '\''  for i in data]
print ','.join(data)



All times are GMT -5. The time now is 08:09 AM.