Need more efficient log file grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need more efficient log file grep
# 1  
Old 05-03-2006
Network Need more efficient log file grep

I'm writing a script that at one point needs to check the contents of another script's log file to determine how to proceed. An example record from the log file is:

"mcref04152006","060417","ANTH0415","282","272","476,983.37","465,268.44","loaded"

I want my script to return this record if:

1.) the record contains "mcref", which is declared earlier in the script as $reffilepattern, and
2.) the record contains "060417", which is declared earlier in the script as $todayyymmdd.

I don't need to be picky about which fields contain the data, because the format is consistent. I'm presently doing this to read the entire line into the variable $loadedref:

Code:
todayyymmdd=$(date +%y%m%d)
reflog=reflogfile.txt
reffilepattern="mcref"
...
loadedref=$(grep "\"$todayyymmdd\"" $reflog | grep $reffilepattern)

Obviously, this works just fine, except that piping the results of one grep into another grep seems unholy. The log file is small, so while efficiency is not a concern at this point, it will be eventually as the log file grows. I tried to combine this into one grep that says, "give me the record that contains both "mcref" (without quotes) AND "060417" (with quotes), with any number of characters in between (including none)." I've tried goofy stuff like:

Code:
grep ${reffilepattern}[A-Za-z0-9]*\"${todayyymmdd}\" $reflog

...which doesn't work. Can anyone give me some pointers here? I know I'm missing something really simple -- it's late in the day! Thanks in advance.
# 2  
Old 05-03-2006
I think what you have is fine. If your worried about performace then I suggest going to PERL. Perl is much faster and better at parsing files. It's what perl was meant for.

I was going to write the script but I got tired and I'm going home. I'll check back tomorrow.

-X
# 3  
Old 05-03-2006
Would this work for you:

grep "${reffilepattern}.*${todayyymmdd}" $reflog

I tried on a test file with your data, seemed to work, let us know...
# 4  
Old 05-04-2006
Interesting -- grep "${reffilepattern}.*${todayyymmdd}" $reflog works (I can't believe I didn't think of that), but on a larger test file (about 60MB) it is actually 2 to 3 times slower than the original method of piping one grep through another. I'm guessing because the first grep narrows the records down for the second grep. I guess I'll just keep my original method. Thanks for the help, though. This is good to know!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Efficient way to search array in text file by awk

I have one array SPLNO with approx 10k numbers.Now i want to search the subscriber number from MDN.TXT file (containing approx 1.5 lac record)from the array.if subscriber number found in array it will perform below operation.my issue is that it's taking more time because for one number it's search... (6 Replies)
Discussion started by: siramitsharma
6 Replies

2. Shell Programming and Scripting

Monitor log entries in log files with no Date format? - Efficient logcheck?

is there a way to efficiently monitor logfiles that do not have a date or time format? i have several logs on several different servers that need to be monitored. but i realized writing a script for this would be very complex and time consuming giving the variety of things i need to check for i.e.... (2 Replies)
Discussion started by: SkySmart
2 Replies

3. Shell Programming and Scripting

Efficient method of determining if a string is in a file.

Hi, I was hoping someone could suggest an alternative to code I currently have as mine takes up far too much processor time and it to slow. The situation: I have a programme that runs on some files just before they are zipped up and archived, the program appends a one line summary of the... (4 Replies)
Discussion started by: RECrerar
4 Replies

4. Shell Programming and Scripting

Efficient population of array from text file

Hi, I am trying to populate an array with data from a text file. I have a working method using awk but it is too slow and inefficent. See below. The text file has 70,000 lines. As awk is a line editor it reads each line of the file until it gets to the required line and then processes it.... (3 Replies)
Discussion started by: carlr
3 Replies

5. Homework & Coursework Questions

Efficient Text File Writing

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a template main.c file via shell script to make it easier for yourself later. The issue here isn't writing... (2 Replies)
Discussion started by: george3isme
2 Replies

6. UNIX for Dummies Questions & Answers

Efficient way of extracting data from file

I am having a file, around 500 lines. which contains one letter words, two letters words,...and so on(up to 15 letter words and words are not seprated by line). I need to compare all 1 letter words with 3,4,5 and 6 letters word, all 2 letters words with 2,3,4 and 5 letters words and all 3 letters... (3 Replies)
Discussion started by: akhay_ms
3 Replies

7. UNIX for Advanced & Expert Users

Efficient way to grep

Hi Experts, I've been trying simple grep to search for a string in a huge number of files in a directory. grep <pattern> * this gives the search results as well as the following - grep: <filename>: Permission denied grep: <filename>: Permission denied for files which I don't have... (4 Replies)
Discussion started by: sumoka
4 Replies

8. Shell Programming and Scripting

File transformation - what is most efficient method

I've done quite a bit of searching on this but cannot seem to find exactly what I'm looking for. Say I have a | delimited input file with 6 columns and I need to change the value of a few columns and create an output file. With my limited knowledge I can do this with many lines of code but want... (5 Replies)
Discussion started by: 1superdork
5 Replies

9. UNIX for Dummies Questions & Answers

efficient raid file server

I need to put together a RAID1 file server for use by Windoze systems. I've built zillions of windows systems from components. I was a HPUX SE for a long time at HP, but have been out of the game for years. I've got an old workhorse mobo FIC PA-2013 with a 450 MHz K6 III+ I could use, but I'd... (2 Replies)
Discussion started by: pcmacd
2 Replies

10. Shell Programming and Scripting

Efficient way to grep multiple strings

I have a script which searches a huge log file for the existence of a specified string and if the string is not present i receive an alert mail. Here's an extract: STRING=$(grep 'warning' logfile | tail -1 | wc -l) if (( ${STRING} > 0 )); then print -- "---- Warning etc.... (3 Replies)
Discussion started by: Moxy
3 Replies
Login or Register to Ask a Question