Find the answer to your Linux question:
Results 1 to 10 of 10
I need help to write a simple bash script. In a directory, there are 6 log files. Each file has a name and date, for example file1.Sep0918, file2.Sep0918, etc. For ...
  1. #1
    Just Joined!
    Join Date
    Sep 2008
    Posts
    3

    Help with Bash Script Please!!

    I need help to write a simple bash script. In a directory, there are 6 log files. Each file has a name and date, for example file1.Sep0918, file2.Sep0918, etc. For each file, I have to search for a key word, lets say the word "Error". I need to count the occurances of that word in each file and print them out in to file. The directory path should be an argument past to the script. The file output file should cut out the date from the filename and should resemble something like:

    54 errors in file1
    12 errors in file2
    .
    .
    .

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    This sounds suspiciously like a homework assignment, which are not allowed per The Forum Rules.

    I suggest that you read a few guides on Bash scripting:

    Bash Guide for Beginners
    Advanced Bash-Scripting Guide
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Sep 2008
    Posts
    3
    This is actually not a homework assignment. I am a beginner program and am trying to learn scripting. I would appreciate any help. I did not ask for exact code, even a description would be helpful.

    Thanks

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    to search for one file
    Code:
    egrep -o Error file | wc -l
    i leave it to you to search the rest

  5. #5
    Just Joined!
    Join Date
    Sep 2008
    Posts
    3
    If i pass the script an argument thats the directory path, how do I get it to read each file on the path? Thats my biggest issue. There are several files in the directory, so to read each one, I thought using a for loop would work. So far, i've thought of...

    for file in $1
    do
    <parse each file>
    done

    but all that does is make $file store the directory path.

  6. #6
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by youngmoney07 View Post
    If i pass the script an argument thats the directory path, how do I get it to read each file on the path? Thats my biggest issue. There are several files in the directory, so to read each one, I thought using a for loop would work. So far, i've thought of...

    for file in $1
    do
    <parse each file>
    done

    but all that does is make $file store the directory path.
    You'd rather do

    Code:
    for file in "$1"/*
    Provided that $1 will hold the path.

    If you need to process subdirs as well, you can do a recursive script, but I advise to use the command find instead. Read on the -exec option for find.

    For more versatility, you could use this as well:

    Code:
    find /path/to/ -type f | while read file
    do
      #whatever
    done

  7. #7
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    or
    Code:
    for file in $(find /path/to -type f)
    do
        # whatever
    done

  8. #8
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by scm View Post
    or
    Code:
    for file in $(find /path/to -type f)
    do
        # whatever
    done
    files with spaces may be broken with this method. its better to use while read loop

  9. #9
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    That method can work with a tiny modification:

    Code:
    IFS=$'\012'
    
    for file in $(find /path/to -type f)
    do
        # whatever
    done
    This will tell the loop to only split on newlines, preventing spaces from messing with things. IFS stands for "Internal Field Separator".
    DISTRO=Arch
    Registered Linux User #388732

  10. #10
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by Cabhan View Post
    That method can work with a tiny modification:

    Code:
    IFS=$'\012'
    
    for file in $(find /path/to -type f)
    do
        # whatever
    done
    This will tell the loop to only split on newlines, preventing spaces from messing with things. IFS stands for "Internal Field Separator".
    true. However, a while read loop enable one to work with fields as you work each line. IMO, it is more "flexible".

Posting Permissions

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