简体   繁体   English

使用一个awk输出到另一个awk命令

[英]Using one awk output into another awk command

I have one file (excel file) which has some columns (not fixed, changes dynamically) and I need to get values for couple of particular columns. 我有一个文件(excel文件),它有一些列(不固定,动态更改),我需要获取几个特定列的值。 I'm able to get the columns using one awk command and then printing rows using these columns numbers into another awk command. 我可以使用一个awk命令获取列,然后使用这些列号将行打印到另一个awk命令中。 Is there any way I can combine into one? 有什么方法可以组合成一个吗?

awk -F',' ' {for(i=1;i < 9;i++) {if($i ~ /CLIENT_ID/) {print i}}} {for(s=1;s < 2;s++) {if($s ~ /SEC_DESC/) {print s}}} ' <file.csv> | awk -F "," '!($5~/...[0-9]L/ && $21~/FUT /) {print $0}' <file.csv>

Gives me output as 5 and 9 for columns (client_id and sec_desc`), which is their column number (this changes with different files). 为列(client_id and sec_desc`)提供输出为5和9,这是它们的列号(这会随着不同的文件而变化)。

Now using this column number, I get the desired output as follows: 现在使用此列号,我得到所需的输出,如下所示:

awk -F "," '!($5~/...[0-9]L/ && $21~/FUT /) {print $0}' <file.csv>

How can I combine these into one command? 如何将这些组合成一个命令? Pass a variable from the first to the second? 将变量从第一个传递到第二个?

Input (csv file having various dynamic columns, interested in following two columns) 输入(具有各种动态列的csv文件,对以下两列感兴趣)

CLIENT_ID   SEC_DESC
USZ256      FUT DEC 16 U.S.
USZ256L     FUT DEC 16 U.S. BONDS
WNZ256      FUT DEC 16 CBX
WNZ256L     FUT DEC 16 CBX BONDS

Output give me rows- 2 and 4 that matched my regex pattern in second awk command (having column numbers as 5 & 21). 输出给我行-2和4匹配我的正则表达式模式在第二个awk命令(列号为5和21)。 These column numbers changes as per file so first have to get the column number using first awl and then giving it as input to second awk. 这些列号按文件更改,因此首先必须使用第一个awl获取列号,然后将其作为第二个awk的输入。

To solve your problem you can test when you're processing the first row, and put the logic to discover the column numbers there. 要解决您的问题,您可以测试何时处理第一行,并将逻辑放在那里发现列号。 Then when you are processing the data rows, use the column numbers from the first step. 然后,在处理数据行时,请使用第一步中的列号。

( NR is an awk built-in variable containing the record number being processed. NF is the number of columns.) NR是一个awk内置变量,包含正在处理的记录号NF是列数。)

Eg: 例如:

$ cat red.awk
NR == 1 {
  for (i=1; i<=NF; ++i) {
    if ($i == "CLIENT_ID") cl_col = i;
    if ($i == "SEC_DESC") sec_col = i;
  }
}

NR > 1 && $cl_col ~ /...[0-9]L/ && $sec_col ~ /FUT /


$ awk -F'\t' -f red.awk RED_FUT_TST.csv
USZ256L FUT DEC 16 U.S. BONDS
WNZ256L FUT DEC 16 CBX BONDS

I think I got it. 我想我明白了。

awk -F',' '
    NR == 1 {
        for (i=1; i<=NF; ++i) {
            if ($i == "CLIENT_ID") cl_col = i
            if ($i == "SEC_DESC") sec_col = i
        }
    }
    NR > 1 && !($cl_col ~ /...[0-9]L/ && $sec_col ~ /FUT /) {print $0}
' RED_FUT_TST.csv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM