简体   繁体   English

Ubuntu终端中的AWK命令出错

[英]Error with AWK command in ubuntu terminal

I am new in linux and AWK as well. 我也是linux和AWK的新手。 I have a file in my home folder called testing.txt and i am trying to read the file using this awk command: 我的主文件夹中有一个文件testing.txt ,我正在尝试使用以下awk命令读取该文件:

**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*. 在shell中,双引号"字符串可以包含具有特殊含义的表达式(例如反引号,变量),这些表达式将在作为完整shell命令的一部分处理该字符串之前进行扩展。单引号'中的字符串已完全转义;将其括起来换句话说,字符串在字面上没有任何解释地传递,这就是为什么在编写awk脚本时应使用单引号,因为awk变量解引用运算符$与shell中的相同,没有其他有效的字符串定界字符*。

I initially thought you'd used backticks (thanks to Andras Deak for spotting my error). 我最初以为您使用了反引号(感谢Andras Deak发现我的错误)。 Backticks have a special meaning in shell (equivalent to wrapping something in $(...) ): execute this string as a command, and evaluate to its output (stdout). 反引号在shell中有特殊含义(相当于在$(...)包装内容):将该字符串作为命令执行,并对其输出求值(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: 您可以在几个地方阅读更多有关shell行为的信息:

* ignoring that spaces are also technically string-delimiters *忽略空格在技术上也是字符串定界符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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