selecting a line and line above it.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting selecting a line and line above it.
# 1  
Old 08-13-2008
selecting a line and line above it.

hi..i have a file like the following

Application handle = 2604
Application status = UOW Waiting
Application handle = 2384
Application status = UOW Waiting
Application handle = 2383
Application status = UOW Waiting
Application handle = 1679
Application status = Lock-wait
Application handle = 2248
Application status = UOW Waiting
Application handle = 2236
Application status = UOW Waiting
Application handle = 2382
Application status = UOW Waiting
Application handle = 2863
Application status = Lock-wait
Application handle = 2381
Application status = UOW Waiting
Application handle = 2380
Application status = UOW Waiting


here i need just only the lines with
Application handle = 1679
Application status = Lock-wait
Application handle = 2863
Application status = Lock-wait

ie the line with Application status = Lock-wait and the line above it

any help is appreciated.
# 2  
Old 08-13-2008
If your grep groks the -B option,

Code:
grep -B1 Lock-wait file

Or hack your own little awk script:

Code:
awk '/Lock-wait/ { print prev; print } { prev = $0 }' file

# 3  
Old 08-13-2008
you can get the next line...

Below will give you the line with Lock-wait and the next line. have to check to get the previous line.

sed -n '/Lock-wait/{N;p;}' <FILENAME>
# 4  
Old 08-13-2008
You can do the same in sed as in the awk script: remember the previous line, then if there's a match, print the remembered data and the current line.

Code:
sed -n '/Lock-wait/{x;p;x;p;d;};h' file

# 5  
Old 08-14-2008
thank you very much guys...

oops sorry forgot to ask about printing the below line also along with line above the Lock-wait..
# 6  
Old 08-14-2008
Code:
grep -A 1 -B 1 Lock-wait file

# 7  
Old 08-14-2008
Code:
a=$1
b=$2
cat file | sed -n "/$a/{
p
n
p
}
/$b/{
p
n
p
}"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Comparing two one-line files and selecting what does not match

I have two files. One is consisting of one line, with data separated by spaces and each number appearing only once. The other is consisting of one column and multiple lines which can have some numbers appearing more than once. It looks something like this: file 1: 20 700 15 30 file2: 10... (10 Replies)
Discussion started by: maya3
10 Replies

3. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

4. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

5. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

6. Shell Programming and Scripting

Help in selecting line numbers between two values

Hi! I've a file as shown below 1 hello 2 how 5 are 3 you 4 hello 5 world Every statement consists of line numbers at the beginning which are not in sequence. Now I want to select all the line numbers which are present between the line numbers specified by the user.. For example... (1 Reply)
Discussion started by: abk07
1 Replies

7. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

8. UNIX for Dummies Questions & Answers

Selecting specific line using awk

Hi, I would like to get the specific line from the file taking specific coloumn as reference. Thanks and Regards (1 Reply)
Discussion started by: kkarthik_kaja
1 Replies

9. UNIX for Dummies Questions & Answers

Selecting line ahead and next using AWK or SED

:confused: Good Day, I have this script that gets the archive names and the time it applies based on the alert log. The application of archives are of daily basis and usually many so having this script helps my job become easier. My problem is that when i get all the time stamps and... (1 Reply)
Discussion started by: ownins
1 Replies

10. Shell Programming and Scripting

Selecting a line value

Hello, I just wanted to know if there is a way in UNIX to select a line value from a list of words. there is no line number before each word, hence could not use grep. (4 Replies)
Discussion started by: unibboy
4 Replies
Login or Register to Ask a Question