How to read each file grep a value from that?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read each file grep a value from that?
# 8  
Old 10-24-2017
Hi,

Below is the content from my file..

Code:
BmmRaiseAlert.sh::308: AlertConfigResult [MatchedOn=033A,Appalert=A60107,Description=eRIC for MWW not processed messages for 5 minutes]
BmmRaiseAlert.sh::320: Calling BuildXHTMLForBmtm.sh with Bmm File [/opt/BMC/BMTM/RFRAlertingIntegration/BmmOutputFiles/BMMEventOutput.1508657897.215.812]
BmmRaiseAlert.sh::323: Call to BuildXHTMLForBmtm.sh complete
BmmRaiseAlert.sh::365: Calling appalert with args [ -l 3 -a MQ -e A60107 -f gbahevpl41.krf.express.RFR -u "http://messagemonitor.krf.express.RFR/BMTM/alerts/BMMEventOutput.1508657897.215.812.html" -m "gbahevpl41.krf.express.RFR:QPAHECL1:QTW1.BOQ.109 ==> eRIC for MWW not processed messages for 5 minutes"]
BmmRaiseAlert.sh::367: Call to appalert completed
BmmRaiseAlert.sh::369: Completed successfully

I want out from each file with file name after grepping the value into a single file.. For example
Code:
Name of first log file ->  line starting with "AlertConfigResult" and starting with "Calling appalert" and "last line"

Code:
BmmRaiseAlert.sh.201710220838.6155.logfile.log ->BmmRaiseAlert.sh::308: AlertConfigResult [MatchedOn=033A,Appalert=A60107,Description=eRIC for MWW not processed messages for 5 minutes]  

Calling appalert with args [ -l 3 -a MQ -e A60107 -f gbahevpl41.krf.express.RFR -u "http://messagemonitor.krf.express.RFR/BMTM/alerts/BMMEventOutput.1508657897.215.812.html" -m "gbahevpl41.krf.express.RFR:QPAHECL1:QTW1.BOQ.109 ==> eRIC for MWW not processed messages for 5 minutes"]


BmmRaiseAlert.sh::369: Completed successfully


Name of second log file ->  line starting with AlertConfigResult and Calling appalert and last line

Name of n log file ->  line starting with AlertConfigResult and Calling appalert and last line

we can ignore first 22 characters like "BmmRaiseAlert.sh::369:"

---------- Post updated at 03:33 AM ---------- Previous update was at 03:22 AM ----------

i tried below

Code:
cat BmmRaiseAlert.sh.201710220838.6155.logfile.log| egrep 'Calling appalert|Completed'

BmmRaiseAlert.sh::365: Calling appalert with args [ -l 3 -a MQ -e A60107 -f gbahevpl41.krf.express.RFR -u "http://messagemonitor.krf.express.RFR/BMTM/alerts/BMMEventOutput.1508657897.215.812.html" -m "gbahevpl41.krf.express.RFR:QPAHECL1:QTW1.BOQ.109 ==> eRIC for MWW not processed messages for 5 minutes"]
BmmRaiseAlert.sh::369: Completed successfully

but i need to read each file and save with file name front of the output..

Example

Code:
BmmRaiseAlert.sh.201710220838.6166.logfile.log

BmmRaiseAlert.sh::365: Calling appalert with args [ -l 3 -a MQ -e A60107 -f gbahevpl41.krf.express.RFR -u "http://messagemonitor.krf.express.RFR/BMTM/alerts/BMMEventOutput.1508657897.215.812.html" -m "gbahevpl41.krf.express.RFR:QPAHECL1:QTW1.BOQ.109 ==> eRIC for MWW not processed messages for 5 minutes"]
BmmRaiseAlert.sh::369: Completed successfully



BmmRaiseAlert.sh.201710220838.6177.logfile.log

BmmRaiseAlert.sh::365: Calling appalert with args [ -l 3 -a MQ -e A60107 -f gbahevpl41.krf.express.RFR -u "http://messagemonitor.krf.express.RFR/BMTM/alerts/BMMEventOutput.1508657897.215.812.html" -m "gbahevpl41.krf.express.RFR:QPAHECL1:QTW1.BOQ.109 ==> eRIC for MWW not processed messages for 5 minutes"]
BmmRaiseAlert.sh::369: Completed successfully

# 9  
Old 10-24-2017
That spec still is quite confusing. How far would
Code:
grep -Ei 'Calling appalert|AlertConfigResult|Completed succ' *.log

