1

如果匹配,我需要匹配一些模式我需要打印接下来的几个单词。我只有三个这样的条件要匹配……但我只需要打印满足第一个条件后出现的那些单词……iez。 .

虽然在 LLL SSS 之前我有 TTT ,但我只需要将 LLL SSS 之后的那些打印到 AAA RRR ,当我遇到 LLL SSS 需要做同样的事情时,我尝试声明一个 awk 标志变量,但它没有t 似乎工作。请帮助我。

var1="LLL";var2="SSS";var3="TTT";var4="AAA";var5="RRR";
awk -v flag=1 '{
for (m=1;m<=NF;m++)
if($m~m/'$var1'/ && $(m+1)~/'$var2'/)
print "ABCD: ", $(m+2),$(m+3),$(m+4)
{flag=0};
else {
if {flag==0 && $m~/'var3'/)
print "EFG : ", $(m+1);
else { 
if (flag==0 && $m~/'$var4'/ && $(m+1)~/'var5'/)
print "HIJ: ",$(m+2),$(m+3) 
{flag=1}; 
}
}
}
}' fliename 

我的文件是这样的:

AAA RRR H I
DDDD OOO HI J UGC RR
TTT YYY
LLLLL IIIIII
LLL SSS A B C D
N
DDDD TTT EFG
BBB AAA RRR J K
HHHHHH RRRRRR 
LLL SSS E H G H
N
DDDD TTT EFG
UUUUU BBB AAA RRR J K
4

2 回答 2

3

When you have multiple nested ifs like this, it's really important to use braces to be completely unambiguous. I've made an attempt to rewrite your code.

  • use -v to pass shell variables into awk instead of playing with quoting
  • whitespace is your friend -- or rather it's the friend of the next guy who has to maintain your code.
  • fixed a couple of errors on line 8 of your script
var1="LLL";var2="SSS";var3="TTT";var4="AAA";var5="RRR";
awk -v flag=1 \
    -v var1="$var1" \
    -v var2="$var2" \
    -v var3="$var3" \
    -v var4="$var4" \
    -v var5="$var5" \
'{
    for (m=1; m<=NF; m++) {
        if ($m ~ var1 && $(m+1) ~ var2) {
            print "ABCD: ", $(m+2),$(m+3),$(m+4)
            flag=0
        }
        else {
            if (flag == 0 && $m ~ var3) {
                print "EFG : ", $(m+1)
            }
            else {
                if (flag == 0 && $m ~ var4 && $(m+1) ~ var5) {
                    print "HIJ: ",$(m+2),$(m+3)
                    flag=1
                }
            }
        }
    }
}' fliename
于 2013-05-28T16:43:22.270 回答
0

格伦的版本有所改进。我否定flag了,所以可以跳过初始化。

var1="LLL";var2="SSS";var3="TTT";var4="AAA";var5="RRR";
awk -v var1="$var1" -v var2="$var2" -v var3="$var3" -v var4="$var4" -v var5="$var5" \
'!flag {
    for (m=1; m<=NF-1; ++m) 
        if ($m == var1 && $(m+1) == var2) {
            print "ABCD: ", $(m+2),$(m+3),$(m+4)
            flag=1
            next
        }
 }
 flag {
    for (m=1; m<=NF-1; ++m) 
        if ($m == var3) {
            print "EFG : ", $(m+1)
            break
        } else {
            if ($m == var4 && $(m+1) == var5) {
                print "HIJ: ",$(m+2),$(m+3)
                flag=0
                break
            }
        }
}' filename

输出:

ABCD:  A B C
EFG :  EFG
HIJ:  J K
ABCD:  E H G
EFG :  EFG
HIJ:  J K

或者您甚至可以尝试纯 bash 版本:

var1="LLL";var2="SSS";var3="TTT";var4="AAA";var5="RRR";

found=0;
while read -a a; do
  if [ $found -eq 0 ]; then
    for((i=0;((i+1))<${#a[*]};++i)) {
      [ ${a[$i]} == $var1 -a ${a[((i+1))]} == $var2 ] &&
        echo ABCD: ${a[((i+2))]} ${a[((i+3))]} ${a[((i+4))]} &&
        found=1 && break
    }
  else
    for((i=0;((i+1))<${#a[*]};++i)) {
      [ ${a[$i]} == $var3 ] && echo EFG: ${a[((i+1))]} && break
      [ ${a[$i]} == $var4 -a ${a[((i+1))]} == $var5 ] && found=0 &&
        echo HIJ:  ${a[((i+2))]} ${a[((i+3))]} && break
    }
  fi
done <filename
于 2013-05-28T20:43:18.343 回答