Removing " from a text using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing " from a text using awk
# 1  
Old 10-24-2013
Removing " from a text using awk

I was testing some and from this string try to remove all ", but not \"
Code:
cat file
The quick brown fox "jumps", over  the 'lazy \"dog\"'

result requested: The quick brown fox jumps, over the 'lazy \"dog\"'

I have seen a working solution for sed, but I like awk Smilie

This code seem to work, but for some reason it does remove a blank space after the fox, why?
Code:
awk '{gsub(/[^\\]\"/,x)}1' file
The quick brown foxjump, over  the 'lazy \"dog\"'

As far as I understand this means not \ and ", so why is the space gone?

EDIT:
I found why Smilie
not \ can is any characters but not \, so space is gone and the s in jumps
Any Idea on how to fix this in awk?
gensub should work, but not very portable.

---------- Post updated at 13:26 ---------- Previous update was at 12:57 ----------

I found two working version.
Fist is not perfect, but is portable.
Second is less portable.
Code:
awk '{gsub(/\\\"/,"_#_");gsub(/\"/,x);gsub(/_#_/,"\\\"")}1'

Code:
awk '{print gensub(/([^\\])\"/, "\\1", "g")}'


Last edited by Jotne; 10-24-2013 at 08:08 AM..
# 2  
Old 10-24-2013
Try one more

Code:
$awk -F '\\\\"' '{for(i=1;i<=NF;i++){gsub("\"","",$i)}}1' OFS='\\"' file

The quick brown fox jumps, over  the 'lazy \"dog\"'

# 3  
Old 10-24-2013
Would this help you

Code:
$ awk '{gsub(/[^\\A-Za-z]\"/," ");gsub(/"\,/,",")}1' file

Resulting
Code:
The quick brown fox jumps, over  the 'lazy \"dog\"'

your s is missing because gsub(/[^\\]\"/," ") its removing space + string after the space it was suppose to be
gsub(/[^\\A-Za-z]\"/," ")

For example

Code:
$ echo "test \"demo\" " | awk '{gsub(/[^\\]\"/," ");}1'
test dem  # o is missing here

Code:
$ echo "test \"demo\" " | awk '{gsub(/[^\\A-Za-z]\"/," ");}1'
test demo"


Last edited by Akshay Hegde; 10-24-2013 at 08:56 AM..
# 4  
Old 10-24-2013
@Akshay Hegde
Your solution fail with this line:
Code:
The quick brown fox "jumps", over  the 'lazy \"dog\"' here are some "more data"

it gives
Code:
The quick brown fox jumps, over  the 'lazy \"dog\"' here are some more data"

The blue " at final is not removed.

Last edited by Jotne; 10-24-2013 at 09:32 AM.. Reason: Removed errors
# 5  
Old 10-24-2013
Quote:
Originally Posted by Jotne
@Akshay Hegde
Your solution fail with this line:
Code:
The quick brown fox "jumps", over  the 'lazy \"dog\"' here are some "more data"

it gives
Code:
The quick brown fox jumps, over  the 'lazy \"dog\"' here are some  more data"

The blue " infront to more is lost.

@pamu
Your fail with both the last " giving
Code:
The quick brown fox jumps, over  the 'lazy \"dog\"' here are some  more data

Edit: and the my solution fails with this too Smilie



Thanks Jotne

Try... for given input this will work Smilie

Code:
$ awk '{gsub(/[^\\A-Za-z]\"|"$/," ");gsub(/"\, /,",")}1' file
The 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 }' file
The quick brown fox jumps, over the 'lazy \"dog\"' here are some more data


Last edited by Akshay Hegde; 10-24-2013 at 09:17 AM..
# 6  
Old 10-24-2013
I messed some up, sorry Smilie
pamus solution is ok, and mine to.
And Akshay Hegdes now works fine.
# 7  
Old 10-24-2013
Hi,
If you use the commutator &:
Code:
awk '{gsub(/[^\\]"/,"&\"");gsub(/""/,"")}1' file

Regards.
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