0

I have an awk script which is called something like this:

awk -f d.awk /var/log/app*.log 

And this works fine. Log file path is constant and not changing (only the number of files changes in this location due to log rotation) so I want to remove it from command line and hard code inside the awk script. Is there a way to skip passing this argument from command line and hard code it within the awk script and still achieve the same result?

I read about getline but it is not working for me. awk script outline is something like this:

BEGIN{
    #Initialization of few variable
}
match() {
    #Main process logic
    # Collect the output
    output=output" " result_after_processing
}
END{
    #Write the output to output file
    print output >> some_output_file
}
6
  • 1
    Why? Seems to me you want to work the other direction. Instead of awk -f, write a shell script which embeds the awk and uses /var/log/app*.log as arguments. Commented Jun 12, 2020 at 19:03
  • @WilliamPursell thanks for your reply. I thought of that but I need to call this awk from an existing C code and don't want to (or allowed to) add another layer in between. Problem is existing C code is very bad in parsing wild card (*) and producing some weird results. I don't have access to existing C code. Hence thought of getting rid of this argument. Commented Jun 12, 2020 at 19:06
  • The log file path is processed by the shell to give a list of filenames for awk to work on, you can't control that with awk Commented Jun 12, 2020 at 19:07
  • 1
    You can use getline < "filename" to read file a file, but it won't expand wildcards, and I don't think there's a way to reassign awk's input to another file in general. Commented Jun 12, 2020 at 19:10
  • 1
    @Inian, yes, you can, but it's ugly - you can populate awk's ARGC/ARGV based on your own scanning of a dir for files. Commented Jun 12, 2020 at 19:11

1 Answer 1

5
BEGIN {
    cmd = "printf \047%s\n\047 /var/log/app*.log"
    while ( (cmd | getline line) > 0 ) {
        ARGV[ARGC++] = line
    }
    close(cmd)
}

should work as long as your file names don't contain newlines.

This is one of those rare cases where use of getline is appropriate - make sure to read http://awk.freeshell.org/AllAboutGetline if you're ever considering using it in future.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.