I have a log file in which I need to check if there are any error messages, if there are, extract the error message to another file.

Like this I may have error file for the different file have different log patterns. So how can I search matching pattern using a variable?

As I want to create one generic .sh file for all log files. Suppose I have one file in which I want to search to pattern and print in another file $pat1="ERROR MESSAGE" $pat2="RAISE_ERROR_SEEN". I want all lines from file b/w these two patterns.

Using variable only.


Try this,

 awk "/""$pat1""/,/""$pat2""/" test.log | grep -v "$pat3" > Error.bk
  • will print the context between $pat1 and $pat2
  • Since we have space in the first variable, use double quotes twice.

in a more simple way:

grep -e "$pat1\|$pat2" filename > resultfile
  • being careful with any regular-expression special characters in $pat – Jeff Schaller Aug 12 '18 at 13:57
  • you are right but I only considered his concern in his question. – Hossein Vatani Aug 12 '18 at 18:02
  • Hi, Actually I want to work on awk only as I need to add more filters code and logic to process all log files. – Vipin Sahu Aug 14 '18 at 8:47
  • OK!then please complete your question. – Hossein Vatani Aug 14 '18 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.