- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Getting the third line above the pattern match...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-20-2010 08:22 PM
тАО04-20-2010 08:22 PM
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.
Solved! Go to Solution.
- Tags:
- grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-20-2010 09:59 PM
тАО04-20-2010 09:59 PM
Re: Getting the third line above the pattern matching line and combine
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.
Horia.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-20-2010 11:05 PM
тАО04-20-2010 11:05 PM
Solutionawk '
/Server name:/ {
server=$0
next
}
/New risk found:/ {
print server, $0
}' file
Outputs:
Server name: ng171tdsep1 New risk found: WindowsAntivirusPro.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-21-2010 12:38 AM
тАО04-21-2010 12:38 AM
Re: Getting the third line above the pattern matching line and combine
Horia.
Horia.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-21-2010 04:08 AM
тАО04-21-2010 04:08 AM
Re: Getting the third line above the pattern matching line and combine
# perl -ne 'm{Server name: (.+)} and $name=$1;m{New risk} and print "$name $_"' file
ng171tdsep1 New risk found: WindowsAntivirusPro.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-21-2010 08:41 AM
тАО04-21-2010 08:41 AM
Re: Getting the third line above the pattern matching line and combine
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-21-2010 10:56 AM
тАО04-21-2010 10:56 AM
Re: Getting the third line above the pattern matching line and combine
#! /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.