Linux 使用 awk 的输出来运行命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20389799/
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
Using output of awk to run command
提问by nLee
I am brand new to shell scripting and cannot seem to figure out this seemingly simple task. I have a text file (ciphers.txt) with about 250 lines, and I would like to use the first column of each line as an argument in a command. Any help would be greatly appreciated!
我是 shell 脚本的新手,似乎无法弄清楚这个看似简单的任务。我有一个大约 250 行的文本文件 (ciphers.txt),我想使用每行的第一列作为命令中的参数。任何帮助将不胜感激!
the command is:
命令是:
openssl s_client -connect host:port -cipher argument
It works fine when I do one at a time but I do not really want to run the same command 250+ times. Here is my script so far:
当我一次执行一个时它工作正常,但我真的不想运行相同的命令 250 多次。到目前为止,这是我的脚本:
awk '{command = "openssl s_client -connect localhost:4433 -cipher > results.txt"
print awk '{ system("openssl s_client -connect host:port -cipher " ) }' ciphers.txt
| command}' ciphers.txt
I keep getting an error so I am pretty sure I have a syntax error somewhere. Is the output of awk being appended after -cipher?
我一直收到错误,所以我很确定我在某处有语法错误。awk 的输出是否附加在 -cipher 之后?
采纳答案by David
Use system
from within awk:
system
在 awk 中使用:
awk '{cmd="openssl s_client -connect host:port -cipher" ; system(cmd)}' results.txt
回答by Andreas
there are quite a few things wrong with your command. For one you want to use the first column. That's referred to as $1 in awk and not $0 (which would be the whole line). Second, you forgot a semicolon at the end of your definition of command.
你的命令有很多问题。对于您想使用第一列的人。这在 awk 中被称为 $1 而不是 $0(这将是整行)。其次,您在命令定义的末尾忘记了分号。
To actually run the command you can either use system() or a pipe (the latter only makes sense if the command can read from stdin, which openssl in your case won't, I think). The easiest would be something like
要实际运行命令,您可以使用 system() 或管道(后者仅在命令可以从 stdin 读取时才有意义,我认为在您的情况下 openssl 不会)。最简单的就是这样
awk '{print ls -1 *.txt | xargs -I{} mv "{}" "{}.$(date '+%y%m%d')"
}' <ciphers.txt | xargs -I{} openssl s_client -connect host:port -cipher {} >>results.txt
Note, that this will only return the exit status. If you need to capture stdout, you will have to pipe the command through getline.
请注意,这只会返回退出状态。如果您需要捕获标准输出,则必须通过 getline 管道命令。
Andreas
安德烈亚斯
PS: Posting the actual error you got, would have helped.
PS:发布您遇到的实际错误会有所帮助。
回答by T.Rob
The xargs
command is specifically for that use case.
该xargs
命令专门用于该用例。
This version is a bit longer for the example case because awk
was already being used to parse out $0
. However, xargs
comes in handy when you already have a list of things to use and are not running something that can execute a subshell. For example, awk
could be used below to execute the mv
but xargs
is a lot simpler.
对于示例案例,此版本稍长一些,因为awk
已经用于解析$0
. 但是,xargs
当您已经有了要使用的东西列表并且没有运行可以执行子 shell 的东西时,它会派上用场。例如,awk
可以在下面使用来执行mv
但xargs
要简单得多。
The above command renames each text file in the current directory to a date-stamped backup. The equivalent in awk
requires making a variable out of the results of the date
command, passing that into awk
, and then constructing and executing the command.
上述命令将当前目录中的每个文本文件重命名为带有日期戳的备份。等价的 inawk
需要从date
命令的结果中创建一个变量,将其传递给awk
,然后构造并执行命令。
The xargs
command can also accumulate multiple parameters onto a single line which is helpful if the input has multiple columns, or when a single record is split into recurring groups in the input file.
该xargs
命令还可以将多个参数累积到一行中,如果输入有多个列,或者当单个记录被拆分为输入文件中的重复组时,这将很有帮助。
For more on all the ways to use it, have a look at "xargs" All-IN-One Tutorial Guideover at UNIX Mantra.
有关使用它的所有方法的更多信息,请查看UNIX Mantra上的“xargs”多合一教程指南。