Grep file and print value only if the following line doesnt contain value xxxx


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep file and print value only if the following line doesnt contain value xxxx
# 1  
Old 01-08-2015
Grep file and print value only if the following line doesnt contain value xxxx

Hi All,
I have some large files which I would like to obtain some data from, I want to grep (or alturnative method) out certain values hut only if the folowing line doesnt contain a certain value. I will explain better below showing an example print of the data.

Code:
NE : V0001
NE : V0002
NE : V0003
NE : V0004
ccc
NE : V00005
ccc
NE : V00006

from the above I want to print every line where NE : is present but only if the line under doesnt contain NE :

I'm sure one of you guys will be able to help.

Thanks again.
# 2  
Old 01-08-2015
Any attempts from your side?
# 3  
Old 01-08-2015
Quote:
Originally Posted by RudiC
Any attempts from your side?
Hi Rudi,
Its something I have never done and was struggling to find a solution, hopefully you or someone on here will be able to help me out.

Thanks in advance.
# 4  
Old 01-08-2015
Like so?
Code:
awk '/^NE :/ {P=$0; next} {print P}' file
NE : V0004
NE : V00005

This User Gave Thanks to RudiC For This Post:
# 5  
Old 01-08-2015
Quote:
Originally Posted by RudiC
Like so?
Code:
awk '/^NE :/ {P=$0; next} {print P}' file
NE : V0004
NE : V00005

Perfect, Thanks Rudi really apprecaite it. Would you mind explaining the command just to help me understand.

Thanks.
# 6  
Old 01-08-2015
The suggestions works if there is always one line without NE :. If there are more lines then P gets printed for every following line, so a provision would need to be made if that could be the case..

Also, technically from the description it follows that the last line should get printed since it does not have a line following it with NE :
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 01-08-2015
awk reads and processes every single line in the input stream. A line starting with NE : is stored in the variable P, overwriting the last one, then that line's processing is abandoned and the next is read/processed. If it doesn't start with NE : P is printed, representing the last line.
BTW, I found a flaw in above: If there's multiple non-NE lines, P is printed several times. This should avoid it:
Code:
awk '/^NE :/ {P=$0; next} P {print P; P=""}' file
NE : V0004
NE : V00005

This User Gave Thanks to RudiC For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep in file and print in the line

hi # cat test.txt Test Date: 20131008 1515 -------------------------------------------------------------------------------------------------------------- Saxx = Proc_m0_s13 : 1640 Saxx = Proc_m0_s15 : 1791 Saxx = Proc_m0_s17 ... (2 Replies)
Discussion started by: justbow
2 Replies

2. Shell Programming and Scripting

Grep or print each section of a file on one line with a separator

I can obtain information from itdt inventory command however it display as below, I'd like to print each entity on one line but seperated by : the file is something like and each section ends with Volume Tag Drive Address 256 Drive State ................... Normal ASC/ASCQ... (3 Replies)
Discussion started by: gefa
3 Replies

3. Shell Programming and Scripting

grep a string and print strings on that line

i have a file like below ABS 12234 A C 12G CFY 23865 A C 34D i want to grep "A C" then print ABS 12G 12234 CFY 34D 23865 Thanks! (4 Replies)
Discussion started by: kingpeejay
4 Replies

4. UNIX for Dummies Questions & Answers

Script or SED command for [[Xxxx Xxxx Xxxx]] to [[Xxxx xxx xx]]

Hi, To comply with a new naming convention on a mediawiki site we have to run a SED or other PERL command to change all instances of ] or ] or ] to ] Can someone please explain how to do this... It has to be done on a mysql dump, so if there is a way to do this in mysql even... (2 Replies)
Discussion started by: lawstudent
2 Replies

5. UNIX for Dummies Questions & Answers

grep N lines after match and then print them on 1 line each

Hello I have a silly question. I need to grep a match in text file and then print 5 lines after it. grep -A 5 .... do it. OK The next thing I can not handle is I need each output to be on 1 line match line2 line3 line4 line5 match line2 line3 line4 line5 etc.. I will really... (10 Replies)
Discussion started by: alekkz
10 Replies

6. Shell Programming and Scripting

grep N lines after match and then print them on 1 line each

Hello I need some help with this job. file.txt ----- cut ---- TARGET 13/11/08 20:43:21 POINT 1 MOVE 8 772102y64312417771 TARGET 13/11/08 21:10:01 POINT 2 MOVE 5 731623jjd12njhd ----- cut ---- this is the example. i need to grep for the word TARGET and print next 4 lines like... (1 Reply)
Discussion started by: alekkz
1 Replies

7. Shell Programming and Scripting

Delete all line breakes from File doesnt work

Hi, I have to delete all line breakes but it doenst work. $ sed -e 's/\\n//g' file.txt and the output contents still line breakes. What should I do? sincerely, Blackbox (3 Replies)
Discussion started by: Blackbox
3 Replies

8. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies

9. Shell Programming and Scripting

print a line containing word in a column using grep

hi, how to print a row which contains a perticular word in its third column using grep, cut, or any thing else. thanks (2 Replies)
Discussion started by: useless79
2 Replies

10. UNIX for Dummies Questions & Answers

rm: Unable to remove directory xxxx/xxxx: File exists

Hi Everyone, I am having problem to delete an "empty" folder ( messages attached ). It displays "total 12" when i typed "ls -lart" on the fnxroot44 folder, but i can't view any file. Is there any way to view those unseen files ? I don't know why option "a" is not working this time. Would... (1 Reply)
Discussion started by: deejay
1 Replies
Login or Register to Ask a Question