I am trying to read multiple files in an AWK script, but when changing between files, I also need to change the field separator (FS). At the moment, I have received:
FILENAME=="A.txt"{ FS=";" //DoSomething } FILENAME=="B.txt"{ FS=" - " //DoSomething }
But, as you know, FS will not be configured correctly for the first line of the file. How can i solve this?
You can specify field separators on the command line:
awk -f a.awk FS=";" A.txt FS=" - " B.txt
Thus, 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 variable assignment among arguments on the command line when awk is called
and
awk-, .
, @HakonHaegland, FS arg, . .
, (, * ), BEGINFILE, GNU awk, $0 FS, awk . :.
*
$ 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
, ( FNR==1).
FNR==1
Source: https://habr.com/ru/post/1539900/More articles:REDIRECT_ prefix in environment variables (not related to mod_rewrite) - phpUsing complex objects in SOAP requests - soapSPARQL Querying multiple ORs in the same filter - semantic-webmalloc / free in C ++: why does Free not accept const void *, and is there a better way? - c ++Specify the number of equal lines in the XML file - c #AngularJS Corner Callback - performanceJava: find the corresponding keys of two HashMaps - javaPhonegap application hides screen saver too fast - androidКак скопировать каталог из одной рабочей области Jenkins в другую? - jenkinsОшибка агрегации SQL - sqlAll Articles