Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can field separator in awk encompass multiple characters?

Tags:

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=\",\"  $* 
like image 567
yudistrange Avatar asked Nov 24 '11 13:11

yudistrange


People also ask

Can AWK use multiple field separators?

As you can see, you can combine more than one delimiter in the AWK field separator to get specific information.

How do you use a field separator in awk?

Just put your desired field separator with the -F option in the AWK command and the column number you want to print segregated as per your mentioned field separator. Show activity on this post. AWK works as a text interpreter that goes linewise for the whole document and that goes fieldwise for each line.

What is awk default field separator?

The default field delimiter or field separator (FS) is [ \t]+ , i.e. one or more space and tab characters.

How do I change the delimiter in awk?

AWK: Change Field Separator By default, awk uses both space and tab characters as the field separator. You can tell awk how fields are separated using the -F option on the command line.


1 Answers

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
like image 118
Kent Avatar answered Sep 30 '22 21:09

Kent