LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-28-2019, 10:52 AM   #16
tofino_surfer
Member
 
Registered: Aug 2007
Posts: 385

Rep: Reputation: 108Reputation: 108

Quote:
..which only changes the first comma. You seem to want to take issue with my post and scasey's, but aren't putting forth one of your own
My point was that the g for global definitely should not be added to replace the comma. You were suggesting that it needs to be there.

scasey had already posted a sed statement without the g just before my post. His solution was correct. There was no need to add my own however I should have explicitly mentioned that his solution was correct.


From scasey:

Code:
sed  "s/^/'/;s/$/'/;s/,/','/" filename > newfilename
Quote:
If you only want to put quotes around the first comma, remove the g modifier on that pattern.

Quote:
Because the OP mentions unix, not Linux...and things on AIX, HPUX, or Solaris have different versions of things, which behave differently. The GNU version may not be installed.
The OP is also an LQ Newbie with all of 3 posts. They may use the terms unix and linux interchangeably. They never mentioned what distribution they are using or what "unix" version they plan to use. Also on Unix bash may not even be installed. It could have the ancient old Bourne shell which disappeared from Linux many years ago.
 
Old 11-28-2019, 12:16 PM   #17
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 22,235

Rep: Reputation: 5973Reputation: 5973Reputation: 5973Reputation: 5973Reputation: 5973Reputation: 5973Reputation: 5973Reputation: 5973Reputation: 5973Reputation: 5973Reputation: 5973
Quote:
Originally Posted by tofino_surfer View Post
My point was that the g for global definitely should not be added to replace the comma. You were suggesting that it needs to be there.
...because the OP said it was only getting a single-quote at the start of the line. And that I misread the question, which is why I suggested the 'g' option, since (as the OP themselves stated), they were only getting the first replacement.
Quote:
scasey had already posted a sed statement without the g just before my post. His solution was correct. There was no need to add my own however I should have explicitly mentioned that his solution was correct. From scasey:
Code:
sed  "s/^/'/;s/$/'/;s/,/','/" filename > newfilename
Yep...in this particular instance, with that set of data, the 'g' isn't needed, nor is specifically the '1'...I specify them because that was how I would have done it, and both work.
Quote:
The OP is also an LQ Newbie with all of 3 posts. They may use the terms unix and linux interchangeably. They never mentioned what distribution they are using or what "unix" version they plan to use. Also on Unix bash may not even be installed. It could have the ancient old Bourne shell which disappeared from Linux many years ago.
..which is exactly *WHY* it's important to ask a newbie, so they can LEARN the differences between Unix and Linux, and know that they're not interchangeable. Which is why I asked them for details. And which is why sed may be different, and that's why I asked previously, but that seemed to be a sticking point as well.
 
Old 11-28-2019, 04:56 PM   #18
dmchess
Member
 
Registered: Jan 2005
Posts: 154

Rep: Reputation: 24
Here is a simple way:

cat Test.txt | awk -F, '{printf("%c%s%c,%c%s%c\n",39,$1,39,39,$2,39)}'
 
Old 11-28-2019, 07:40 PM   #19
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,267

Rep: Reputation: 580Reputation: 580Reputation: 580Reputation: 580Reputation: 580Reputation: 580
Thumbs down

Quote:
Originally Posted by Kenhelm View Post
sed "s/$/'/" will apparently fail if the file has Microsoft \r\n end-of-line characters instead of Unix \n.
The \r takes printing back to the start of the line creating the impression of failure when the line is viewed in a terminal.
Code:
echo -e 'abcde\r' |  sed "s/$/'/"        # Echo a line with a carriage return, \r
'bcde                                    # Displayed in a terminal the ' overprints the first character on the line
                                         # But the actual line sed created is abcde\r'\n
I think that's the case here.
Fix: convert with dos2unix first.
Or let sed convert to unix i.e. remove a \r
Code:
sed  "s/\r\$/; s/^/'/; s/\$/'/; s/,/','/" filename
I have \escaped the $ because it is within "" quotes, in case the shell would know (and substitute) a special $/ variable.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to make the output in multiple row become single row? ahpin Linux - Newbie 4 07-19-2013 08:49 AM
translate value from single quotes to double quotes venkateshrupineni Linux - Newbie 2 06-14-2012 04:03 PM
gnome-terminal is missing one pixel row and its an important row rednuht Linux - General 1 12-24-2009 11:30 AM
Shell script to parse csv-like output, row by row utahnix Linux - General 8 12-08-2007 06:03 AM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 12:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:52 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration