开发者

Execute awk output

开发者 https://www.devze.com 2023-04-05 22:10 出处:网络
while rea开发者_JS百科d line; do awk \'/ differ$/ {print \"diff \"$2\" \"$4\" > \"$2\".diff\"}{}\';
while rea开发者_JS百科d 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?


| bash does the trick...

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


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

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消