如何使用awk命令打印模式匹配行以及我需要使用另一个模式匹配打印下一行

时间:2015-02-11 09:58:17

标签: unix

我的档案是这样的:

Setting application AAA active for user [usernme@mailid.com]
Connected from [::ffff:161.179.445.43]
Received client request: List database locks (from user [usernme@mailid.com)
Connected from [::ffff:161.179.221.43]
Setting application AAA active for user [usernme@mailid.com]
Connected from [::ffff:161.179.445.43]
Setting application AAA active for user [usernme@mailid.com]
Connected from [::ffff:161.179.445.43]
Setting application AAA active for user [usernme@mailid.com]
Received client request: List database locks (from user [usernme@mailid.com)
Setting application AAA active for user [usernme@mailid.com]
Setting application AAA active for user [usernme@mailid.com]

当搜索设置应用程序时,它必须打印设置应用程序,如果下一行有IP地址,则必须打印下一行  Connected from [::ffff:161.179.445.43]否则只需打印设置应用程序行。

我的输出如下:

Setting application AAA active for user [usernme@mailid.com]
Connected from [::ffff:161.179.445.43]
Setting application AAA active for user [usernme@mailid.com]
Connected from [::ffff:161.179.445.43]
Setting application AAA active for user [usernme@mailid.com]
Connected from [::ffff:161.179.445.43]
Setting application AAA active for user [usernme@mailid.com]
Setting application AAA active for user [usernme@mailid.com]
Setting application AAA active for user [usernme@mailid.com]

请帮我解释如何为此

编写unix代码

2 个答案:

答案 0 :(得分:0)

我会用sed这样做。请考虑以下事项:

 [ test $] cat test.in                                                      
Setting application AAA active for user [usernme@mailid.com]                
Connected from [::ffff:161.179.445.43]
Received client request: List database locks (from user [usernme@mailid.com)
Connected from [::ffff:161.179.221.43]
Setting application AAA active for user [usernme@mailid.com]
Connected from [::ffff:161.179.445.43]
Setting application AAA active for user [usernme@mailid.com]
Connected from [::ffff:161.179.445.43]
Setting application AAA active for user [usernme@mailid.com]
Received client request: List database locks (from user [usernme@mailid.com)
Setting application AAA active for user [usernme@mailid.com]
Setting application AAA active for user [usernme@mailid.com]
Connected from [::ffff:161.179.445.44]
Setting application AAA active for user [usernme@mailid.com]
Setting application AAA active for user [usernme@mailid.com]
Setting application AAA active for user [usernme@mailid.com]
Connected from [::ffff:161.179.445.45]
Setting application AAA active for user [usernme@mailid.com]
Setting application AAA active for user [usernme@mailid.com]
Received client request: List database locks (from user [usernme@mailid.com)
Setting application AAA active for user [usernme@mailid.com]
Received client request: List database locks (from user [usernme@mailid.com)
Setting application AAA active for user [usernme@mailid.com]
Connected from [::ffff:161.179.445.46]
Setting application AAA active for user [usernme@mailid.com]
Received client request: List database locks (from user [usernme@mailid.com)
Setting application AAA active for user [usernme@mailid.com]
Received client request: List database locks (from user [usernme@mailid.com)
Setting application AAA active for user [usernme@mailid.com]
Received client request: List database locks (from user [usernme@mailid.com)
Setting application AAA active for user [usernme@mailid.com]
Connected from [::ffff:161.179.445.47]


 [ test $]
 [ test $]

以下sed程序:

 [ test $] cat test.sed
#!/bin/sed  -nf

/Setting application/{
         N;
         /ffff:[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/{p;d};
         /\nSetting application/!{
                      s|\n.*||;p;d;
        }
        P;D
}

给出了这个输出:

 [ test $] ./test.sed test.in
Setting application AAA active for user [usernme@mailid.com]
Connected from [::ffff:161.179.445.43]
Setting application AAA active for user [usernme@mailid.com]
Connected from [::ffff:161.179.445.43]
Setting application AAA active for user [usernme@mailid.com]
Connected from [::ffff:161.179.445.43]
Setting application AAA active for user [usernme@mailid.com]
Setting application AAA active for user [usernme@mailid.com]
Setting application AAA active for user [usernme@mailid.com]
Connected from [::ffff:161.179.445.44]
Setting application AAA active for user [usernme@mailid.com]
Setting application AAA active for user [usernme@mailid.com]
Setting application AAA active for user [usernme@mailid.com]
Connected from [::ffff:161.179.445.45]
Setting application AAA active for user [usernme@mailid.com]
Setting application AAA active for user [usernme@mailid.com]
Setting application AAA active for user [usernme@mailid.com]
Setting application AAA active for user [usernme@mailid.com]
Connected from [::ffff:161.179.445.46]
Setting application AAA active for user [usernme@mailid.com]
Setting application AAA active for user [usernme@mailid.com]
Setting application AAA active for user [usernme@mailid.com]
Setting application AAA active for user [usernme@mailid.com]
Connected from [::ffff:161.179.445.47]
 [ test $]

如果您想要解释,或者这不能做您想做的事,请回复。请不要忘记评价答案。

顺便说一句,我是在centos 6上运行这个:

[ test $] sed --version
GNU sed version 4.2.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.

GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-gnu-utils@gnu.org>.
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.
 [ test $]

编辑:

#!/bin/sed  -nf

/Setting application/{ # match this string
         N; # pull in the next line
         /ffff:[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/{p;d}; # if it contains an ip address print and restart processing
         /\nSetting application/!{ 
                # otherwise check it doesn't contain this text
                      s|\n.*||;p;d;# if not, remove the 2nd line, print and restart processing
        }
        P;D # else print then delete only the first part, then restart processing on the next line.
}

答案 1 :(得分:0)

awk '/Setting application/{print; k=1; next}; k && /Connected from/; {k=0}' input-file

或稍微优雅一点:

awk 'k && /Connected from/; k=/Setting application/' input-file