LinuxQuestions.org
Help answer threads with 0 replies.
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 10-17-2012, 12:27 AM   #1
parrishdb
LQ Newbie
 
Registered: Oct 2012
Posts: 2

Rep: Reputation: Disabled
SED on multiple quotation marks in BASH


I've inherited a lengthy text file that looks similar to:

"foo ""bar""; foo ""bar"""
"foo ""bar""; foo ""bar"""
"foo ""bar""; foo ""bar"""

I would like it to become:

foo "bar"; foo "bar"
foo "bar"; foo "bar"
foo "bar"; foo "bar"

I'm most familiar with SED, but still a Linux / BASH newbie and can't quite figure this one out. I've covered Bruce Barnett's SED tutorial page, and working my way through the AWK tutorial as well, but after a few days I'm stumped

I appreciate any help / advice anyone on here can offer.

Thanks!
 
Old 10-17-2012, 01:20 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Is tr ok?
Code:
cat t.t |tr -s [\"]

"foo "bar"; foo "bar"
"foo "bar"; foo "bar"
"foo "bar"; foo "bar"
 
1 members found this post helpful.
Old 10-17-2012, 01:24 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
And the sed solution:
Code:
sed -r 's/""+/"/g' file
 
Old 10-17-2012, 11:53 AM   #4
parrishdb
LQ Newbie
 
Registered: Oct 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks

Thanks to both of you for your quick responses. I actually hadn't previously come across the translate (tr) command until searching the message boards for this particular question (and chrism01's answer). I do have a couple short follow-up questions for clarification

Both of the codes (cat t.t |tr -s [\"] & sed -r 's/""+/"/g' file) worked to produce

"foo "bar"; foo "bar"
"foo "bar"; foo "bar"
"foo "bar"; foo "bar"


I need to remove the single quotation mark in front of foo at the beginning of each line. I was able to accomplish this with:

sed 's/^" //' file

which produced

foo "bar"; foo "bar"
foo "bar"; foo "bar"
foo "bar"; foo "bar"

But is there a way to put this all together in on short line, either 'tr' or 'sed?' Or is it just best to pipe the two commands together?

Secondly, colucix can you explain what the + character is doing in the sed command? I have never seen that until now and haven't found it explained anywhere else?

I certainly don't mind figuring it out for myself if anyone is willing to point me in the direction of a good external resource.

Thanks again
 
Old 10-17-2012, 12:25 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You can put two sed expressions together using two different -e options:
Code:
sed -r -e 's/""+/"/g' -e 's/^"//' file
Regarding the + sign, in a regular expression it means 1 or more occurrences of the previous pattern, whereas the * sign means zero or more occurrences. In the previous example the regular expression:
Code:
/""+/
ensures it matches only two or more double quotes (the first plus the second one repeated 1 or more times). In other words it leaves single double quotes untouched (even if - in this case - changing them with another single double quote doesn't make any difference).

A good tutorial on sed with a lot of regular expressions examples is http://www.grymoire.com/Unix/Sed.html.
 
  


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
BASH variables, quotation marks and cURL struggles matthias_k Programming 13 01-27-2013 05:56 PM
Bash scripting general questions about quotation marks veeruk101 Linux - Newbie 6 10-20-2011 01:37 PM
can not input double quotation marks(invalid quotation key) wangzhilife Linux - Hardware 2 09-18-2009 07:44 AM
Use of quotation marks in bash dunryc Programming 2 06-08-2008 03:24 PM
no quotation marks jdoe9898 Linux - Newbie 1 10-12-2005 04:07 AM

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

All times are GMT -5. The time now is 02:42 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