Use fold to wrap long lines for an easier diff

March 1, 2013 Mark Rushakoff

We had two versions of a not-really-intended-to-be-human-readable file that were only slightly different, and we wanted to know how they were different. The lines were several hundred characters long, so when we diffed the files, we saw a basically useless output of

@@ -1 +1 @@
- bf659d7a2e0e45223095367c561526f8a10311459433adf322f2590a4987c423e55afe6e88abb09f0bee39549f1dfbbdfba99e39c0b70a7a656d07061ee113676f0d6db25d88c8034db6d9d8beec42daaaf8818273d9436fca8f442cdb6245c285c33638
+ bf659d7a2e0e45223095367c561526f8a10311459433adf322f2590a4987c423e55afe6e88abb09f0bee39549f1dfbbdfba99e39c0b70a7a656d07061ee113676f0d6db25d88c8034db6d9d8beec42daaaf8818278d9436fca8f442cdb6245c285c33638

My usual trick of opening the file in Vim and using gq didn’t work because there was no whitespace in the string, and Vim (at least in my configuration) will only split on whitespace.

Text-wrapping is certainly not a new problem — surely there’s a Unix utility for that, I thought. And sure enough, fold was there to help.

After folding at an arbitrary length of 25 characters (with fold -w 25 $FILE), the diff output became readable and thus useful:

@@ -4,5 +4,5 @@

Now can you see the 3 that turned into an 8?

(Of course, this whole situation could have been avoided if we tried to make that output human-readable in the first place. Live and learn!)

About the Author


More Content by Mark Rushakoff
BDD starts with a conversation
BDD starts with a conversation

Let’s talk about cargo culting. When I started doing BDD, I cargo-culted it. I basically had no idea what I...

We’ll see you at the Bazaarvoice Summit!
We’ll see you at the Bazaarvoice Summit!

We’re going to be speaking at the sold-out Bazaarvoice Summit in Austin, Texas from March 4-6. Drop by to l...

Enter curious. Exit smarter.

Register Now