0

I am new in linux and AWK as well. I have a file in my home folder called testing.txt and i am trying to read the file using this awk command:

**arjun@arjun-Aspire-4741:~$ awk ´{print $1}´ testing.txt¨**   

And I am getting this as output

**¨awk: ´{print
awk: ^ invalid char '�' in expression
arjun@arjun-Aspire-4741:~$ ¨**

1 Answer 1

1

The problem is that you've used forward ticks instead of quotes (in this case only single quotes are appropriate):

awk '{print $1}' testing.txt

instead of

awk ´{print $1}´ testing.txt

In shell, strings in double quotes " can contain expressions with special meaning (such as backticks, variables) which will be expanded before the string is processed as part of the full shell command. Strings in single quotes ' are fully escaped; to put it another way, the string is passed literally without any interpretation. That's why you should use single quotes when writing awk scripts, because the awk variable dereference operator $ is the same as in shell. There are no other valid string-delimiting characters*.

I initially thought you'd used backticks (thanks to Andras Deak for spotting my error). Backticks have a special meaning in shell (equivalent to wrapping something in $(...)): execute this string as a command, and evaluate to its output (stdout). This is done before your main command is executed.

So, if I do

cat `echo myfile`

this turns into

cat myfile

which then executes.

You can read more about shell behaviour in a few places:

* ignoring that spaces are also technically string-delimiters

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

2 Comments

Actually, OP seems to have forward ticks, for whatever reason, which explains why the error says invalid char '�' instead of something about a token or a command not found.
@AndrasDeak Well spotted! I've adjusted my answer appropriately.

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.