Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I know ARGV[i] can be used for storing user input. However, I want to use it in awk script and get the ARGV[i] to compare the field from another text file. If the ARGV[i] matches the field or the field contains ARGV[i] which is the user input, then I want to return the other fields of that line.

Let say I have a file, testing.txt:

123123123:Walter White:1:2:3:4
123123124:Jesse Pinkman:1:3:4:1

This is my awk script, awkScript.awk:

 #!/usr/bin/awk -f
    BEGIN{FS = ":"; print "ENTER A NAME: "}
    {
        for (i = 1; i < ARGC; i++)
        {   
            if ($2 ~ /'ARGV[i]'/)
            {
                print "Member ID: " $1
            }
        }
    }

It just prints ENTER A NAME: when I execute the script file. It doesn't even get the input from the user.

share|improve this question

closed as off-topic by jasonwryan, slm Oct 19 '14 at 19:03

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." – jasonwryan, slm

    
how do you call awk's file ? how do you tell awk to read testing.txt ? how do you think awk will get user input ? –  Archemar Oct 19 '14 at 9:38
    
awk -f awkScript.awk testing.txt My question is how to get user input by using awk... –  coding Oct 19 '14 at 9:42
1  
"ARGV[i] can be used for storing user input" it's not user input, it's argument passing to awk script, like positional params to bash –  Rahul Patil Oct 19 '14 at 10:01
    
Crossposted on SO... –  jasonwryan Oct 19 '14 at 18:44

1 Answer 1

To match the contents of ARGV[i] you need to change your test to

if ($2 ~ ARGV[i])

Otherwise, you are testing for a match to the literal regex 'ARGV[i]' which is not what you want.

Then your script will (kind of) work if you run it like this:

awk -f qtest.awk testing.txt "Jesse Pinkman" "Walter White"

But then it will throw an error when it finishes reading testing.txt and tries to open the file "Jesse Pinkman" (unless, of course, you happen to have a file with that name).

In other words, ARGV contains the file names passed to your script by the shell and you shouldn't try to use it for passing in arbitrary arguments. The usual way to pass non-file arguments to an awk script is to use the -v var = val syntax to initialise variables in your script.

But I suspect that a better approach here is to read testing.txt in your BEGIN block, using the getline var <namefile.txt syntax, splitting the data (using the split() function), and storing the split data in a 2D array. Then your main processing loop can read in names (either from a file or from the user) and you can then test those names using the data you've saved in the array.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.