Visit Our UNIX and Linux User Community


Removing quotes within quotes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing quotes within quotes
# 8  
Old 08-31-2013
Try also this seddy:
Code:
sed -r 's/(^[^"]*)"/\1^A/; s/"([^"]*$)/^A\1/; s/"//g; s/^A/"/g' file

# 9  
Old 08-31-2013
Code:
"Hey, I was ready to go on stage or "break a leg", but I failed miserably."
"Of course, never say never. This is perhaps a "See you later"rather than a "Goodbye". Thank you for reading."
"I\'ve been rehabilitated. It\'s such a "wonderful" account of her journey."
"This is a test"
Here are some more test
Now we have a single ", just for test

Replace first and last " with , then remove all other ", then convert back to "
Code:
awk '/^"/ {gsub(/^"|"$/,"");gsub(/"/,"");gsub(//,"\"")}1' file

Code:
"Hey, I was ready to go on stage or break a leg, but I failed miserably."
"Of course, never say never. This is perhaps a See you laterrather than a Goodbye. Thank you for reading."
"I\'ve been rehabilitated. It\'s such a wonderful account of her journey."
"This is a test"
Here are some more test
Now we have a single ", just for test

# 10  
Old 08-31-2013
1) "" is a two-byte-character. No major problem, but to be kept in mind.
2) It may be used in some texts; it will be erased, then.
# 11  
Old 08-31-2013
What do I change to, to make sure its not used in the text?
Any good suggestion Smilie
# 12  
Old 08-31-2013
Quote:
Originally Posted by Jotne
What do I change to, to make sure its not used in the text?
Any good suggestion Smilie
Nothing. Literally. It seems easier just to remove all double quotes, then add back one at the start and end of the line.
# 13  
Old 08-31-2013
A basic bash version done longhand...
Not sure if it is of any use but it is _weapon_ of last resort... ;o)
(Hopefully it displays correctly...)
Code:
Last login: Sat Aug 31 19:20:17 on ttys000
AMIGA:barrywalker~> text="\"This is a \"quote within\" a quote.\""
AMIGA:barrywalker~> echo -e -n "$text" > /tmp/quotes.txt
AMIGA:barrywalker~> text=""
AMIGA:barrywalker~> newtext=""
AMIGA:barrywalker~> read text < /tmp/quotes.txt
AMIGA:barrywalker~> echo "$text"
"This is a "quote within" a quote."
AMIGA:barrywalker~> echo "${#text}"
35
AMIGA:barrywalker~> # Correct string length...
AMIGA:barrywalker~> for n in $( seq 0 1 ${#text} ); do if [ "${text:$n:1}" == '"' ]; then newtext=$newtext; else newtext=$newtext${text:$n:1}; fi; done
AMIGA:barrywalker~> echo "$newtext"
This is a quote within a quote.
AMIGA:barrywalker~> echo "${#newtext}"
31
AMIGA:barrywalker~> # Correct string length...
AMIGA:barrywalker~> # Done longhand and will be slow but works.
AMIGA:barrywalker~>
AMIGA:barrywalker~> text="\"This is a \"quote within\" a quote.\""
AMIGA:barrywalker~> newtext=""
AMIGA:barrywalker~> for n in $( seq 0 1 ${#text} ); do if [ "${text:$n:1}" == '"' ]; then newtext=$newtext"'"; else newtext=$newtext${text:$n:1}; fi; done
AMIGA:barrywalker~> echo "$newtext"
'This is a 'quote within' a quote.'
AMIGA:barrywalker~> # Quotes changed from double to single...
AMIGA:barrywalker~> _

EDIT:

Juggle the part that is...
Code:
${text:$n:1}

...and leave the start and end double quotes in as required...

Last edited by wisecracker; 08-31-2013 at 03:50 PM.. Reason: See above.
# 14  
Old 08-31-2013
My last two suggestions above don't care if it's at the begin or the end of a line. They leave the outermost quotes alone and remove all the inner ones.

Previous Thread | Next Thread
Test Your Knowledge in Computers #701
Difficulty: Easy
MySQL NOT LIKE is a sardonic DB operator used to exclude those rows which are not liked by Oracle Corporation executives.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing consecutive double quotes

Hi I have a .csv file and when opened in notepad looks like this gggg,nnnn,"last,first","llll""",nnn So, Here I would like the ouput as below gggg,nnnn,"last,first","llll",nnn i.e replace all two double quotes into one. How could I do that? This file is being processed by another... (5 Replies)
Discussion started by: dnat
5 Replies

2. Shell Programming and Scripting

Removing newline characters within DEL quotes.

Hi, Text file has DEL character(ASCII code 127) as quotes with comma as field delimiter. If any of the field contains new line character then I need to remove it. Please help me to achieve this. Thanks Vikram (4 Replies)
Discussion started by: Vikramhm
4 Replies

3. Shell Programming and Scripting

Removing quotes in sed

I have a file and I want to remove quotes from the word " ` " through sed command but unable to remove I am using below command sed s/"`XYZ`"/"ZXY"/g file1.txt > file2.txt But this is not working. How can we remove "`" through sed command (2 Replies)
Discussion started by: kaushik02018
2 Replies

4. Shell Programming and Scripting

Issue with Single Quotes and Double Quotes for prompt PS1

Hi, Trying to change the prompt. I have the following code. export PS1=' <${USER}@`hostname -s`>$ ' The hostname is not displayed <abc@`hostname -s`>$ uname -a AIX xyz 1 6 00F736154C00 <adcwl4h@`hostname -s`>$ If I use double quotes, then the hostname is printed properly but... (3 Replies)
Discussion started by: bobbygsk
3 Replies

5. Shell Programming and Scripting

Having a terrible problem with quotes/single quotes!

Hello. I'm trying to write a bash script that uses GNU screen and have hit a brick wall that has cost me many hours... (I'm sure it has something to do with quoting/globbing, which is why I post it here) I can make a script that does the following just fine: test.sh: #!/bin/bash # make... (2 Replies)
Discussion started by: jondecker76
2 Replies

6. UNIX for Dummies Questions & Answers

grep single quotes or double quotes

Unix superusers, I am new to unix but would like to learn more about grep. I am very familiar with regular expressions as i have used them for searching text files in windows based text editors. Since I am not very familiar with Unix, I dont understand when one should use GREP with the... (2 Replies)
Discussion started by: george_vandelet
2 Replies

7. Shell Programming and Scripting

AWK removing away needed double quotes.

The below code is to convert csv file to pipe delimited. It replaces comma with pipe if it is not in double quotes; If comma is in double quotes it doesnot replace the comma with a pipe. The code works fine except it eat away the double quotes in the output file. BEGIN... (6 Replies)
Discussion started by: pinnacle
6 Replies

8. Shell Programming and Scripting

Removing back quotes from string in CSH

Hello, I am using csh to read a text file and save its words into variable $word in a foreach loop. These words have small back quotes ` as integral parts of them, for example, one word would be `abc`, another would be `xyz1` etc... These quotes are always the first and last characters of the... (5 Replies)
Discussion started by: aplaydoc
5 Replies

9. UNIX for Dummies Questions & Answers

Removing double quotes in a file

Hi All, I have a tab delimited file where each of the strings have double quotes. The problem is that I have records which are in the following format: "TEXAS" ""HOUSTON"" "123" "" "2625-39-39" ""MAINE"" "" "456" "I" "3737-39-82" I would have to output... (3 Replies)
Discussion started by: kingofprussia
3 Replies

10. Shell Programming and Scripting

Double quotes or single quotes when using ssh?

I'm not very familiar with the ssh command. When I tried to set a variable and then echo its value on a remote machine via ssh, I found a problem. For example, $ ITSME=itsme $ ssh xxx.xxxx.xxx.xxx "ITSME=itsyou; echo $ITSME" itsme $ ssh xxx.xxxx.xxx.xxx 'ITSME=itsyou; echo $ITSME' itsyou $... (3 Replies)
Discussion started by: password636
3 Replies

Featured Tech Videos