Search file pattern using grep command

I 'm writing a script to search particular strings from log files. The log file contains lines start with *. The file may contain many other lines start with *. I need to search a particular line from my log file. The grep command is working in command line , but when i run my script, Its printing all files in that directory . ( Example : script1.shScript2.shMessage CompletedScript4.sh)

Any suggestions?

Ex:

************Message Completed

tail -F file.log  | while read LINE
do
   echo $LINE | awk '{print $0}' | grep "[\*]*.Message Completed*" >>$TestFile
done

Thanks for your help !

Not clear. What exactly is the problem? What's the difference between your command and your script?

The code is not working inside the script.
If i try

grep "[\*]*.Message Completed*"  $Logfile

in the command line its printing the correct line. But if i try the below line inside the script its not working.

echo $LINE | awk '{print $0}' | grep "[\*]*.Message Completed*"

Let me know if u need any other information.

or maybe:

tail -F file.log | awk '/[*].*Message Completed/' > $TestFile

sure about the -F option?

You need to use double quotes around the expansion of variable LINE . Try:

tail -F file.log  | while read LINE
do
   echo "$LINE" | awk '{print $0}' | grep "[\*]*.Message Completed*" >>$TestFile
done

or try shell-only without grep and awk:

tail -F file.log  |
while read LINE
do
  case $LINE in 
    (*"*Message Completed"*) printf "%s\n" "$LINE"
  esac
done >>$TestFile

--
Note that the shell approach has an advantage that it is faster and does not use buffering, which works better in combination with tail -F

1 Like

Scrutinizer,

Thanks a lot for your help !

I tried the below line and it worked .

echo "$LINE" | awk '{print $0}' | grep "[\*]*.Message Completed*" >>$TestFile

I spent many hours and couldn't find the solution. Thank you so much !

The

 | awk '{print $0}'

has no effect. You can omit it.

1 Like

You are welcome..

One more note:

grep "[\*]*.Message Completed*"

should be:

grep "[*]Message Completed[*]"

or

grep "[*].*Message Completed.*[*]"

Depending on what you need to test:

grep "[\*]*.Message Completed*"     # Matches "Message Complete" preceded by an arbitrary character (.) and followed by zero
                                    # or more d's after the sentence. The matching of the literal backslash or the asterisk ([\*])
                                    # has no meaning here, because it is followed by an asterisk, which means zero or more times
                                    # so it will match, whether or not these characters are present
grep "[*]Message Completed[*]"      # Matches "Message Completed" followed by an asterisk directly before and after the sentence
grep "[*].*Message Completed.*[*]"  # Matches "Message Completed" followed by an asterisk somewhere before and after the sentence

grep uses BRE (Basic Regular Expressions) and not Unix Pattern Matching, which use a different syntax

Please learn how to use CODE tags (and not ICODE tags)

1 Like