2

Having troubles with awk. Basically what I'm trying to do is search for this particular string in the file. And then print the 3rd element of that line.

Here's what I did:

awk -F' / ' '$1=="$log"{print $3}' schedlist

For some reason that won't work but if I do this:

awk -F' / ' '$1=="20121213-20:58:53"{print $3}' schedlist

The code works. But I need to let the user input log name.

2 Answers 2

3

You need to use the -v option to pass a shell variable into awk, like this:

awk -v tstamp="$log" -F' / ' '$1==tstamp{print $3}' schedlist

(Updated the variable name to tstamp (as mentioned in the comments), because log clashes with the awk built-in function log.)

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

2 Comments

Hmmm I've tried that and I get this error:awk: run time error: cannot command line assign to log type clash or keyword FILENAME="" FNR=0 NR=0
+1 for the correct answer but unlucky that you picked a variable name that clashes with a builtin function name "log"! Just change the variable name to tstamp or something: awk -v tstamp="$log" -F' / ' '$1==tstamp{print $3}' schedlist
2

You have several options. Use a variable ( either with -v or simply as an assignment, but you cannot name it "log", because that is an awk function):

awk -F' / ' '$1 == s {print $3}' s="$log" schedlist

quote your script differently:

awk -F' / ' '$1 == "'"$log"'" { print $3}' schedlist

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.