简体   繁体   中英

Error with AWK command in ubuntu terminal

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:~$ ¨**

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM