Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to escape single quote ?? 1

Status
Not open for further replies.

nnxx

Technical User
Aug 8, 2006
4
US
Hello experts,
Any idea how to substitute a character with a single quote "'" in awk ?

If I use gsub, for example ..
gsub(/out/, "in", $0)
this replaces "out" with "in". But if I want to replace "out" with "'" instead, it obviously gives an error since it thinks its EOF.

I've tried "\'" too. Any clues ?

Thanks!
nnxx
 
it obviously gives an error since it thinks its EOF
???
Either you don't know the -f option or you don't play well with the shell quoting mechanism ...
Could you please post an example of such hard gsub ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV,
Thanks for the quick response. My usage is pretty simple, maybe too simple. I have the following in a text file and I simply run it from cmd line...

gawk '{if(substr($0,1,1)=="c") {gsub(/out/, "\'", $0); print $0;} else print }' infile > outfile

How can -f help ?

Thanks!
nnxx
 
Shell quoting
Replace this [tt]"[!]\'[/!]",[/tt]
with this [tt]"[!]'"'"'[/!]",[/tt]

-f option
create an awk program in a file named, say, infile.awk[tt]
{ if(substr($0,1,1)=="c") {
gsub(/out/, [!]"'"[/!], $0); print $0
} else print
}
[/tt]
And then your shell script:
gawk -f infile.awk infile > outfile

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PH,
Thanks for your suggestions. I don't know if its my gawk version (3.1.1), but neither approach is working for me. Anyway, I'll spend some more time debugging this.

Thanks again.
Nitin.
 
but neither approach is working for me
what happen ? error message ? computer crash ? unexpected result ?

Could you please just copy/paste the following line at a shell prompt ?
gawk '/^c/{gsub(/out/,"'"'"'")}1' infile>outfile

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
That's brilliant! It works.

I like how efficient your suggestion is. I guess you must be writing awk code more than 2-3 lines every 2 years :)

Thanks again!
nnxx
 
Here's a method I sometimes use. Normally I'd use a variable, e.g., BEGIN{ QT=sprintf("%c",39) }...

Code:
awk '/^c/{gsub( /out/, sprintf( "%c", 39 ))} 1'

sed makes this easy, too. E.g.

Code:
sed "/^c/ s/out/'/g"

Cheers,
ND [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top