0

I have a text file with lines saying:

File fileA and fileB differ 
File fileX and fileY differ

I need a bash script that goes through the whole file, for each line parses the file names and executes the command diff fileA fileB > fileA.diff

1
  • 2
    Good on you! Why does it have to be a bash script? Let us know which part of the solution you get stuck with and we'll endeavour to help. We won't do your work for you. Commented Sep 20, 2011 at 9:49

2 Answers 2

1

The following command:

awk '/ differ$/ {print "diff "$2" "$4" >"$2".diff"}{}'

will give you a script you can run to do this.

See the following transcript:

pax$ echo 'File fileA and fileB differ
hello
File fileX and fileY differ' | awk '
    / differ$/ {print "diff "$2" "$4" >"$2".diff"}{}'
diff fileA fileB >fileA.diff
diff fileX fileY >fileX.diff

Capture the output, then run it with bash and you'll have what you need.

Note that this won't work well with filenames that have spaces in them - if you have such heinous beasts, you will need to do a little more intelligent parsing.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I'm now using this: while read line; do awk '/ differ$/ {print "diff "$2" "$4" > "$2".diff"}{}'; done < diffs.txt Problem is awk only prints the command... how do I tell it to execute it?
0

This command generates a list of diff commands:

[me@home]$ sed -n 's/File \(.*\) and \(.*\) differ/diff "\1" "\2" > "\1.diff"/p' infile
diff "fileA" "fileB" > "fileA.diff"
diff "fileX" "fileY" > "fileX.diff"

You can execute the generated commands by passing them to bash using process substitution.

[me@home]$ bash <(sed -n 's/File \(.*\) and \(.*\) differ/diff "\1" "\2" > "\1.diff"/p' infile )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.