0

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.

slm
  • 363,520
  • 117
  • 767
  • 871
  • Possibly related: [Pass shell variable as a /pattern/ to awk](https://unix.stackexchange.com/questions/120788/pass-shell-variable-as-a-pattern-to-awk) – steeldriver Aug 12 '18 at 13:16
  • I tried to improve this but I still don't understand what you're asking? – slm Aug 12 '18 at 13:21

2 Answers2

0

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.
Siva
  • 9,017
  • 8
  • 56
  • 86
0

in a more simple way:

grep -e "$pat1\|$pat2" filename > resultfile
Hossein Vatani
  • 726
  • 4
  • 9