get you?
This User Gave Thanks to RudiC For This Post:
# 10  
Old 10-24-2017
Yes, it worked RudiC
# 11  
Old 10-24-2017
As Don stated already, the -i option makes things slower, and is not required here.
The problem was that the (useless) use of cat the file names disappear:
Code:
cat *.log | egrep 'Calling appalert|Completed'

because egrep sees only one input stream.
Simply let egrep open the files, so it sees the file names and even prefixes them by default.
Code:
egrep 'Calling appalert|Completed' *.log

If you want to ensure it prefixes the file name even if there is only one log file, then do as Don suggested
Code:
egrep -H 'Calling appalert|Completed' *.log

or, if your grep -E or egrep does not support the -H option, use the /dev/null trick
Code:
egrep 'Calling appalert|Completed' /dev/null *.log

The trick is that grep sees one more file /dev/null that is empty (so will never match anything in it).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need help for faster file read and grep in big files

I have a very big input file <inputFile1.txt> which has list of mobile no inputFile1.txt 3434343 3434323 0970978 85233 ... around 1 million records i have another file as inputFile2.txt which has some log detail big file inputFile2.txt afjhjdhfkjdhfkd df h8983 3434343 | 3483 | myout1 |... (3 Replies)
Discussion started by: reldb
3 Replies

2. UNIX for Beginners Questions & Answers

Read a file and send mail based on grep

Hi All, I am having a job and I need to send email when the job is running. On any other case (success,fail) I don't needed to send email. I check with BMC they told they dont have that in the version I am using. So I created a dependent job and grepped for the status and sent email. My... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

3. UNIX for Dummies Questions & Answers

Piping grep into awk, read the next line using grep

Hi, I have a number of files containing the information below. """"" Fundallinfo 6.3950 14.9715 14.0482 """"" I would like to grep for Fundallinfo and use it to read the next line? I ideally would like to read the three numbers that follow in the next line and... (2 Replies)
Discussion started by: Paul Moghadam
2 Replies

4. Shell Programming and Scripting

read a file for input and grep in another file

Hi, I'm trying to read a fille into a loop and grep for a string and print the last field of the string in the second file. Then redirect the output to another file but keeping the output in the same order as the original file. I've tried using the following but the ouput from this does not... (3 Replies)
Discussion started by: elmesy
3 Replies

5. Shell Programming and Scripting

read from file and grep using shell

Hi Guys, I have a small script which greps for the username reading from stdinput. ./file.sh pattern pattern=$1 grep "blah blah.*$pattern" /home/user/log.txt Instead of typing the pattern everytime i want to read the pattern from a file inside the shell script and execute the... (5 Replies)
Discussion started by: Irishboy24
5 Replies

6. UNIX for Dummies Questions & Answers

Strange error: grep does not read from file

I have two files file1.txt angie mary susan file2.txt angie blond mary brunnet susan red christine blackI want to get this output angie blond mary brunnet susan redI write grep --file=file1.txt file2.txtand i get no results i also wrote cat file1.txt|while read line... (19 Replies)
Discussion started by: FelipeAd
19 Replies

7. UNIX for Advanced & Expert Users

how to grep/read a file inside compressed tgz without extract?

Hi all, I would like to ask whether in Unix shell/perl have any functions or command to allow grep/cat/read a file inside compressed .tgz without extract it? I know we can tar tvf a compressed tgz but this only allow we read the path/filename contained inside the tarball. If we want to read... (3 Replies)
Discussion started by: mayshy
3 Replies

8. Shell Programming and Scripting

grep vs do while read

I have inherited the UNIX scripting duties for our team and have only been at this for a few months so I apologize for what may be a simple question. We have a script we are reworking. Part of this script takes a parameter and reads a csv file. When it finds a matching line, the cut command is... (4 Replies)
Discussion started by: 15a0
4 Replies

9. Shell Programming and Scripting

read in variable data from another file - grep

Hello! I think this should be an easy solution. I have a large file with many fields of data. The first field has a unique identifier (a subject number) for every record for a chunk of data. Something like this: There were ten experimental conditions (ec), but the ec is identified by only... (11 Replies)
Discussion started by: ccox85
11 Replies

10. Shell Programming and Scripting

Read file then grep the line

Dear all, I am reading a file that has 1 column. While reading I must find the line references from the another file. The following shell doesn't works. Please help #!/bin/bash while read filename; do grep ${filename} fs_full.dat >> unprocfull.dat; done < unproc.dat But when... (2 Replies)
Discussion started by: mr_bold
2 Replies
Login or Register to Ask a Question