我正在尝试读取 AWK 脚本中的多个文件,但是当我在文件之间进行更改时,字段分隔符 (FS) 也需要更改。此时我得到:
FILENAME=="A.txt"{
FS=";"
//DoSomething
}
FILENAME=="B.txt"{
FS=" - "
//DoSomething
}
但是您可能知道,文件的第一行不会正确设置 FS。我该如何解决这个问题?
最佳答案
您可以在命令行指定字段分隔符:
awk -f a.awk FS=";" A.txt FS=" - " B.txt
这样,每个文件的字段分隔符都会改变。
来自 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
和
With it, a variable is set either at the beginning of the awk run or in between input files.
关于unix - 在 AWK 中为多个文件更改 FS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23561985/