Help with awk

Hi,

I have to print last field in a record separated by ~ symbol:

Eg:

code:

echo abc~@def\(b d c)~ghi("a b c") | awk -F~ '{print $3}'

getting errors like this

-bash: syntax error near unexpected token `('

You should echo it properly -

echo "abc~@def(b d c)~ghi("a b c")" | awk -F'~' '{print $NF}'

It will give you -

ghi(a b c)

or
If you want the double quotes in last field -

echo 'abc~@def(b d c)~ghi("a b c")' | awk -F'~' '{print $NF}'

It will give you -

ghi("a b c")

HTH

You can't use "nested" double quotes. The first quoted string ends with the opening parenthesis, the second contains only ) . As you can see, a b c is missing the double quotes. Try
escaping the quotes:

echo "abc~@def(b d c)~ghi(\"a b c\")" | awk -F~ '{print $3}'
ghi("a b c")

---------- Post updated at 09:47 ---------- Previous update was at 09:46 ----------

or, use single quotes (but beware that the shell doesn't expand within single quotes):

echo 'abc~@def(b d c)~ghi("a b c")' | awk -F~ '{print $3}'
ghi("a b c")

Whatever is placed inside single () the shell is going to interpret it as commands and try to create a subshell. You have to think as () having extra meaning for the shell, and when they need to be just parenthesis, you need to escape them with `\' or quote them.

echo abc~@def\(b d c\)~ghi\("a b c"\)