2

I want to invoke shell command from awk so I am using system() but when I am passing expression in I am getting error at backslash. Here is my expression

echo "select * from abc limit 1;"| impala-shell|grep -Eo " a-[0-9]-[0-9]* \| HS[0-9]  \| [0-9]* \| [0-9]* "

But when I am passing it to shell, I'm getting an error backslash not last character on line.

system("echo \"select * from abc limit 1;\"| impala-shell|grep -Eo \" a-[0-9]-[0-9]* \| HS[0-9]  \| [0-9]* \| [0-9]* \" ")

I want to get the above command result in awk programming after system(), I know we can access through getline but don't know the exact syntax.

EDIT

When using \| in system as "\|" got following

`warning: escape sequence `\|' treated as plain `|'` 

I took some simple example to see \| in an expression

system("echo hey i am  \"\\|\" ashish ")
hey i am \| ashish

but when I try within double quote and single quote yields in

system("echo \" a-[0-9]* \"\\|\" HS[0-9]  \"\\|\" [0-9]* \"\\|\" [0-9]* \" ")
a-[0-9]* | HS[0-9]  | [0-9]* | [0-9]*
//single quote
system("echo \' a-[0-9]* \"\\|\" HS[0-9]  \"\\|\" [0-9]* \"\\|\" [0-9]* \' ")
a-[0-9]* "\|" HS[0-9]  "\|" [0-9]* "\|" [0-9]*

what I am expecting is a-[0-9]* \| HS[0-9] \| [0-9]* \| [0-9]*.

  • I removed the system-call tag and changed the title (call -> command) to prevent confusion. While you are calling the awk system() command, this is unrelated to the concept of a system call. – goldilocks Jul 16 '14 at 13:36
1

This string literal in awk

"echo \"select * from abc limit 1;\"| impala-shell|grep -Eo \" a-[0-9]-[0-9]* \| HS[0-9]  \| [0-9]* \| [0-9]* \" "

represents the following string value:

echo "select * from abc limit 1;"| impala-shell|grep -Eo " a-[0-9]-[0-9]* | HS[0-9]  | [0-9]* | [0-9]* " 

Backslash followed by another character which is not a letter or digit represents that digit. (At least with the historical awk implementation and with GNU awk — some awk implementations leave the backslash alone when it's followed by a character such as | that awk doesn't treat as special.)

When you want awk to emit the character sequence \|, you need to protect the backslash in a string literal, so write `"\|".

system("echo \"select * from abc limit 1;\"| impala-shell|grep -Eo \" a-[0-9]-[0-9]* \\| HS[0-9]  \\| [0-9]* \\| [0-9]* \""
  • thanks for ur answer,go following warning: escape sequence " \|' treated as plain |' " – Aashu Jul 17 '14 at 12:32

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.