Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change FS in AWK for multiple files

Tags:

unix

fs

awk

I'm trying to read multiple files in an AWK-script but when I change between file, the field seperator (FS) needs to change as well. At this point I got:

FILENAME=="A.txt"{
    FS=";"
    //DoSomething
}
FILENAME=="B.txt"{
    FS=" - "
    //DoSomething
}

But as you might know, the FS will not get set correctly for the first line of the file. How can I solve this?

like image 713
TheJsp Avatar asked Dec 06 '22 02:12

TheJsp


1 Answers

You can specify the field separators at the command line:

awk -f a.awk FS=";" A.txt FS=" - " B.txt

In this way, the field separator will change for each file. From http://www.delorie.com/gnu/docs/gawk/gawk_82.html :

Any awk variable can be set by including a variable assignment among the arguments on the command line when awk is invoked

and

With it, a variable is set either at the beginning of the awk run or in between input files.

like image 78
Håkon Hægland Avatar answered Jan 09 '23 17:01

Håkon Hægland