3

I am learning awk to advance my skills and tried

$ atq | awk "{print $1}" 28 Wed Oct 31 10:23:00 2018 27 Tue Oct 30 21:20:00 2018 25 Tue Oct 30 21:19:00 2018 29 Wed Oct 31 10:42:00 2018 26 Tue Oct 30 21:20:00 2018 20 Tue Oct 30 18:25:00 2018 30 Wed Oct 31 10:59:00 2018 32 Wed Oct 31 21:03:00 2018 23 Tue Oct 30 18:28:00 2018 31 Wed Oct 31 13:58:00 2018 19 Tue Oct 30 15:43:00 2018 21 Tue Oct 30 18:27:00 2018 

It did not work as I expected, but it's relatively easy to work small programs together

$ atq | cut -f 1 28 27 25 29 26 20 30 32 23 31 19 21 

How could I retrieve the first field using awk?

1
  • 3
    You need single quotes around the awk commands. Commented Oct 31, 2018 at 13:25

1 Answer 1

8

Try the below,

atq | awk '{print $1}' 

The single quotes stops the shell from expanding $1 to the shell's first positional parameter.

3
  • great, could you please elaborate the reasons. Commented Oct 31, 2018 at 13:24
  • 3
    An alternative would be atq | awk "{print \$1}" - sometimes there are reasons you want the double quote, and in that case, escaping the $ is necessary. Contrived example: prefix="foo: "; atq | awk "{print $prefix, \$1}". Granted, that would normally be better accomplished using awks -v variable=... syntax, but it can still be useful sometimes... Commented Oct 31, 2018 at 14:09
  • @twalberg No, you don't generally want the shell to interpolate the awk code. To use a shell variable in an awk program, import it using -v var="$var" or export it and access it through ENVIRON["var"]. Using single quotes around awk code is always possible and always what you'd want to do. Commented Nov 6, 2018 at 10:46

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.