Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hi, i have an awk srcipt that runs over a batch of log files. The script is running fine which is good!. However when i run the awk command i ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    3

    Awk and loads of files

    Hi, i have an awk srcipt that runs over a batch of log files. The script is running fine which is good!. However when i run the awk command i have to specify a number of files to be processed - for example i run

    awk -f myawkfile.awk /adirectory/file1 /adirectory2/file2 etc etc

    There are about 20 files that have to be added to the list of files to process and i have to enter these when i want to run the command. I have put them all into a shell script so i don't have to type them in every time but when i want to add or remove one of these files i have to alter the script.

    Is there any way to either:
    1. Tell awk to use a wild card and recursivly search from a directory to find the files. For example process all files that are *.log in any sub directories.
    2. Tell awk to match files based on a regex for the file path.
    3. Get a shell script to get the list of these files and then add them to the awk command.

    Thanks

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    usually, you can just do this:
    Code:
    awk -f myawk.awk /path/*.log

  3. #3
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    You can use find, try this:

    Code:
    find /yourDirname -name *log -exec "awk -f myawkfile.awk" {} \:
    Regards

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by ghostdog74 View Post
    usually, you can just do this:
    Code:
    awk -f myawk.awk /path/*.log
    This doesn't work recursively.

    Regards

  5. #5
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    Quote Originally Posted by Franklin52 View Post
    You can use find, try this:

    Code:
    find /yourDirname -name *log -exec "awk -f myawkfile.awk" {} \:
    Regards
    You're going to want to single-quote the -name parameter value, otherwise you risk expanding it on the command line when it's issued - i.e. '*log' instead of *log.

    Also, that should be a semicolon at the end (not a colon).

  6. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by anomie View Post
    You're going to want to single-quote the -name parameter value, otherwise you risk expanding it on the command line when it's issued - i.e. '*log' instead of *log.

    Also, that should be a semicolon at the end (not a colon).
    Good comments, thanks for the attentiveness.

    Regards

  7. #7
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by Franklin52 View Post
    This doesn't work recursively.

    Regards
    how is this not so?
    Code:
    #more test1.log
    this is first line of test1.log
    second line of test1.log
    last line of test1.log
    #more test2.log
    this is first line of test2.log
    second line of test2.log
    last line of test2.log
    #more test3.log
    this is first line of test3.log
    second line of test3.log
    last line of test3.log
    #ls test[123]
    test1:
    test1.log
    
    test2:
    test2.log
    
    test3:
    test3.log
    #awk '{print "Now in " FILENAME;print}' test[123]/*.log
    Now in test1/test1.log
    this is first line of test1.log
    Now in test1/test1.log
    second line of test1.log
    Now in test1/test1.log
    last line of test1.log
    Now in test2/test2.log
    this is first line of test2.log
    Now in test2/test2.log
    second line of test2.log
    Now in test2/test2.log
    last line of test2.log
    Now in test3/test3.log
    this is first line of test3.log
    Now in test3/test3.log
    second line of test3.log
    Now in test3/test3.log
    last line of test3.log
    seeing that OP has around 20 plus files to process, it might be faster to just use shell's expansion (assuming that OP knows where these log files are created.) rather than find which may take longer if the files are not in the directories and it so happens there are many directories to traverse.

  8. #8
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by ghostdog74 View Post
    how is this not so?
    That means that it only works in the current directory not in the subdirectories of the mentioned path.

    Regards

  9. #9
    Just Joined!
    Join Date
    May 2007
    Posts
    3

    Thanks for the responses

    Its been a help.

    I am interested in knowing more about the 'expansion' mentioned. I have previously tried this and it has not worked for me.

    If i have these directories that contain my filed

    server-15/de/logfile.txt
    server-15/uk/logfile.txt
    server-16/de/logfile.txt
    server-16/uk/logfile.txt

    How can i use expansion in this? I thought that i would be able to somthing like this
    server-[15|16]/[uk|de]/logfile.txt
    But then i get problems and i think its because of the pipes?

  10. #10
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    I think you want something like this:

    Code:
    $ ls server-{15,16}/{uk,de}/logfile.txt
    (Not that you want to use 'ls', but that's an example using expansion.)

    I haven't tested that - you may need to tweak it.

Page 1 of 2 1 2 LastLast

Posting Permissions

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