LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   SED on multiple quotation marks in BASH (https://www.linuxquestions.org/questions/linux-newbie-8/sed-on-multiple-quotation-marks-in-bash-4175432617/)

parrishdb 10-16-2012 11:27 PM

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!

chrism01 10-17-2012 12:20 AM

Is tr ok?
Code:

cat t.t |tr -s [\"]

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


colucix 10-17-2012 12:24 AM

And the sed solution:
Code:

sed -r 's/""+/"/g' file

parrishdb 10-17-2012 10:53 AM

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

colucix 10-17-2012 11:25 AM

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.


All times are GMT -5. The time now is 03:58 AM.