Hi ths would give me searching "" and printing but i dont want to avoid Pipe delimited as well my file is totally having columns with pipe dilimted so i want to cosider if "xyz|zzz" occurs it should take as single col
Found this ugly code using google so can not help you with it, only that its replace the |, to make it work
Some guru may be able to simplify it
sed -e 's/|/,/g' file | awk '{
$0=$0",";
while($0) {
match($0,/[^,]*,| *"[^"]*" *,/);
sf=f=substr($0,RSTART,RLENGTH);
gsub(/^ *"?|"? *,$/,"",f);
print "Field " ++c " is " f;
sub(sf,"");
}}'
Field 1 is xx
Field 2 is yy
Field 3 is xyz,zzz
Field 4 is zzz
Field 5 is 12.
Thanks Yoda i will try to apply this but wat i needed is
i am having a file with | seperated in which i need to search char in 3rd column and replace with null. i need to replace only the coulmn where character occurs in 3rd field
for eg:
I think the only good solution for you is to change the pipe | inside double quotes "" to something else, like yoda does here: sed 's#\("[^|]*\)\([|]\)\([^|]*"\)#\1,\3#g'
The idea is good: transform the data, before it's seen by the main script, then undo the transform after the main script has done its job.
The implementation, however, is not. Modifying field contents should be avoided. It is much less intrusive to eliminate collisions between delimiter-pipes and quoted-pipes by replacing the delimiter-pipes. Why does this matter? Because delimiters are not visible from AWK script logic. Field contents, however, are visible and used in unpredictable ways.
Instead of changing xx|yy|"xyz|xx"|AAA|12... into xx|yy|"xyz,xx"|AAA|12... , it should become xx,yy,"xyz|xx",AAA,12... .
echo 'xx|yy|"xyz|zzz"|zzz|12' | awk '{c=0
$0=$0"|"
while($0) {
match($0,/ *"[^"]*" *\||[^\|]*\|/)
f=substr($0,RSTART,RLENGTH)
gsub(/^ *"?|"? *\|$/,"",f)
print "Field " ++c " is " f
$0=substr($0,RLENGTH+1)
}}'
Field 1 is xx
Field 2 is yy
Field 3 is xyz|zzz
Field 4 is zzz
Field 5 is 12
awk ' {for (i=2; i<=NF; i+=2) { # every second field is one inside double quotes
gsub (/\|/, "\001", $i) # replace every delimiter in quoted field by something
}
FS="|" # set desired field separator
$0=$0 # rebuild fields
for (i=1; i<=NF; i++) { # in every new field
gsub ("\001", "|", $i) # replace something back to commas
}
}
{for (i=1; i<=NF; i++) print $i} # print new $i fields
' FS="\"" OFS="\"" file
xx
yy
"xyz|zzz"
zzz
12