Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Getting the third line above the pattern matching line and combine

 
SOLVED
Go to solution
RamkumarA
Respected Contributor

Getting the third line above the pattern matching line and combine

Hi Experts,

In the below file, i want to match "New risk found" line and once it get matched i need to get the "Server name" line which is present exactly above 2 lines from "New risk found" line and after that i need to combine both these lines and print in a single line.


------=_Part_22499_14758952.1271060277342
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

Message from:
Server name: ng171tdsep1
Server IP: 10.5.21.238

New risk found: WindowsAntivirusPro.


I tried using the below logic, but it just prints the current line and previous line as pasted below. My requirement is to retrieve the third line which is present above the "New risk found: WindowsAntivirusPro." line and then combine both these lines and print in a single line.

shcscp18:/clocal/cschpov/user/t1982ra $ awk '/New/{print s "\n" $0}{s=$0}' /tmp/testing

New risk found: WindowsAntivirusPro.

Please advise.

Thanks,
Ramkumar A.
6 REPLIES 6
Horia Chirculescu
Honored Contributor

Re: Getting the third line above the pattern matching line and combine

Hello,

You could do it in 2 steps:

In first step you will get the line numbers matching the string "New risk found" (whatever)

$STRING="New risk found"
grep -n "$STRING" my_file.txt

In the second step you extract the lines that you need.

Each line from the first step will be preceeded by line number and ':' like in

25:New risk found

So you need the line no. 22

cat -n my_file.txt |grep 22 |awk '{print $3}'

$3 will be the name of the server (without "Server Name: " - which is $2)

Of course in your script you must read 25 (from this sample) in one variable and from that variable extract 3


Horia.
Best regards from Romania,
Horia.
Dennis Handly
Acclaimed Contributor
Solution

Re: Getting the third line above the pattern matching line and combine

>I want to match "New risk found" line and once it get matched I need to get the "Server name"

awk '
/Server name:/ {
server=$0
next
}
/New risk found:/ {
print server, $0
}' file

Outputs:
Server name: ng171tdsep1 New risk found: WindowsAntivirusPro.
Horia Chirculescu
Honored Contributor

Re: Getting the third line above the pattern matching line and combine

I believe Dennis just showed us state of the art awk scripting :-)

Horia.
Best regards from Romania,
Horia.
James R. Ferguson
Acclaimed Contributor

Re: Getting the third line above the pattern matching line and combine

Hi Ramkumar:

# perl -ne 'm{Server name: (.+)} and $name=$1;m{New risk} and print "$name $_"' file
ng171tdsep1 New risk found: WindowsAntivirusPro.

Regards!

...JRF...
RamkumarA
Respected Contributor

Re: Getting the third line above the pattern matching line and combine

Hi Dennis,

Thanks for your help. Its perfectly working as expected!!

Also, thank you very much for all of the guys who gave the solution.

Thanks,
Ramkumar A.
Highlighted
Bob E Campbell
Honored Contributor

Re: Getting the third line above the pattern matching line and combine

This does not have to be very complicated. Straight shell would handle this:

#! /usr/bin/sh

IFS=:

while read L R
do
case ${L} in

"Server name") SERVER=${R} ;;

"New risk found") print "$SERVER: $R" ;;

esac
done

With output:
ng171tdsep1: WindowsAntivirusPro.