LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-08-2007, 04:29 PM   #1
eln01
LQ Newbie
 
Registered: May 2007
Distribution: Fedora 6
Posts: 8

Rep: Reputation: 0
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?
 
Old 05-08-2007, 05:31 PM   #2
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
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/$/\',/
 
Old 05-08-2007, 07:00 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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"
 
Old 05-09-2007, 11:14 AM   #4
nmh+linuxquestions.o
Member
 
Registered: Feb 2007
Posts: 135

Rep: Reputation: 15
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.
 
Old 05-09-2007, 11:37 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.

Last edited by druuna; 05-09-2007 at 11:52 AM. Reason: A better solution using perl.
 
Old 05-09-2007, 01:31 PM   #6
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
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
 
Old 05-09-2007, 04:09 PM   #7
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Or awk:
Code:
awk 'NR==1{rec=sp$0sp;next}
{rec=rec","sp$0sp}END{print rec}' sp="'" infile
 
Old 05-10-2007, 11:02 AM   #8
eln01
LQ Newbie
 
Registered: May 2007
Distribution: Fedora 6
Posts: 8

Original Poster
Rep: Reputation: 0
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)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert and delete lines at the end of a file using sed DriveMeCrazy Programming 1 01-05-2007 01:45 AM
help removing some text from a file (sed) BrianK Programming 2 08-13-2006 07:10 PM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
Insert a quote into your message harisethuraman Linux - Software 0 02-12-2004 09:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration