Michael
Michael

Reputation: 14218

Execute awk output

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?

Upvotes: 8

Views: 14743

Answers (3)

iankit
iankit

Reputation: 9352

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.

Upvotes: 3

Chris
Chris

Reputation: 3047

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

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

Upvotes: 6

Michael
Michael

Reputation: 14218

| bash does the trick...

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

Upvotes: 17

Related Questions