سؤال

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?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

You can do it as @HakonHaegland suggests by setting FS between file names in the arg list if you are listing the files individually. That is the typical way to do this.

Alternatively, if you can't do that (e.g. because you need to use * or similar for the file list), then you can use BEGINFILE if you are using GNU awk, but otherwise you can do it the way you are already by adding an assignment of $0 to itself after changing FS to force awk to re-split the record. e.g.:

$ cat file
a-b-c
d e f

$ awk '{print NF, $1}' file
1 a-b-c
3 d

$ awk '{FS="-"; $0=$0; print NF, $1}' file
3 a
1 d e f

If you are going to do it that way it's best done just once at the start of each file (when FNR==1).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top