Diff`ing Files with Difftastic

Diff`ing Files with Difftastic

What is Difftastic?

Difftasic is a CLI tool that allows you to compare files based on their syntax rather than rely on line-by-line matching.

For example, this means that if you had the following files, Difftastic would not detect any differences.

$ cat vlans_1.json
{ "vlans": [100, 200, 300] } 

$ cat vlans_2.json 
{
  "vlans": [
    100,
    200,
    300
  ]
}

$ difft vlans_1.json vlans_2.json
vlans_2.json --- JSON
No syntactic changes.

Some other features that Difftastic provides are:

  • Support for 40+ programming languages
  • Support for all the major structured text formats (inc JSON, YAML, XML, etc.)
  • Ability to perform diffs recursively across directories.
  • Language detection based on file extension.

Install

Since Difftastic is Rust-based, we must ensure the Rust package manager – cargo – is installed. Then, we perform the required installation. Like so:

# Install the Rust package manager
# sudo apt install cargo

# Install difftastic
cargo install --locked difftastic

Example

Here's a quick example of using Difftastic against 2 JSON files that contain the output of show ip ospf neighbors from a Cisco Nexus device.

$ difft pre/sh_ip_ospf_neighbor.json post/sh_ip_ospf_neighbor.json
...

Here's an example of the output:

There are a few things to mention here:

  • The format of the file is autodetected.
  • The default display is side-by-side
  • The exit code of 0 will be returned regardless.

Therefore a few other options we can also use are:

  • --display inline to display the output in a single column.
  • --exit-code to return a return code of 1 should there be a difference in files.

In addition, should you have multiple files in 2 folders with the same name, we can use: difft folder1 folder2 to have Difftastic recurse and diff the various files across each of the folders.

Closing Comments

If you need a quick way to diff different files based on syntax, then Difftasitc is an excellent choice for a tool to add to your automation toolbox, as it's quick and easy to use, plus it provides support for many different languages.

On the other hand:

  • if you are looking to compare text-based files, then a tool such as difflib may be better suited.
  • if you want the results from diffing structured based formats such as JSON, back also as structured data then a tool such as jsondiff may be a better choice.

Subscribe to our newsletter and stay updated.

Don't miss anything. Get all the latest posts delivered straight to your inbox.
Great! Check your inbox and click the link to confirm your subscription.
Error! Please enter a valid email address!