Displaying the Second Line of the Grep Search Results


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Displaying the Second Line of the Grep Search Results
# 1  
Old 08-03-2010
Displaying the Second Line of the Grep Search Results

Hi
I really hope someone can help with the below question.
Lets say that I have a file called output.txt and I want to display all of the lines which contain the word ‘disconnect'. I know that this can easily be obtained by using the following command:
grep -i disconnect output.txt

However, the next task is a bit trickier. Lets say that I don't want to print the line with the keyword ‘disconnect' but rather I am interested in displaying the line immediately below the that line. The line below the line which has the word ‘disconnect' could contain anything, such as an IP address or a string. How would I achieve that? For instance, the second line would be displayed in the below example:
Error, disconnect from server
The following machine as affected: IP 101.256.33.44

Many Thanks
# 2  
Old 08-03-2010
Code:
awk '/disconnect/ { getline; print $0;}' <inputfile>


Last edited by Scott; 08-03-2010 at 07:36 AM.. Reason: Added code tags
# 3  
Old 08-03-2010
Like this?
Code:
connect
connect
disconnect
that one
connect
disconnect
that one too
connect
$> awk '/^disco/ {getline; print}' infile
that one
that one too

# 4  
Old 08-03-2010
Hi .. if u have "disconnect" in consecutive lines .... the first alone printing not the second line.

Example : a file has three line
--------------

Error disconnect :
Error disconnect 1
Error in 1 line:

Here the second line "Error disconnect 1" is printing not the "Error in 1 line:"
# 5  
Old 08-03-2010
@girija:
It depends on where in the consecutive lines the string "disconnect" is placed so you could maybe check by this if it should be printed or not. But to make sure:

@Sunny Sid:
To reduce the chance of errors and guessing around please post an excerpt of your input file and the expected output, ty. Please use code tags when doing so.
# 6  
Old 08-03-2010
I think it would be helpful.

Code:
bash-3.00$ cat sample.txt
connect
connect
disconnect
that one
connect
disconnect
that one too
connect
disconnect
disconnect
connect
connect
disconnect
that one too
bash-3.00$

Code:
 grep -A 1 "disconnect" sample.txt | grep -v "disconnect"

# 7  
Old 08-04-2010
Hi
Thanks for all of the replies. The input file would not contain the keyword 'disconnect' on two consecutive lines. Additionally, 'disconnect' may not necessarily appear at the start of a line. I just want the line below the line containing 'disconnect' to appear. Here is a sample from the input file:
connect
connect
disconnect
that one
connect
disconnect
that one too
connect

... and this would be the output:
that one
that one too


For the above situation would both the following commands achieve the same result:
awk '/disconnect/ { getline; print $0;}' <inputfile>
grep -A 1 "disconnect" sample.txt | grep -v "disconnect"
If so then I would be more keen on using grep | grep to do the job because I am still quite unfamilar with awk.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

Search Results (Search, New, and Today's Topics) Animation Switch

Hey, I added an animation switch on the search results page; so by default the thread previews are off, but if you want to look at them, just click on the green button and the thread previews will turn on (and back off). See image and attached animation: ... (1 Reply)
Discussion started by: Neo
1 Replies

2. Shell Programming and Scripting

Multiple Results of Grep in one Line/String?

Hello, I have a Textfile sees like this "Word1":aksdfjaksdf "Word2":askdfjalsdkfdlsjfasldfj "This is Word3":asdfkjalskdfj what i need is a string which sees like this Word1;Word2;This is Word3 Conclusion always the text within "" which is before the : i tried it with grep.... (10 Replies)
Discussion started by: SwordMaster
10 Replies

3. Shell Programming and Scripting

Comparing fields of two files and displaying results

Hello , I am trying to compare two files i.e one master file and the other exclusion file. If the second field of masterfile is oracle8 then I need to compare the 3rd field of master file with the 1st field of all the rows of exclusion file else I need to compare 2nd field from master file with... (2 Replies)
Discussion started by: rahul2662
2 Replies

4. Shell Programming and Scripting

Can ctag and cscope support recording search results and displaying the history results ?

Hello , When using vim, can ctag and cscope support recording search results and displaying the history results ? Once I jump to one tag, I can use :tnext to jump to next tag, but how can I display the preview search result? (0 Replies)
Discussion started by: 915086731
0 Replies

5. Shell Programming and Scripting

String: Grep / SED for multy line search

Hi, At first I want to please you to provide the solution with grep/sed if possible. :cool: File looks like: wished result: so I want in a new file BLUE@@RED string from first line like: grep "/folder_start" cs_src > tmp1 string from second line: grep "/main" cs_src... (14 Replies)
Discussion started by: unknown7
14 Replies

6. UNIX for Dummies Questions & Answers

grep line pattern search

Hello everyone, I have been trying to get a list of all files containing a line of this type: };#followed by anything with any spaces (0 or more or 0 or more tabs) before the } and between each of the characters. I have been trying this : grep '*}*;*#*' *.c but I have not been fully... (1 Reply)
Discussion started by: gio001
1 Replies

7. Shell Programming and Scripting

Search for Files and clear line after results

Hi, this is a little strange, i have the following code: if then echo -e "psa/admin/sbin present " which shows if a directory is present, but what I would like it to do is show the line and then remove its self and show the rest of the script... no idea what to look... (1 Reply)
Discussion started by: foz
1 Replies

8. UNIX for Dummies Questions & Answers

Multiple Line search using grep

Hi All, I am trying to search multiple lines in file using grep /sed.And i cant seem to make it work. The File looks like this 5012001,100,AUTOBATCH,FEE,DAILYFEE,0,0 4241 SERVICE DENIED 5012002,100,AUTOBATCH,FEE,DAILYFEE,0,0 4241 SERVICE DENIED... (6 Replies)
Discussion started by: pistachio
6 Replies

9. Shell Programming and Scripting

search whole line using grep

hi, how to search whole line using grep in a file. (1 Reply)
Discussion started by: useless79
1 Replies

10. Shell Programming and Scripting

diffrent results between command line and scripted grep

When I type a command at the command line it supplies one result and the exact same command in a script egrep '^01|^02|^03|^04' file > fileout count = 29353 same count in the script yields a count of 23492 is there any reason this could be happening. (1 Reply)
Discussion started by: r1500
1 Replies
Login or Register to Ask a Question