Find the answer to your Linux question:
Results 1 to 5 of 5
I'm writing a script that takes 1 argument and searches the standard input stream for it. So my script is getRecords.awk #!/bin/awk -f BEGIN { search = ARGV[3] print(search) delete ...
  1. #1
    Just Joined!
    Join Date
    Oct 2009
    Posts
    10

    awk script taking arguments from command line

    I'm writing a script that takes 1 argument and searches the standard input stream for it.

    So my script is getRecords.awk

    #!/bin/awk -f
    BEGIN {
    search = ARGV[3]
    print(search)
    delete ARGV[3]
    }
    /ARGV[3]/{print $0}

    So I use it by doing
    cat sometxtfile | getRecords.awk charlie
    and it should print out every line with the word charlie.

    my problem is that awk always interprets charlie as a file and then exits when it can't open it.
    that's why I put in the delete statement but it doesn't help.
    I know this would be easier to accomplish with grep, but this is part of something bigger so I'm writing an awk script.
    Last edited by blackbox111; 10-16-2009 at 07:23 PM. Reason: changed title

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    955
    I think it's that "-f " argument that causes the trouble. It tells awk to look for a file containing the script that it is supposed to run. Why do you use that argument?
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  3. #3
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Try this:

    Code:
    #!/bin/awk -f
    BEGIN {
      search = ARGV[1]
      print "search is:", search
      delete ARGV[1]
    }
    
    $0 ~ search

  4. #4
    Just Joined!
    Join Date
    Oct 2009
    Posts
    10
    thanks it works. So deleting argv[1] makes it so that awk doesn't try to open up the argument thinking it's a file? at what point in time does it try to do this? for example, by the time the delete statement is reached

    doesn't the -f option on the command line tell awk to expect a file?
    one more question when: when using fields (e.g. $0) how does it know whether it's referring to the input (in this case whatever I pipe to it when I got cat words | ...) or from the argument?

  5. #5
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Quote Originally Posted by blackbox111 View Post
    thanks it works. So deleting argv[1] makes it so that awk doesn't try to open up the argument thinking it's a file?
    Exactly.

    at what point in time does it try to do this? for example, by the time the delete statement is reached
    awk process the BEGIN block before reading any input. That's why you can actually preprocess the arguments in the BEGIN block. In the BEGIN and END blocks awk do not try to open it's arguments as a named files, it does it in the blocks that do not try to match the spetial patterns BEGIN and END.

    doesn't the -f option on the command line tell awk to expect a file?
    You need the -f in the shebang line in order to make awk parse the rest of the file. From the man pages:

    -f program-file
    --file program-file
    Read the AWK program source from the file program-file, instead
    of from the first command line argument. Multiple -f (or
    --file) options may be used.
    one more question when: when using fields (e.g. $0) how does it know whether it's referring to the input (in this case whatever I pipe to it when I got cat words | ...) or from the argument?
    The source (STDIN rather than a named file) does not make any difference for awk's record processing.

    Code:
    % print a b | awk '{ print $2 }' - <(print c d)
    b
    d
    If you need to, consider the following:

    Code:
    % cat infile 
    c d
    % print a b | 
      awk '{ print "Input from:",
              FILENAME ~ "-" ?
              "STDIN" : "named file",
               $2
      }' - infile
    Input from: STDIN b
    Input from: named file d

Posting Permissions

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