简体   繁体   English

在BASH别名或函数中使用awk

[英]Using awk in BASH alias or function

I've a command that works just fine at the command line, but not when I try to put it in an alias or function. 我有一个在命令行上运行正常的命令,但是当我尝试将它放在别名或函数中时却没有。

$ awk '{print $1}' /tmp/textfile
0

That's correct, as '0' is in position 1 of "textfile". 这是正确的,因为'0'在“textfile”的位置1。

$ alias a="awk '{print $1}' /tmp/textfile"
$ a
1 0 136 94

That's the entire line in "textfile". 这就是“textfile”中的整行。 I've tried every variety of quotes, parentheses and backticks that I could imagine might work. 我已经尝试了各种各样的引号,括号和反引号,我可以想象它们可能会起作用。 I can get the same problem in a wide variety of formats. 我可以用各种格式得到同样的问题。

What am I not understanding? 我不明白的是什么?

You need to escape the $ like so: 你需要像这样逃避$

 alias a="awk '{print \$1}' /tmp/textfile"

Otherwise your alias is: 否则你的别名是:

 awk '{print }' /tmp/textfile

Which prints the whole file... 打印整个文件...

使用函数而不是别名

myfunc(){ awk '{print $1}' file; }

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

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