- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: grep and fetch previous line
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
Forums
Discussions
Discussions
Discussions
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
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
08-18-2006 06:48 AM
08-18-2006 06:48 AM
grep and fetch previous line
Line 1: pro_fw_tssndmstats_cuyr_pri_f
Line 2: 08/11/2006 23:22 08/12/2006 00:16 OH Line 3: 10335470/1
Line 1: pro_fw_tssndmstats_cuyr_pri_f
Line 2: 08/11/2006 23:22 08/12/2006 00:16 OH Line 3: 10335470/1
Now i want to find lines those has word 'OH'
in it.
I'm using grep ' OH '. It returns only
line 2, Fine. But i'm interested in knowing the job name which is available only in Line 1.
How to grep for a word and fetch the previous
line from there ???
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2006 07:01 AM
08-18-2006 07:01 AM
Re: grep and fetch previous line
I'm not sure that you can do this with grep, but it's easy to do with awk:
awk '$5=="OH" {print prev;next}
{prev=$0}' datafile
HTH
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2006 07:05 AM
08-18-2006 07:05 AM
Re: grep and fetch previous line
Create an awk file, my.awk that looks something like this:
------------------------
BEGIN {
prev = "No Previous line!"
}
{
if ($0 ~ "OH$")
{
print prev
print $0
}
prev = $0
}
------------------------
You then execute it like this:
awk -f my.awk < infile > outfile
Note that I am using an anchored string and only matching "OH" when found at the end of the line; that's what the '$' does in "OH$".
The nice thing about using awk for this is that using the posiotional variables is very easy.
For example, if the current line is this:
08/11/2006 23:22 08/12/2006 00:16 OH
$0 = the entire line
$1 = 08/11/2006
$2 = 23:22
$3 = 08/12/2006
$4 = 00:16
$5 = $NF = OH
NF (Number of fields) = 5
This should make extracting exactly what you want, very easy.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2006 07:15 AM
08-18-2006 07:15 AM
Re: grep and fetch previous line
# perl -nle 'BEGIN{$pat=shift;die unless @ARGV};print $last,"\n",$_ if m/$pat/io;$last=$_' pattern filename
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2006 07:33 AM
08-18-2006 07:33 AM
Re: grep and fetch previous line
# ex -s +"g/OH/-1,/OH/p | q!" inputfile
cheers!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2006 07:59 AM
08-18-2006 07:59 AM
Re: grep and fetch previous line
1) OH may not be always at the end of line 2.
but will be in some where in line 2.
how should i pass input to awk?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2006 08:02 AM
08-18-2006 08:02 AM
Re: grep and fetch previous line
if ($0 ~ "OH$")
to:
if ($0 ~ "OH")
"OH" will then be matched anywhere in the line.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2006 07:55 PM
08-18-2006 07:55 PM
Re: grep and fetch previous line
awk '/OH/ {print prev;next}
{prev=$0}' datafile
HTH
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2006 07:57 PM
08-19-2006 07:57 PM
Re: grep and fetch previous line
# sed -n '/^Line 1:/N;/OH/p' inputfile
~cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2006 05:22 PM
08-20-2006 05:22 PM
Re: grep and fetch previous line
We wanted the process name of a failed cron job. In this case we could get the PID by a simple logfile parse (in our case from OVO). However it was a little more difficult to get the actual name of the process that failed. To get this we had to find the second last reference to that process id and fetch the previous line.
Because the processing was done soon after the lines were added, and cron logs could be megs long, and we wanted to minimise the system load I ended up gatting a c programmer to use a reverse reading routine that he wrote to which I could pass the process id and it would return the line required. It rurned out to be extremely fast and efficient.