Print remaining lines using grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print remaining lines using grep
# 1  
Old 12-07-2016
Print remaining lines using grep

Hi All,

I am having a text file like below

Code:
ERROR - Not a valid
ID : 123


ERROR - Not a valid
hello
ID : 124

SUCCESS - Valid
ID : 12

I need to display like below after reading the file if it finds the error keyword
along with displaying this first line when error pattern is matched

Code:
ERROR - Not a valid
There is Error for ID 123
ERROR - Not a valid
There is Error for ID 124

I have tried something like this but need to print only first line of pattern match and display which ID. There will be always ID printed in text file
Code:
grep -A2 ERROR test.txt

ERROR - Not a valid
ID : 123

--
ERROR - Not a valid
hello
ID : 124

# 2  
Old 12-08-2016
Code:
awk '/ERROR/{err=$0}err && /ID/{printf("%s\nThere is Error for %s\n",err,$0);err="";}' input.txt

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 12-08-2016
Thanks a lot is it possible to remove the echo statment which gets printed since the txt file is generated from sh -x execution

Code:
ERROR - Not a valid
ID : 123


ERROR - Not a valid
hello
+ echo ID : 124

SUCCESS - Valid
ID : 12

If i try this i am not getting the output

Code:
awk '/ERROR/!/echo/{err=$0}err && /ID/{printf("%s\nThere is Error for %s\n",err,$0);err="";}' test.txt

# 4  
Old 12-08-2016
try with this..

Code:
awk '/ERROR/{err=$0}err && /ID/{printf("%s\nThere is Error for %s : %s\n",err,$(NF-2),$NF);err="";}' input.txt

This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 12-08-2016
Hello rohit_shinez,

Could you please try following and let me know if this helps.
Code:
awk '($1 ~ /ERROR/){Q=$0;next} ($1 ~ /ID/){print Q ORS "There is Error for ID " $NF}'   Input_file

Output will be as follows.
Code:
ERROR - Not a valid
There is Error for ID 123
ERROR - Not a valid
There is Error for ID 124
ERROR - Not a valid
There is Error for ID 12

Thanks,
R. Singh
# 6  
Old 12-09-2016
Thanks ravinder,

But i am not getting the relavent output my text file
Code:
ERROR - Not a valid
ID : 123


ERROR - Not a valid
hello
+ echo ID : 124

SUCCESS - Valid
ID : 12

output with your snippet
Code:
There is Error for ID 123

There is Error for ID 12

# 7  
Old 12-09-2016
change $1 to $0

Code:
awk '($1 ~ /ERROR/){Q=$0;next} ($0 ~ /ID/){print Q ORS "There is Error for ID " $NF}'   Input_file

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

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: duplicate column and print remaining as is

Hello there I'd like to make a copy of 2nd column and have it printed in place of column 1. Remaining columns are needed as it. test data: ProbeSet GeneSymbol X22565285 X22566285 ILMN_1050008 MYOCD 6.577 7.395 ILMN_1050014 GPRC6A 6.595 6.668 ILMN_1050017 ... (2 Replies)
Discussion started by: genome
2 Replies

2. Shell Programming and Scripting

How to print N number of lines before and after the grep?

Hi , My record file , need to print up to above (DATA array)(there may be n no lines ) , grep "myvalue" row now .....suggest me some options --- DATA Array--- record type xxxxx sequence type yyyyy 2 3---> data1 /dev/ --- DEVICE --- MAXIMUM_People= data_blocks= MY_value=2 xyz abc ... (0 Replies)
Discussion started by: Huvan
0 Replies

3. Shell Programming and Scripting

Print lines before and after..not grep -A

Hi I have this in my file 2011-04-18 15:32:11 system-alert-00012: UDP flood! From xxxxxx to yyyyyyyyyy, int ethernet0/2). Occurred 1 times. 2011-04-18 15:32:11 system-alert-00012: UDP flood! From xxxxxx to yyyyyyyyyy, int ethernet0/2). Occurred 1 times. 2011-04-18 15:32:11... (9 Replies)
Discussion started by: zorrox
9 Replies

4. Shell Programming and Scripting

Print the above and below lines for the grep pattern.

Hi, i would like to get the above and below lines of the grep pattern . For ex : file as below: chk1- aaaa 1-Nov chk2 -aaaa ########## chk1-bbbbbb 1-Nov chk2-bbbbbb ######### my search pattern is date : 1-Nov i need the o/p as below chk1- aaaa 1-Nov (6 Replies)
Discussion started by: expert
6 Replies

5. Shell Programming and Scripting

Print lines between two lines after grep for a text string

I have several very large file that are extracts from Oracle tables. These files are formatted in XML type syntax with multiple entries like: <ROW> some information more information </ROW> I want to grep for some words, then print all lines between <ROW> AND </ROW>. Can this be done with AWK?... (7 Replies)
Discussion started by: jbruce
7 Replies

6. Shell Programming and Scripting

AIX equivalent to GNU grep's -B and -A [print lines after or before matching lines]

Hi folks I am not allowed to install GNU grep on AIX. Here my code excerpt: grep_fatal () { /usr/sfw/bin/gegrep -B4 -A2 "FATAL|QUEUE|SIGHUP" } Howto the same on AIX based machine? from manual GNU grep ‘--after-context=num’ Print num lines of trailing context after... (4 Replies)
Discussion started by: slashdotweenie
4 Replies

7. UNIX for Dummies Questions & Answers

print remaining part from the first-match within a file

Hi, i was looking for unix command(s) for : find the first occurrence of a given pattern with in a file and print the remaining part. below is an example of what i am looking for: lets say, a file named myfile.txt now, the command i am looking for will do the following (4 Replies)
Discussion started by: nurulamin862
4 Replies

8. Shell Programming and Scripting

Print lines after grep

Hi all, I need help in following scenario. I have a file with about 10,000 lines. There are several lines which have word "START" (all upper case) in them. I want to grep line with word "START" and then do the following 1. Print the line number having word "START" 2. Print the next 11 lines. ... (4 Replies)
Discussion started by: jakSun8
4 Replies

9. Shell Programming and Scripting

print remaining file after a condition is satisfied

Hi , could any one suggest me that how to determine if the first field is numeric and if it is greater than another number then from that point everything else should be printed using awk. I have tried this : awk -v xxxx=$xxxxx ' BEGIN { enable=0 } { print $1 if ( ( $1 !~ "^*$" ... (5 Replies)
Discussion started by: hitmansilentass
5 Replies
Login or Register to Ask a Question