Print remaining lines using grep

Hi All,

I am having a text file like below

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

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

grep -A2 ERROR test.txt

ERROR - Not a valid
ID : 123

--
ERROR - Not a valid
hello
ID : 124
awk '/ERROR/{err=$0}err && /ID/{printf("%s\nThere is Error for %s\n",err,$0);err="";}' input.txt
1 Like

Thanks a lot is it possible to remove the echo statment which gets printed since the txt file is generated from sh -x execution

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

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

try with this..

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

Hello rohit_shinez,

Could you please try following and let me know if this helps.

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

Output will be as follows.

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

Thanks ravinder,

But i am not getting the relavent output my text file

ERROR - Not a valid
ID : 123


ERROR - Not a valid
hello
+ echo ID : 124

SUCCESS - Valid
ID : 12

output with your snippet


There is Error for ID 123

There is Error for ID 12

change $1 to $0

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

Yeah got the sorted, thanks guys is there a way to add multiple pattern search of string

ERROR - Not a valid
ID : 123


ERROR - Not a valid
hello
+ echo ID : 124

SUCCESS - Valid
ID : 12

WARNING

IGNORE ERROR

Desired output

There is Error for ID 123

There is Error for ID 12

WARNING

Tried below
awk '($0 ~ /ERROR|WARNING/){Q=$0;next} ($1 ~ /ID/){print Q ORS "There is Error for ID " $NF}' test.txt

i need to exlcude IGNORE ERROR

awk '($0 ~ /ERROR/ && $0!~/IGNORE/){Q=$0;next} ($0 ~ /ID/){print Q ORS "There is Error for ID " $NF}' test.txt
1 Like

Perfect thanks guys

Hello rohit_shinez,

Could you please try following and let me know if this helps you.

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

Also as per your output shown, it will make sure first string ERROR should be there before any ID is coming then only it will print the ID. If you have any further requirements then kindly mention them too.

Thanks,
R. Singh

1 Like