Removing " from a text using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing " from a text using awk
# 8  
Old 10-24-2013
Smart idea Smilie
You just add some extra to the stand alone ", then remove it.

Just a small variation (saves two characters):
Code:
awk '{gsub(/[^\\]"/,"&_");gsub(/"_/,x)}1' file

# 9  
Old 10-24-2013
Quote:
Thanks Jotne

Try... for given input this will work Smilie



Code:
$ awk '{gsub(/[^\\A-Za-z]\"|"$/," ");gsub(/"\, /,",")}1' fileThe quick brown fox jumps,over the 'lazy \"dog\"' here are some more data
OR



Code:
$ awk '{for(i=1;i<=NF;i++)if($i~/^\"|"$/){gsub("\"","",$i);printf $i FS}else{printf $i FS}printf RS }' fileThe quick brown fox jumps, over the 'lazy \"dog\"' here are some more data
Hello Akshay,

Could you please explain both the codes a bit please.



Thanks,
R. Singh
# 10  
Old 10-24-2013
Quote:
Originally Posted by disedorgue
Hi,
If you use the commutator &:
Code:
awk '{gsub(/[^\\]"/,"&\"");gsub(/""/,"")}1' file

Regards.
Quote:
Originally Posted by Jotne
Smart idea Smilie
You just add some extra to the stand alone ", then remove it.

Just a small variation (saves two characters):
Code:
awk '{gsub(/[^\\]"/,"&_");gsub(/"_/,x)}1' file

Both suggestions will run into trouble if the replacement string is already present on the line, or if a double quote is in the first position..


--
Try:
Code:
awk '{gsub(/\\"/,RS); gsub(/"/,x); gsub(RS,"\\\"")}1' file

With RS="\n" as the record separator, we can be sure that it will never appear on the line, so that is a suitable intermediate character.

Last edited by Scrutinizer; 10-24-2013 at 03:23 PM..
# 11  
Old 10-24-2013
We can always create a string that not present, but therefore a fix for case of double quote in the first position:
Code:
awk '{gsub(/^"|[^\\]"/,"&ASTRINGTHATNOTEXIST");gsub(/"ASTRINGTHATNOTEXIST/,"")}1' file

Regards.
# 12  
Old 10-24-2013
Quote:
Originally Posted by disedorgue
We can always create a string that not present, but therefore a fix for case of double quote in the first position:
Code:
awk '{gsub(/^"|[^\\]"/,"&ASTRINGTHATNOTEXIST");gsub(/"ASTRINGTHATNOTEXIST/,"")}1' file

Regards.
That will still pose a problem if there are two consecutive double quotes ("") in the input file
# 13  
Old 10-24-2013
Try this awk one liner...
Code:
awk '{FS="\"";for (i=1;i<=NF;i++) printf("%s%s",$i,($i ~ "[\]$" ? FS : (i < NF ? "" :"\n")))}' file

# 14  
Old 10-24-2013
Golfing with ORS:
Code:
awk '{ORS=(/\\$/ ? RS : x)}1' RS=\"

That should always work, except in the unlikely case that ....

Can you figure it out?

Regards,
Alister

Last edited by alister; 10-24-2013 at 03:57 PM..
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk: Performing "for" loop within text block with two files

I am hoping to pull multiple strings from one file and use them to search within a block of text within another file. File 1PS001,001 HLK PS002,004 MWQ PS004,002 RXM PS004,006 DBX PS004,006 SBR PS005,007 ML PS005,009 DBR PS005,011 MR PS005,012 SBR PS006,003 RXM PS006,003 >SJ PS006,010... (11 Replies)
Discussion started by: jvoot
11 Replies

2. Shell Programming and Scripting

awk - help in removing some text

Hey Guys, Earlier I asked a question under Solaris, which I got great help... thanks. Although I got the script working for what it really needed to do, I am looking for a bit of help to change the output for nicer reading. my script gets a list of zones under a global-zone and puts this... (4 Replies)
Discussion started by: dakelly
4 Replies

3. UNIX for Dummies Questions & Answers

Removing PATTERN from txt without removing lines and general text formatting

Hi Everybody! First post! Totally noobie. I'm using the terminal to read a poorly formatted book. The text file contains, in the middle of paragraphs, hyphenation to split words that are supposed to be on multiple pages. It looks ve -- ry much like this. I was hoping to use grep -v " -- "... (5 Replies)
Discussion started by: AxeHandle
5 Replies

4. Shell Programming and Scripting

Difference between /text/ and "text" in awk

Why does this search give different result in awk I do see a mix of this in the example around the net. What to use and why? data 1 = red 2 = green 3 = blue 4 = black awk '$3 == /blue/' data "no result" awk '$3 == "blue"' data 3 = blue awk '$3 ~ /blue/' data 3 = blue (9 Replies)
Discussion started by: Jotne
9 Replies

5. Shell Programming and Scripting

Removing "^M" from the end of a String (i.e. "Ctrl+M")?

Hello All, I have an Expect script that ssh's to a remote server and runs some commands before exiting. One of the commands I run is the "hostname" Command. After I run this command I save the output using this line in the code below... Basically it executes the hostname command, then I... (2 Replies)
Discussion started by: mrm5102
2 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

Extract text between two specified "constant" texts using awk

Hi All, From the title you may know that this question has been asked several times and I have done lot of Googling on this. I have a Wikipedia dump file in XML format. All the contents are in one XML file i.e. all different topics have been put in one XML file. Now I need to separate them and... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

8. Shell Programming and Scripting

removing the "\" and "\n" character using sed or tr

Hi All, I'm trying to write a ksh script to parse a file. When the "\" character is encountered, it should be removed and the next line should be concatenated with the current line. For example... this is a test line #1\ should be concatenated with line #2\ and line number 3 when this... (3 Replies)
Discussion started by: newbie_coder
3 Replies

9. Shell Programming and Scripting

Removing " " chars using Awk

HI Friends, I am trying to elliminate the " " characters from the word: "hello" using awk. I need the output to be just = hello (without " " chars). Is there any way to do this ? Thanks! (3 Replies)
Discussion started by: vijaya2006
3 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question