Want to search value and two previous value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Want to search value and two previous value
# 1  
Old 10-16-2008
Want to search value and two previous value

I am using following command to get the search value and previous line value.
sed -n -e 'N; /resize/p' alter_PU1M03.log

Please let me know how can I get the search string + previous two values and combine the output to one line.

I am using HPUX 11.11
# 2  
Old 10-16-2008

Code:
 awk '/resize/{printf "%s %s %s\n", last2, last1, $0 }
{ last2 = last1
  last1 = $0
}'  FILE

# 3  
Old 10-16-2008
Thanks it works, I really appreciate your help.
# 4  
Old 10-17-2008
awk:

Code:
nawk '{
if(index($0,"pattern")!=0)
	print l2" "l1" "$0
else
{
	l2=l1
	l1=$0
}
}' filename

sed:

Code:
pat=$1
sed -n '/'$pat'/ !{
H
}
/'$pat'/ {
H
x
s/\(.*\)\n\(.*\)\n\(.*\)\n/\2 \3/
p
}' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

2. Shell Programming and Scripting

Remove previous line if next & previous lines have same 4th character.

I want to remove commands having no output. In below text file. bash-3.2$ cat abc_do_it.txt grpg10so>show trunk group all status grpg11so>show trunk group all status grpg12so>show trunk group all status GCPKNYAIGT73IMO 1440 1345 0 0 94 0 0 INSERVICE 93% 0%... (4 Replies)
Discussion started by: Raza Ali
4 Replies

3. UNIX for Dummies Questions & Answers

Get the previous word from the search pattren

Hi, How do i find the previous worlds from the searched pattrens? Input:- Create or replace procedure some tesx. search work is procedure(case insencitive). output:- Create or replace (8 Replies)
Discussion started by: manasa_vs
8 Replies

4. Shell Programming and Scripting

solved -gawk, search for pattern - mark the previous line as a variable?

Im trying to parse ifconfig with awk and setup a bunch of variables in one shot. But Im having trouble figuring out how to work with data in previous lines. ifconfig output: eth0 Link encap:Ethernet HWaddr 00:50:DA:10:7F:1B inet addr:10.10.10.10 Bcast:10.10.10.127 ... (0 Replies)
Discussion started by: trey85stang
0 Replies

5. Shell Programming and Scripting

Append specific lines to a previous line based on sequential search criteria

I'll try explain this as best I can. Let me know if it is not clear. I have large text files that contain data as such: 143593502 09-08-20 09:02:13 xxxxxxxxxxx xxxxxxxxxxx 09-08-20 09:02:11 N line 1 test line 2 test line 3 test 143593503 09-08-20 09:02:13... (3 Replies)
Discussion started by: jesse
3 Replies

6. Shell Programming and Scripting

How to use sed to search for string and Print previous two lines and current line

Hello, Can anybody help me to correct my sed syntax to find the string and print previous two lines and current line and next one line. i am using string as "testing" netstat -v | sed -n -e '/test/{x;2!p;g;$!N;p;D;}' -e h i am able to get the previous line current line next line but... (1 Reply)
Discussion started by: nmadhuhb
1 Replies

7. Shell Programming and Scripting

Search for a pattern in a file and print previous lines from a particular point

Hi, I am new to ksh scripting and I have a problem. I have a file in which I have to search for a particular pattern say 'a' then from that line I need to search for another pattern say 'b' in the previous lines and thne print the file from pattern 'b' till the end of file. For eg: ... (2 Replies)
Discussion started by: umaislearning
2 Replies

8. Shell Programming and Scripting

Search text from a file and print text and one previous line too

Hi, Please let me know how to find text and print text and its previous line. Please don't get irritated few days back I asked text and next line. I am using HP-UX 11.11 Thanks for your help. (6 Replies)
Discussion started by: kamranjalal
6 Replies

9. Shell Programming and Scripting

need to search text and output previous lines

I have a file (OMlog0) that is quite large, I need to find the line "Automatic Recharge Audit Process Finished" and output that line and the time stamp that occurs two lines previous. The line that I was using is "sed -n '/Automatic Recharge Audit Process Finished/,/No errors/p' /sn/log/OM* >... (8 Replies)
Discussion started by: grinds
8 Replies

10. Shell Programming and Scripting

search and retrieve previous line in file

I've got a file with the following layout: #STMP FSgroup filename /filesysname1 filestatus 2 #STMP FSstatus filename /filesysname1 ratio 30 #STMP FSgroup filename ... (2 Replies)
Discussion started by: paulsew
2 Replies
Login or Register to Ask a Question