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

hmmm

hacky solution is:

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 :wink: too early on a monday morning - need more caffeine

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 :frowning:

ah, then this should work:

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,

Thanks Tytalus !!

Hi

I have a \ (backslash) as delimiter and i want a specific column from that. I have been unable to do that using either awk or sed

Input
tosattam123\mattasewq213

i want the Output
mattasewq213

I have tried to escape the \ with / " ' and many other as well but to no avail

can anybody help

echo 'tosattam123\mattasewq213' | awk -F'\\' '{print $2}'

thank you

The awk escape character is a backslash:

\

Its also the escape character in vi if you were curious :wink: