使用awk,如果下面的条件不符合,如何将记录保存到另一个文件?

时间:2022-05-21 16:02:33

How can I save records to another file if the condition below doesn't meet. I only have "if" below, i do not know where to put else condition.

如果下面的条件不符合,如何将记录保存到另一个文件。我只有“如果”下面,我不知道在哪里放其他条件。

 awk '                                       
 NF {                                        
 data  = substr ($0, 1, 14)    
 data2 = substr ($0, 1, 1)         
 count = gsub (/0/, "", data)               
  }   

 {if (count<=11 || data2!=5) print }' $FILES > ${DST}/${FILES}

1 个解决方案

#1


2  

You can do the redirection inside awk script. Call your bash variables from awk by using the -v construct. FILENAME is awk built-in variable that holds the name of the file which it will receive from ${FILES} variable during run-time.

您可以在awk脚本中进行重定向。使用-v构造从awk调用bash变量。 FILENAME是awk内置变量,它保存在运行时从$ {FILES}变量接收的文件的名称。

awk -v dst="${DST}" -v dstb="${DST_B}" '                                       
NF {                                        
    data  = substr ($0, 1, 14)    
    data2 = substr ($0, 1, 1)         
    count = gsub (/0/, "", data)               
    if (count<=11 || data2!=5) {
        print > ( dst "/" FILENAME )
    }
    else {
        print > ( dstb "/" FILENAME )
    }
}' $FILES 

#1


2  

You can do the redirection inside awk script. Call your bash variables from awk by using the -v construct. FILENAME is awk built-in variable that holds the name of the file which it will receive from ${FILES} variable during run-time.

您可以在awk脚本中进行重定向。使用-v构造从awk调用bash变量。 FILENAME是awk内置变量,它保存在运行时从$ {FILES}变量接收的文件的名称。

awk -v dst="${DST}" -v dstb="${DST_B}" '                                       
NF {                                        
    data  = substr ($0, 1, 14)    
    data2 = substr ($0, 1, 1)         
    count = gsub (/0/, "", data)               
    if (count<=11 || data2!=5) {
        print > ( dst "/" FILENAME )
    }
    else {
        print > ( dstb "/" FILENAME )
    }
}' $FILES