Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined!
    Join Date
    Dec 2008
    Posts
    4

    Unhappy 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?

  2. #2
    Linux 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

  3. #3
    Just 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.

  4. #4
    Linux Enthusiast
    Join Date
    Jul 2005
    Location
    Maryland
    Posts
    521
    Let's say file.log contains:
    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
    The script might be something like this:
    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
    Hope that helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...