#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    2
    Rep Power
    0

    Question editing files in a loop


    I am a unix novice, so please forgive me if this sounds silly.

    I have to go through about 350 log files on a Unix system, all in the same directory. I decided to write a small script to go through all of the files in that directory and invoke the "vi" command on them.

    I did something like this:
    find . -type f -print | while read f; do {
    vi $f
    }
    done

    That kinda worked, it does invoke vi on the first file. However, it seems to continue, putting all kinds of commands on the vi command line, and it eventually throws me out of vi on the first file.

    I need to find a way to get the "vi" command to wait until I can look through the file and quit out of vi before it goes to the next file.

    Can anyone help?
    thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Poland
    Posts
    57
    Rep Power
    12
    if you are only reading them (if they'er logs i don't suppose you're doing sth with them) there's some better solution of your problem:
    cat *|less
  4. #3
  5. Introspective
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Nov 2001
    Location
    London, UK
    Posts
    3,317
    Rep Power
    110
    if all you want to do is read them, then less * would do the job fine, but if you want to parse data out of them, then you'll need to script something with sed, grep and awk, or probably more sensibly just write a PERL script. What exactly are you hoping to do with them? just reading log files is an odd thing to do.

    christo
    This is me: http://chris.uk.com
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    2
    Rep Power
    0
    Less may work, then...all i have to do is go through and look for any errors, warnings, check row counts, things like that.

    Thanks!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Poland
    Posts
    57
    Rep Power
    12
    cat *|grep error|less
  10. #6
  11. Introspective
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Nov 2001
    Location
    London, UK
    Posts
    3,317
    Rep Power
    110
    if you can give a detailed example of the data you need, then we can help you to knock up a proper script for the job.. otherwise you'll have to settle with Jabol's cat *|grep error|less. I think he's probably just trying to give you hints tho.

    christo
    This is me: http://chris.uk.com
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    under a rock
    Posts
    54
    Rep Power
    12

    one more idea


    I don't know how big these 350 files are, but even at one line each, your screen will fill several times over. If these are just subsequent versions of the same logfile, then you'll probably not need to know which file contains what error.

    example:
    http.access.20030806-04.log
    http.access.20030806-05.log
    http.access.20030806-06.log
    etc...

    However, if these logs come from many different sources, like

    database.access.20030806.log
    http.access.Aug2003.log
    sudo.3rdquarter.log

    ...then that many lines will be confusing when you see an error.

    If you use grep first, then it'll show you which file contains the found line.

    grep error ./*

    example:
    [15:24:24 zedmelon@kilby bin] grep CMD ./*
    ./pidmail:CMD=${0##*/}
    ./pidmailer: echo "$CMD:
    ./startApache:CMD=${0##*/}
    ./startApache:EXECMD="/opt/apache/current/bin/httpsdctl.cp startssl"
    ./startApache: echo "Run $CMD as root, please.
    ./startApache: echo "$CMD: argument required."
    ./startApache: $CMD 1 | 2 | 3 | 4 | staging | test | all | h
    ./startApache: time, $CMD does no redundancy check."
    ./startApache:ssh $1 $EXECMD
    ./startApache: for COMPARE in $ACCPTCMD
    ./startApache: echo "$CMD: illegal option: $CHECKIT"

    Of course this assumes that the word "error" appears verbatim in each file. If you have a file that contains "Error", a file that contains "ERR", and a file that contains "ERROR," you can use the -i flag, telling grep to ignore case.

    -zedmelon
    Mother says my .sig can beat up your .sig.
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    under a rock
    Posts
    54
    Rep Power
    12

    ERG


    unix_novice, I must have missed your post before about looking for warnings and row counts (is a row count the number of LINES in the file, or info STORED in the file?).

    maybe something like:

    Code:
    FILELIST=`ls -1`
    ERRLIST='error ERROR Warning! ABORT'
    
    for CHECK in $FILELIST
    do
            wc -l $CHECK
    
            for ERR in $ERRLIST
            do
                    grep $ERR $CHECK
            done
    
            echo
    done
    (Thanks to Scorpions4ever: my tabs no longer get nuked)

    I don't know; that seems horribly inefficient, but I'm brainlocked for anything better, unless there are ways to tell the logs from different applications from other logs, just by filename. Then at the expense of getting more complex, your script can spend less time searching ALL files for EVERY possible error, even errors that will only be encountered in other files.

    good luck.
    -zedmelon
    Last edited by zedmelon; August 6th, 2003 at 05:41 PM.
    Mother says my .sig can beat up your .sig.
  16. #9
  17. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,624
    Rep Power
    4247
    >> (wish I knew how to format this better, but my tabs get nuked)
    Post your code within [ code] and [ /code] tabs (remove the leading space within the square brackets).
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  18. #10
  19. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    under a rock
    Posts
    54
    Rep Power
    12

    okay, I'm leaving


    Thanks.

    I'm leaving now, so you won't get to teach me THREE things in one day.
    ;)

    Have a good one.

    -zedmelon
    Mother says my .sig can beat up your .sig.
  20. #11
  21. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    7
    Rep Power
    0

    Cool other ways


    if all the file to be scanned are in the same directory :

    grep -E 'error|ERROR|Warning!|ABORT' *

    or

    ls | xargs -i grep -E 'error|ERROR|Warning!|ABORT' {}

    or

    ls -l | awk '{print$9}' | xargs -i grep -E 'error|ERROR|Warning!|ABORT' {}

    or

    just with a loop as suggested by zedmelon

    :-)

IMN logo majestic logo threadwatch logo seochat tools logo