The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to extract first column with a specific character selamba_warrior Shell Programming and Scripting 3 05-22-2008 02:14 AM
Escaping the * character in ksh. arvindcgi Shell Programming and Scripting 6 05-19-2008 06:50 AM
[csh] checking for specific character ranges in a variable userix Shell Programming and Scripting 5 05-11-2008 04:56 AM
How to change a specific character in a file sdubey Shell Programming and Scripting 6 02-22-2008 12:30 PM
How to add character in specific position of a string? victorlung Shell Programming and Scripting 5 09-01-2006 07:33 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-28-2008
Registered User
 

Join Date: Jan 2008
Posts: 13
Exclamation Escaping specific character in awk

Hello there,

I have a bit of dirty delimited file, I mentioned dirty because, the delimiter can also appear in wrong positions. However, one uniqueness of this file is
whenever the delimiter appear inside the double quote, then do not consider as delimiter, if it appear outside double then consider it as delimiter.


contents looks like below

abc;def;ghi;"kl;mn;op" ;qrst;uv;w;xyz;

AWk

inp=$1
nawk -F";" '{ print $1"~"$2"~"$3"~"$4"~"$5"~"$6"~"$7"~"$8; }' $inp >> ${inp}_det.txt

gives me ouptut as

abc ~def~ghi~"kl~mn~op" ~qrst~uv

But expected output is

abc ~def~ghi~"kl;mn;op" ~qrst~uv~w~xyz


I'm kind of stuck how to escape the double quote . I appreciate any pointers.

thanks
Reply With Quote
Forum Sponsor
  #2  
Old 01-28-2008
Tytalus's Avatar
Registered User
 

Join Date: Jun 2003
Location: Scotland
Posts: 290
hmmm

hacky solution is:

Code:
tr "\"" "%" <file1 | nawk -F";" '{ print $1"~"$2"~"$3"~"$4"~"$5"~"$6"~"$7"~"$8; }' | tr "%" "\""
i.e. convert the "'s to %'s then map them back later....


EDIT: nope - ignore me - I'm talking rubbish ;-) too early on a monday morning - need more caffeine
Reply With Quote
  #3  
Old 01-28-2008
Registered User
 

Join Date: Jan 2008
Posts: 13
Hi,

To be more precise, all the fields are double quoted and delimited by semi colon ;

something like:
"abc";"def";"ghi";"kl;mn;op" ;"qrst";"uv";"w";"xyz";

I've preprocessed the files by removing all double quote & then noticed, probably that wouldn't work, as I need some sort of marker to tell that any values inside double quote is just value and not delimiter. Somhow struggling to press the right keys
Reply With Quote
  #4  
Old 01-28-2008
Tytalus's Avatar
Registered User
 

Join Date: Jun 2003
Location: Scotland
Posts: 290
ah, then this should work:

Code:
sed 's/\";"/%/g' file | nawk -F"%" '{ print $1"~"$2"~"$3"~"$4"~"$5"~"$6"~"$7"~"$8; }'
same sort of trick - replace ";" with %, and use that % as delimeter,
Reply With Quote
  #5  
Old 01-28-2008
Registered User
 

Join Date: Jan 2008
Posts: 13
Thanks Tytalus !!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:28 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0