3

我不确定 && 运算符是否适用于正则表达式。我要做的是匹配一行,使其以数字开头并具有字母“a”并且下一行以数字开头并具有字母“b”和下一行...字母“c” . 此 abc 序列将用作开始读取文件的唯一标识符。

这就是我在 awk 中的目标。

/(^[0-9]+ .*a)&&\n(^[0-9]+ .*b)&&\n(^[0-9]+ .*c) {
print $0
}

这些正则表达式中只有一个像 (^[0-9]+ .*a) 一样工作,但我不确定如何将它们串在一起,下一行就是这样。

我的文件会是这样的:

JUNK UP HERE NOT STARTING WITH NUMBER
1     a           0.110     0.069          
2     a           0.062     0.088          
3     a           0.062     0.121          
4     b           0.062     0.121          
5     c           0.032     0.100         
6     d           0.032     0.100          
7     e           0.032     0.100   

我想要的是:

3     a           0.062     0.121          
4     b           0.062     0.121          
5     c           0.032     0.100         
6     d           0.032     0.100          
7     e           0.032     0.100 
4

3 回答 3

1

不,它不起作用。你可以尝试这样的事情:

/(^[0-9]+.*a[^\n]*)\n([0-9]+.*b[^\n]*)\n([0-9]+.*c[^\n]*)/

并根据需要对尽可能多的字母重复此操作。

The [^\n]* will match as much non-linebreak characters in a row as possible (so up to the linebreak).

于 2012-10-03T23:46:57.957 回答
1

[Update based on clarification.]

One high order bit is that Awk is a line-oriented language, so you won't actually be able to do a normal pattern match to span lines. The usual way to do something like this is to match each line separately, and have a later clause / statement figure out if all the right pieces have been matched.

What I'm doing here is looking for an a in the second field on one line, a b in the second field on another line, and a c in the second field on a third line. In the first two cases, I stash away the contents of the line as well as what line number it occurred on. When the third line is matched and we haven't yet found the whole sequence, I go back and check to see if the other two lines are present and with acceptable line numbers. If all's good, I print out the buffered previous lines and set a flag indicating that everything else should print.

Here's the script:

$2 == "a" { a = $0; aLine = NR; }
$2 == "b" { b = $0; bLine = NR; }
$2 == "c" && !keepPrinting {
    if ((bLine == (NR - 1)) && (aLine == (NR - 2))) {
        print a;
        print b;
        keepPrinting = 1;
    }
}
keepPrinting { print; }

And here's a file I tested it with:

JUNK UP HERE NOT STARTING WITH NUMBER
1     a           0.110     0.069
2     a           0.062     0.088
3     a           0.062     0.121
4     b           0.062     0.121
5     c           0.032     0.100
6     d           0.032     0.100
7     e           0.032     0.100
8     a           0.099     0.121
9     b           0.098     0.121
10    c           0.097     0.100
11    x           0.000     0.200

Here's what I get when I run it:

$ awk -f blort.awk blort.txt
3     a           0.062     0.121
4     b           0.062     0.121
5     c           0.032     0.100
6     d           0.032     0.100
7     e           0.032     0.100
8     a           0.099     0.121
9     b           0.098     0.121
10    c           0.097     0.100
11    x           0.000     0.200
于 2012-10-04T00:44:06.023 回答
0

A friend wrote this awk program for me. It is a state machine. And it works.

#!/usr/bin/awk -f

BEGIN {
    # We start out in the "idle" state.
    state = "idle"
}

/^[0-9]+[[:space:]]+q/ {
    # Everytime we encounter a "# q" we either print it or go to the
    # "q_found" state.
    if (state != "printing") {
        state = "q_found"
        line_q = $0
    }
}

/^[0-9]+[[:space:]]+r/ {
    # If we are in the q_found state and "# r" immediate follows,
    # advance to the r_found state.  Else, return to "idle" and 
    # wait for the "# q" to start us off.
    if (state == "q_found") {
        state = "r_found"
        line_r = $0
    } else if (state != "printing") {
        state = "idle"
    }
}

/^[0-9]+[[:space:]]+l/ {
    # If we are in the r_found state and "# l" immediate follows,
    # advance to the l_found state.  Else, return to "idle" and 
    # wait for the "# q" to start us off.
    if (state == "r_found") {
        state = "l_found"
        line_l = $0
    } else if (state != "printing") {
        state = "idle"
    }
}

/^[0-9]+[[:space:]]+i/ {
    # If we are in the l_found state and "# i" immediate follows,
    # we're ready to start printing.  First, display the lines we
    # squirrelled away then move to the "printing" state.  Else,
    # go to "idle" and wait for the "# q" to start us off.
    if (state == "l_found") {
        state = "printing"
        print line_q
        print line_r
        print line_l
        line = 0
    } else if (state != "printing") {
        state = "idle"
    }
}

/^[0-9]+[[:space:]]+/ {
    # If in state "printing", print 50 lines then stop printing
    if (state == "printing") {
        if (++line < 48) print
    }
}
于 2012-10-04T18:44:11.733 回答