Search file pattern using grep command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search file pattern using grep command
# 1  
Old 03-01-2016
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**********
Code:
tail -F file.log  | while read LINE
do
   echo $LINE | awk '{print $0}' | grep "[\*]*.Message Completed*" >>$TestFile
done



Thanks for your help !

Last edited by zaxxon; 03-01-2016 at 11:50 AM.. Reason: code tags, not icode
# 2  
Old 03-01-2016
Not clear. What exactly is the problem? What's the difference between your command and your script?
# 3  
Old 03-01-2016
The code is not working inside the script.
If i try
Code:
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.

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


Let me know if u need any other information.

Last edited by Scrutinizer; 03-01-2016 at 03:25 PM.. Reason: CODE tags
# 4  
Old 03-01-2016
or maybe:
Code:
tail -F file.log | awk '/[*].*Message Completed/' > $TestFile

sure about the -F option?
# 5  
Old 03-01-2016
You need to use double quotes around the expansion of variable LINE. Try:
Code:
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:

Code:
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

Last edited by Scrutinizer; 03-01-2016 at 10:43 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 03-01-2016
Scrutinizer,

Thanks a lot for your help !

I tried the below line and it worked .
Code:
echo "$LINE" | awk '{print $0}' | grep "[\*]*.Message Completed*" >>$TestFile

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

Last edited by Scrutinizer; 03-01-2016 at 10:38 PM.. Reason: ICODE tags -> CODE tags
# 7  
Old 03-01-2016
The
Code:
 | awk '{print $0}'

has no effect. You can omit it.
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

Grep command to search pattern corresponding to input from user

One more question: I want to grep "COS_12_TM_4 pattern from a file look likes : "COS_12_TM_4" " ];I am taking scan_out as the input from the user. How to search "COS_12_TM_4" in the file which is corresponds to scan_out (12 Replies)
Discussion started by: Preeti Chandra
12 Replies

3. Shell Programming and Scripting

How can I use find command to search string/pattern in a file recursively?

Hi, How can I use find command to search string/pattern in a file recursively? What I tried: find . -type f -exec cat {} | grep "make" \; Output: grep: find: ;: No such file or directory missing argument to `-exec' And this: find . -type f -exec cat {} \; -exec grep "make" {} \;... (12 Replies)
Discussion started by: cola
12 Replies

4. Shell Programming and Scripting

Grep command is not search the complete pattern

I am facing a problem while using the grep command in shell script. Actually I have one file (PCF_STARHUB_20130625_1) which contain below records. SH_5.55916.00.00.100029_20130601_0001_NUC.csv.gz|438|3556691115 SH_5.55916.00.00.100029_20130601_0001_Summary.csv.gz|275|3919504621 ... (2 Replies)
Discussion started by: sumit.vedi1988
2 Replies

5. Shell Programming and Scripting

Pattern search using grep command !

Hi, I am trying to do pattern search using grep command. But i donot know what mistake i'm doing. I am not getting the expected Result. could any one please help me out? $ cat b.ksh AasdjfhB 57834B 86234B 472346B I want to print the line which is starting with either A or 8 and... (10 Replies)
Discussion started by: nikesh29
10 Replies

6. Shell Programming and Scripting

Remove data from grep command using the pattern in the file

Hi, I writing a shell program to remove the data from output of the find which matches a list in a file I am using the below find command to get the list of files x=`find . -name test*.dat` the output of the find command is as follows test1.dat test2.dat test3.dat test4.dat... (4 Replies)
Discussion started by: pals70423
4 Replies

7. Shell Programming and Scripting

Conditional grep command to search entire file

Let me give you a complete example what I am trying to achieve. 1. Below is the log file structure where I need 2,5 and 14th column of the logs after grepping through the linkId=1ddoic. Log file structure:- abc.com 20120829001415 127.0.0.1 app none11111 sas 0 0 N clk Mozilla/5.0... (3 Replies)
Discussion started by: kmajumder
3 Replies

8. Shell Programming and Scripting

search for a pattern using grep

Hi I am facing the below problem. I have set of lines in which i have to search for only the line which matches with the pattren "/" only. input:- /*+ some text */ /*+ some text */ /* Remove rows from a table of survey results. */ /* Add a survey respondent's name and answers. */ /*... (7 Replies)
Discussion started by: manasa_vs
7 Replies

9. Shell Programming and Scripting

Help to search multiple pattern in file with grep/sed/awk

Hello All, I have a file which is having below type of data, Jul 19 2011 | 123456 Jul 19 2011 | 123456 Jul 20 2011 | 123456 Jul 20 2011 | 123456 Here I wanted to grep for date pattern as below, so that it should only grep "Jul 20" OR "Jul ... (9 Replies)
Discussion started by: gr8_usk
9 Replies

10. Shell Programming and Scripting

fetch last line no form file which is match with specific pattern by grep command

Hi i have a file which have a pattern like this Nov 10 session closed Nov 10 Nov 9 08:14:27 EST5EDT 2010 on tty . Nov 10 Oct 19 02:14:21 EST5EDT 2010 on pts/tk . Nov 10 afrtetryytr Nov 10 session closed Nov 10 Nov 10 03:21:04 EST5EDT 2010 Dec 8 Nov 10 05:03:02 EST5EDT 2010 ... (13 Replies)
Discussion started by: Himanshu_soni
13 Replies
Login or Register to Ask a Question