在 BASH 别名或函数中使用 awk

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7244534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:41:35  来源:igfitidea点击:

Using awk in BASH alias or function

bashawk

提问by Corn-fuzed

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 }' /tmp/textfile
0

That's correct, as '0' is in position 1 of "textfile".

这是正确的,因为“0”位于“文本文件”的位置 1。

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

That's the entire line in "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?

我不明白什么?

回答by Linus Kleen

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...

打印整个文件...

回答by ghostdog74

Use a function instead of alias

使用函数代替别名

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