执行awk输出

发布于 2024-12-05 12:05:11 字数 163 浏览 1 评论 0原文

while read line;
do
  awk '/ differ$/ {print "diff "$2" "$4" > "$2".diff"}{}';
done < diffs.txt

这将完全按照我想要的方式打印命令。我如何告诉它执行命令?

while read line;
do
  awk '/ differ$/ {print "diff "$2" "$4" > "$2".diff"}{}';
done < diffs.txt

This prints the command exactly as I want it. How do I tell it to execute the command?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千紇 2024-12-12 12:05:11

<代码>| bash 可以解决问题...

while read line;
do
  awk '/ differ$/ {print "diff "$2" "$4" > "$2".diff"}{}' | bash;
done < diffs.txt

| bash does the trick...

while read line;
do
  awk '/ differ$/ {print "diff "$2" "$4" > "$2".diff"}{}' | bash;
done < diffs.txt
寄意 2024-12-12 12:05:11

您可以使用“system”命令来执行此类任务。

awk '/ differ$/ {system("diff "$2" "$4" > "$2".diff")} diffs.txt

You can use the "system" command for these kinds of tasks.

awk '/ differ$/ {system("diff "$2" "$4" > "$2".diff")} diffs.txt
夜唯美灬不弃 2024-12-12 12:05:11

对于这个问题,接受的答案(@micheal)仅部分正确。它适用于几乎所有情况,但命令需要创建新终端或伪终端时除外。就像“ssh”命令或“tmux new”一样。

以下代码也适用于这些情况。

while read line;
do

  $(awk '/ differ$/ {print "diff "$2" "$4" > "$2".diff"}{}')
done < diffs.txt

$() 是 bash 命令替换模式。您可以在此处阅读有关 Linux 文档项目中的命令替换的更多信息:http://www. tldp.org/LDP/abs/html/commandsub.html

The accepted answer (by @micheal) for this question is only partially correct. It works for almost all cases, except when the command requires creation of a new terminal or pseudo terminal. Like 'ssh' commands, or 'tmux new'..

Following code works for those cases also.

while read line;
do

  $(awk '/ differ$/ {print "diff "$2" "$4" > "$2".diff"}{}')
done < diffs.txt

$() is the bash command substitution pattern. You can read more about command substitution in Linux Documentation Project here : http://www.tldp.org/LDP/abs/html/commandsub.html.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文