Results 1 to 4 of 4
Hey, I have a question and I really don't know how to approach it...
I have text files which contain logs of certain events. Within these files I would like ...
- 12-05-2008 #1Just Joined!
- Join Date
- Dec 2008
- Posts
- 4
Help, possible script?
Hey, I have a question and I really don't know how to approach it...
I have text files which contain logs of certain events. Within these files I would like to group like events (such as ADD, UPDATE, DELETE or whatever it might be) and output these to respective text files. Every event has a time stamp which I would like to keep and output into my text files. Does anyone know what I could do to achieve this? I am new to running commands via the terminal and don't have the most extensive knowledge of commands to run. I do know however understand that I will need to use piping to achieve what I want. I am running CentOS and using BASH. Can anyone help?
- 12-05-2008 #2Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
You can do this with sed, awk or perl, for example. In sed, you could use something like this:
Code:sed '/pattern1/wFile1 /pattern2/wFile2 ....' inputfile
- 12-05-2008 #3Just Joined!
- Join Date
- Dec 2008
- Posts
- 4
If I was to do this however, would I have to predetermine the patterns? Here is maybe a clearer representation of I want to achieve..
I want my command (or script, whatever) to...
1. Open the log file
2. Search document for potential patterns
3. If no like patterns exist, exit and leave log file as is
4. If like patterns exist, output grouped patterns to pattern named files in a new directory.
Like patterns exist if there are matching word(s). For example in the case of my previous example, if it is seen that there are two lines that contain UPDATE then they contain the like pattern of UPDATE.
- 12-06-2008 #4Linux Enthusiast
- Join Date
- Jul 2005
- Location
- Maryland
- Posts
- 521
Let's say file.log contains:
The script might be something like this:Code:ADD, UPDATE, DELETE or whatever whatever ADD, UPDATE or DELETE DELETE UPDATE or whatever whatever whatever pattarn patternnnnn.nn,-- more stuff pattern apattern bbpattern bbbpattern another bbbpattern pattern
Hope that helps.Code:#!/bin/bash echo -n "what to search:" read pattern if [ -z "`grep $pattern file.log`" ]; then echo Nothing found. Exiting...; exit 0 else if [ ! -d found-patterns ]; then mkdir found-patterns; fi grep "$pattern" file.log| sort -u >> .tmp-file; for i in `cat .tmp-file`; do if [ ! -d found-patterns/$i ]; then mkdir found-patterns/$i; fi echo "`grep "$i" file.log`" > found-patterns/$i/$i.txt done rm -f .tmp-file echo "Check found-patterns directory" fi


Reply With Quote