Search code examples
awkgawk

Can field separator in awk encompass multiple characters?


Can I use a field separator consisting of multiple characters? Like I want to separate words which contain quotes and commas between them viz.

"School","College","City"

So here I want to set my FS to be ",". But I am getting funny results when I define my FS like that. Here's a snippet of my code.

awk -F\",\" '
{
for(i=1;i<=NF;i++)
  {
    if($i~"[a-z0-9],[a-z0-9]") 
    print $i
  }
}' OFS=\",\"  $* 

Solution

  • yes, FS could be multi-characters. see the below test with your example:

    kent$  echo '"School","College","City"'|awk -F'","|^"|"$' '{for(i=1;i<=NF;i++){if($i)print $i}}'
    School
    College
    City