简体   繁体   中英

search two different words in consecutive lines and print both consecutive lines only

I have file which contains different lines. I want to search two words "OK" and " "12.2.1.1.6.180125.1" and print both lines . Actually 1st line is host-name and second line is version hence this needs to be together . Please advise how to find either using python or shell ?

 ` cat file 

 microcldx0093.abc.com        : OK
{:output=>"12.2.1.1.6.180125.1", :exitcode=>0}

microcldx0094.abc.com        : OK
{:output=>"12.2.1.1.6.180125.1", :exitcode=>0}

microcldx0031.abc.com        : OK
{:output=>"12.2.1.1.6.180125.1", :exitcode=>0}

microcldx0032.abc.com        : OK
{:output=>"12.2.1.1.6.180125.1", :exitcode=>0}

microcldx0142.abc.com        : OK
{:output=>"12.1.2.3.4.170111", :exitcode=>0}

microcldx0157.abc.com        : OK
{:output=>"12.1.2.3.4.170111", :exitcode=>0}

microcldx0131.abc.com        : OK
{:output=>"12.1.2.3.4.170111", :exitcode=>0}

microcldx0136.abc.com        : OK
{:output=>"12.1.2.3.4.170111", :exitcode=>0}

`

 ` cat /tmp/1 |grep -e OK -e 12.2.1.1.6.180125.1
   microcldx0093.abc.com        : OK
{:output=>"12.2.1.1.6.180125.1", :exitcode=>0}
microcldx0094.abc.com        : OK
{:output=>"12.2.1.1.6.180125.1", :exitcode=>0}
microcldx0031.abc.com        : OK
{:output=>"12.2.1.1.6.180125.1", :exitcode=>0}
microcldx0032.abc.com        : OK
{:output=>"12.2.1.1.6.180125.1", :exitcode=>0}
microcldx0142.abc.com        : OK
microcldx0157.abc.com        : OK
microcldx0131.abc.com        : OK
microcldx0136.abc.com        : OK
 `

Using AWK

awk 'BEGIN {
    found = 0
}

/OK/ {
    okLine = $0;
    found = 1;
}

/12\.2\.1\.1\.6\.180125\.1/ {
    if (found = 1) {
        print okLine "\n"  $0
    } 
   found = 0
}

{
    found = 0
}' /tmp/1

The BEGIN is a pattern that is matched before any input is read. This just sets the found flag to zero.

The /OK/ pattern matches the first line you are looking for. This caches the line and sets the found flag to 1

The next pattern matches your numbers. It has to escape the dots as they are metacharacters. Unescaped, they would match any character. If thee found flag is 1 then the previous line must have matched the OK. So we output both lines. We now reset the flag so that nothing will print until we first match an OK.

The final part will match anything that hasn't been matched by a previous check. This resets the flag so that we have to start looking for an OK first again.

You could try: $ cat file | grep -A1 OK | grep -B1 '12.2.1.1.6.180125.1'

"grep -A1" gives every line containing "OK" plus one line after, within those results "grep -B1" gives lines containing "12.2.1.1.6.180125.1" plus the line before, which should give the pair of lines you're looking for.

$ cat file | grep -A1 OK | grep -B1 '12.2.1.1.6.180125.1'
 microcldx0093.abc.com        : OK
{:output=>"12.2.1.1.6.180125.1", :exitcode=>0}
--
microcldx0094.abc.com        : OK
{:output=>"12.2.1.1.6.180125.1", :exitcode=>0}
--
microcldx0031.abc.com        : OK
{:output=>"12.2.1.1.6.180125.1", :exitcode=>0}
--
microcldx0032.abc.com        : OK
{:output=>"12.2.1.1.6.180125.1", :exitcode=>0}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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