awk

时间:2017-10-13 23:18:49

标签: awk

你能解释一下这个命令是做什么的吗? dateA =" $ dateA" '?

awk 'FNR>1 && dateA<=$5' FS='|' dateA="$dateA" "$infile"

3 个答案:

答案 0 :(得分:1)

请仔细阅读以下说明,如果有帮助,请告知我们。

说明: 请不要在awk之后运行,只是为了解释目的而扩展。

awk '
FNR>1 && dateA<=$5 ##FNR denotes the number of current line in awk so here 2 conditions with AND conditions are being checked.
                   ##1st is if current line number is greater than 1 and second is variable named dateA value should be lesser
                   ##and equal to 5. 
                   ##So let me explain here awk works on method of condition and then action, so if any condition is TRUE then action
                   ##will happen, here condition is there but NO action defined, so by default print action will happen. print of 
                   ##current line.
' 
FS='|'             ##FS denotes the field separator, in awk we could define the field separator by ourselves too, so making it here as |
dateA="$dateA"     ##creating variable named dateA whose value is equal to shell variable named dateA. In awk if we have to assign 
                   ##shell variable values to awk variables we have to create an awk variable and then assign shell variable value to
                   ##it.
"$infile"          ##Mentioning the Input_file name here which awk has to go through. Point to be noted here the "$infile" means
                   ##it is a shell variable (as we all know to print shell variable value we have to use "$infile")

答案 1 :(得分:1)

awk 'FNR > 1 && dateA <= $5 ' FS='|' dateA="$dateA" "$infile"
  • FNR是变量,为您提供与当前文件相关的记录总数,不要与变量NR混淆,FNRNR值将只要awk读取第一个文件就相同,对于第二个文件,变量FNR将重置,而NR则不会。

这是FNRNRawk

中的工作原理
$ seq 1 5 >file1
$ seq 1 3 >file2
$ cat file1
1
2
3
4
5

$ cat file2
1
2
3

$ awk '{print "Current line : "$0,"File: "FILENAME,"FNR : ",FNR,"NR : ",NR}' file1 file2
Current line : 1 File: file1 FNR :  1 NR :  1
Current line : 2 File: file1 FNR :  2 NR :  2
Current line : 3 File: file1 FNR :  3 NR :  3
Current line : 4 File: file1 FNR :  4 NR :  4
Current line : 5 File: file1 FNR :  5 NR :  5
Current line : 1 File: file2 FNR :  1 NR :  6
Current line : 2 File: file2 FNR :  2 NR :  7
Current line : 3 File: file2 FNR :  3 NR :  8
  • FNR > 1 && dateA <= $5如果读取的记录数不大于1且变量dateA小于或等于第5个字段/列,则会得到布尔值真状态,因此将打印此行

  • FS='|' FS是输入字段分隔符,您也可以将其设置为

    • awk -F'|' '{ .... }'
    • awk -v FS='|' '{ .... }'
    • awk 'BEGIN{FS="|"}{ .... }'
  • dateA="$dateA" dateAawk变量,其值取自您的shell变量$dateA,同样您可以将其设置为

    • awk -v dateA="$dateA" '{ .... }'

您的上述命令也可以像下面那样重写

awk -F'|' -v dateA="$dateA" 'FNR>1 && dateA <= $5' "$infile"

有些人更喜欢awk 'condition{action}'以便更好地阅读,因此您也可以将其写为

awk -F'|' -v dateA="$dateA" 'FNR>1 && dateA <= $5{ print }' "$infile"
                                    ^                 ^
                                    |                 |
                          If this condition is true   |
                                                      |
                                   Action is to print line,
                                   print or print $0 is same

答案 2 :(得分:0)

AWK允许在参数中使用var=value形式分配内部变量。由于AWK无法访问shell变量dateA="$dateA"用于将dateA“导出”到AWK脚本。

请注意,赋值参数在BEGIN之后的文件处理期间发生,并且可以在文件之间使用:

$ echo >file1; echo >file2
$ awk -vx=0 '
        BEGIN {
                print "BEGIN", x
        }
        {
                print FILENAME, x
        }
        END {
                print "END", x
        }' x=1 file1 x=2 file2 x=3
BEGIN 0
file1 1
file2 2
END 3