6

Would someone please explain what is happening with below awk command? if it's no error then why notme is not getting print, and why I'm not getting syntax error then for close bracket ...} need close with quote ...}'?

$ awk '{print "me "$0 '"notme"} <<<"printme"
me printme

Then so I would tried this:

$ awk '{print "me "$0 '"\"$(date)"\"} <<<"printme-at "
me printme-at Wed Apr 11 16:41:34 DST 2018

Or

awk '{print '"\"$(date)\""} <<<"run"
Wed Apr 11 16:56:38 DST 2018

Which as it shows means I can do everything using shell command substitution.

Is this a bug? Or maybe a special state that I cannot find.

  • Correct, but why no syntax error I'm getting? for those who want to know what I'm trying to achieve? I just accidently typed a single quote and I had no close quote for awk there ...}. I also tried to create a file called notme} as I found I'm not getting error for unquoted }, then did ...cat "notme"} <<<"printme" and I got syntax error, and when I add ; in ...; cat "notme"} <<<"printme" then nothing changed because my awk is completely will give syntax error and anything after ; in shell will consider a separate command, so I go into using $(...) within awk. – αғsнιη Apr 11 '18 at 12:53
  • As a side note, at least GNU Awk has a built-in function strftime that can perform the same task as $(date) but without the help of command substitution or other shell features. – David Foerster Apr 11 '18 at 16:39
7

For the first one

$ awk '{print "me "$0 '"notme"} <<<"printme"

What's happening here is:

  1. The part in single quotes is passed to awk verbatim
  2. The next part "notme"} is parsed by the shell and then passed to awk as the resulting string notme}
  3. awk gets to see this:

    {print "me "$0 notme}
    

    Since the awk variable notme has no value, that's what you get

For the second one

$ awk '{print "me "$0 '"\"$(date)"\"} <<<"printme-at "
me printme-at Wed Apr 11 16:41:34 DST 2018

I'd be more inclined to write it like this, using an awk variable to carry the value of $(date):

awk -v date="$(date)" '{print "me "$0 date}' <<<"printme-at "
me printme-at Wed Apr 11 13:43:31 BST 2018

You've asked why there is no syntax error in your version of this one. Let's take it apart:

# typed at the shell
awk '{print "me "$0 '"\"$(date)"\"} <<<"printme-at "

# prepared for awk
awk '{print "me "$0 "Wed Apr 11 16:41:34 DST 2018"}' <<<"printme-at "

# seen by awk
{print "me "$0 "Wed Apr 11 16:41:34 DST 2018"}

The double-quoted string "\"$(date)\"" is parsed by the shell before awk gets anywhere near it, and is evaluated (by the shell) as something like the literal string "Wed Apr 11 13:43:31 BST 2018" (including the double-quote marks). I don't see why there needs to be a syntax error as what you have written is valid - although somewhat tortuous to read.

  • why no awk syntax error awk '{print "me "$0 '"\"$(date)"\"} <<<"printme-at " then? also please read – αғsнιη Apr 11 '18 at 12:54
  • @αғsнιη Why should there be a syntax error there? Run it in the shell, but use set -x first. – Kusalananda Apr 11 '18 at 13:43
2

The single quote ends the string that delimits the awk program in the shell. awk itself will never see it. You then concatenate that initial part of the program with more strings provided by command substitutions and static strings in the shell. This all happens before awk has been invoked.

Of course you could use command substitutions to modify a string that later is read by awk as its code, but it does not really make for easy to read code, and may be rather fragile with regards to the shell's quoting rules, word splitting etc.

It would be better to just set an awk variable in the usual way:

awk -v thedate="$(date)" '{ print $0, thedate }'  <<<"something"
  • hmm, your first sentence make sense to me about the single quote , – αғsнιη Apr 11 '18 at 12:58
  • referring to your first sentence, another thing that I saw is that the single quote insids awk should together with at least one of double quotes otherwise it will produce a syntax error again which it makes more difficult to me understand the issue there. – αғsнιη Apr 11 '18 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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