awk中特定命令的说明

awk中特定命令的说明,awk,Awk,你能解释一下这个命令“dateA=“$dateA”是怎么做的吗 awk'FNR>1&&dateAawk允许在参数中以var=value的形式分配内部变量。由于AWK无权访问shell变量dateA=“$dateA”用于将dateA导出到AWK脚本 请注意,赋值参数出现在文件处理过程中的BEGIN之后,可以在文件之间使用: $ echo >file1; echo >file2 $ awk -vx=0 ' BEGIN { print "BE

你能解释一下这个命令“dateA=“$dateA”是怎么做的吗


awk'FNR>1&&dateAawk允许在参数中以
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

请仔细阅读下面的解释,并让我知道这是否对您有帮助

解释:请不要运行以下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")
awk'
FNR>1&日期a
  • FNR>1&&dateA
    
    awk 'FNR > 1 && dateA <= $5 ' FS='|' dateA="$dateA" "$infile"
    
    $ 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
    
    awk -F'|' -v dateA="$dateA" 'FNR>1 && dateA <= $5' "$infile"
    
    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