diff files while disregarding indentation and trailing white space

diff <(perl -wpl -e '$_ =~ s/^\s+|\s+$//g ;' file1) <(perl -wpl -e '$_ =~ s/^\s+|\s+$//g ;' file2)
**NOTE** Tekhne's alternative is much more succinct and its output conforms to the files actual contents rather than with white space removed My command on the other hand uses bash process substitution (and "Minimal" Perl), instead of files, to first remove leading and trailing white space from lines, before diff'ing the streams. Very useful when differences in indentation, such as in programming source code files, may be irrelevant
Sample Output
gjempty$ diff <(perl -wpl -e '$_ =~ s/^\s+|\s+$//g ;' file1) <(perl -wpl -e '$_ =~ s/^\s+|\s+$//g ;' file2)
> for ($i = 0; $i < 7; $i++) {
> if (!isset($tabData[$i])) $tabData[$i] = '';
> }

gjempty$ diff -b file1 file2
>                         for ($i = 0; $i < 7; $i++) {
>                             if (!isset($tabData[$i])) $tabData[$i] = '';
>                         }

2010-10-06 19:14:42

1 Alternatives + Submit Alt

What Others Think

diff -b $file1 $file2 # GNU Tools
Tekhne · 605 weeks and 5 days ago
You can learn so many such commands through this website and it's good to see the details shared over Lab grown diamonds here. The above command is used to diff files while disregarding indentation and trailing white spaces. You can see the sample output here and another alternative is also provided here.
Alyssalauren · 24 weeks and 6 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: