找到匹配后打印标题,否则不打印标题。

时间:2017-04-17 07:02:11

标签: awk

我有以下输入文件,我需要根据列放置过滤器。

 cat test
------------------------------------------------------------------------------------------
Client-code                       ID     BONUS   Availability           Offer       Credit
------------------------------------------------------------------------------------------
BLAB-LO.AJBdLODikNcpok            SK-19  0       AVAILABLE              OFFERED     NA
BLAB-LO.AJBluDikNcpok             SK-1   0       AVAILABLE              OFFERED     NA
BLAB-LO.AJDaLAgNEp                SK-19  0       AVAILABLE              OFFERED     NA
BLAB-LO.AJDaLDikNcpok             SK-1   0       AVAILABLE              OFFERED     NA
BLAB-LO.AJDbLOAgNEp               SK-19  0       AVAILABLE              OFFERED     NA
BLAB-LO.AJDbLODikNcpok            SK-19  0       AVAILABLE              OFFERED     NA
BLAB-LO.AJHbmLO                   SK-1   0       AVAILABLE              OFFERED     NA
BLAB-LO.AJAcLOBollNcpok           SK-19  0       AVAILABLE              OFFERED     NA
BLAB-LO.AJAcLOApkNamNk            SK-19  0       AVAILABLE              OFFERED     NA
KONEAAO                           SK-19  0       AVAILABLE              OFFERED     NA
BLAB-LO.AJHbmLO                   SK-11  0       AVAILABLE              OFFERED     NA
BLAB-LO.AJAcLOBollNcpok           SK-1   0       AVAILABLE              OFFERED     NA
BLAB-LO.AJAcLOApkNamNk            SK-10  0       AVAILABLE              OFFERED     NA

在这里,我根据this使用等同而不是正则表达式对第二列进行过滤

awk -v userInput='SK-1' 'NR<4 || $2 == userInput' test
------------------------------------------------------------------------------------------
Client-code                       ID     BONUS   Availibility           Offer       Credit
------------------------------------------------------------------------------------------
BLAB-LO.AJBluDikNcpok             SK-1   0       AVAILABLE              OFFERED     NA
BLAB-LO.AJDaLDikNcpok             SK-1   0       AVAILABLE              OFFERED     NA
BLAB-LO.AJHbmLO                   SK-1   0       AVAILABLE              OFFERED     NA
BLAB-LO.AJAcLOBollNcpok           SK-1   0       AVAILABLE              OFFERED     NA

如果有任何匹配,这很有效,但是,如果没有匹配,我会得到空标题。我想摆脱标题行。 (前三行。)。

这不是必需的。

 awk -v userInput='SK-122' 'NR<4 || $2 == userInput' test
------------------------------------------------------------------------------------------
Client-code                       ID     BONUS   Availibility           Offer       Credit
------------------------------------------------------------------------------------------

渴望:

如果没有匹配,我需要获取:空白或找不到匹配的消息。

awk -v userInput='SK-122' 'NR<4 || $2 == userInput' test

3 个答案:

答案 0 :(得分:2)

只需存储标题并仅在第一个 userInput 匹配时打印它:

awk -v userInput='SK-1' 'NR<4{header=sprintf("%s%s\n", header,$0);next}$2 == userInput{if (!i){printf("%s",header)};i=1;print}' test
awk -v userInput='SK-1' 'NR<4{header=sprintf("%s%s\n", header,$0);next}
                 $2 == userInput{if (!i){printf("%s",header)};i=1;print}
                 END{if (!i){print "Not found"}}' test

答案 1 :(得分:2)

缓冲,打印,清洁:

$ awk -v i="SK-1" 'NR<4{b=b $0 "\n"} $2==i {b=b $0; print b; b=""}' file
------------------------------------------------------------------------------------------
Client-code                       ID     BONUS   Availability           Offer       Credit
------------------------------------------------------------------------------------------
BLAB-LO.AJBluDikNcpok             SK-1   0       AVAILABLE              OFFERED     NA
BLAB-LO.AJDaLDikNcpok             SK-1   0       AVAILABLE              OFFERED     NA
BLAB-LO.AJHbmLO                   SK-1   0       AVAILABLE              OFFERED     NA
BLAB-LO.AJAcLOBollNcpok           SK-1   0       AVAILABLE              OFFERED     NA

答案 2 :(得分:1)

Laporan Transaksi Penjualan +something