sdiff Command in Linux with Examples

This tutorial explains Linux “sdiff” command, options and its usage with examples.

sdiff – Compares two files and displays the differences in a side-by-side format.

DESCRIPTION

The sdiff command reads the files specified by the File1 and File2 parameters, uses the diff command to compare them, and writes the results to standard output in a side-by-side format. The sdiff command displays each line of the two files with a series of spaces between them if the lines are identical. It displays a < (less than sign) in the field of spaces if the line only exists in the file specified by the File1 parameter, a > (greater than sign) if the line only exists in the file specified by the File2 parameter, and a | (vertical bar) for lines that are different.

When you specify the -o flag, the sdiff command merges the files specified by the File1 and File2 parameters and produces a third file.

SYNOPSIS

sdiff [ -l | -s ] [ -o OutFile ] [ -w Number ] File1 File2

advertisement
advertisement

OPTIONS :

-l
Displays only the left side when lines are identical.
-o OutFile
Creates a third file, specified by the OutFile variable, by a controlled line-by-line merging of the two files specified by the File1 and the File2 parameters.
The following subcommands govern the creation of this file:
e
Starts the ed command with an empty file.
e b or e |
Starts the ed command with both sides.
e l or e < Starts the ed command with the left side. e r or e >
Starts the ed command with the right side.
l
Adds the left side to the output file.
r
Adds the right side to the output file.
s
Stops displaying identical lines.
v
Begins displaying identical lines.
q
Performs one of the following functions:
-Exits the ed command.
-Exits the sdiff command if no ed command is running.
-Exits both commands. This action occurs when there are no more lines to be merged into the output file.
Each time you exit from the ed command, the sdiff command writes the resulting edited file to the end of the file specified by the OutFile variable. If you do not save the changes before exiting (for example, you press the Ctrl-C key sequence), the sdiff command writes the initial input to the output file.

-s
Does not display identical lines.
-w Number
Sets the width of the output line. The default value of the Number variable is 130 characters. The maximum width of the Number variable is 2048. The minimum width of the Number variable is 20. The sdiff command uses 2048 if a value greater than 2048 is specified.

EXAMPLES

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

1. To print a comparison of two files, enter

$ sdiff file1.txt file2.txt
abc								abc
def							      <
ghi								ghi
							      >	klm

2. To display only the lines that differ, enter

$ sdiff  -s  -w 80 chap1.bak chap1

The sdiff command displays the differences at the work station. The -w 80 flag and variable sets the page width to 80 columns. The -s flag indicates lines that are identical in both files will not be displayed.

advertisement

3. To combine and edit two files, staff.jan and staff.apr, and write the results to the staff.year file, perform the steps indicated

The staff.jan file contains the following lines:

 Members of the Accounting Department
Andrea
George
Karen
Sam
Thomas

The staff.apr file contains the following lines:

 Members of the Accounting Department
Andrea
Fred
Mark
Sam
Wendy

Enter the following command:

advertisement
$ sdiff -o staff.year staff.jan staff.apr

The sdiff command will begin to compare the contents of the staff.jan and staff.apr files and write the results to the staff.year file. The sdiff command displays the following:

 Members of the Accounting Dept   Members of the Accounting Dept
Andrea                            Andrea
George                          | Fred
%

The % (percent sign) is the command prompt.

Enter the e b subcommand to start editing the output file with the ed command.
The sdiff command displays a sequence of digits, indicating the byte count of lines being merged. In this case, the byte count is 23.

Enter the q subcommand to exit the ed command and continue combining and editing the two files. The sdiff command displays the following:

 Sam                              Sam
Thomas                          | Wendy

Enter the e b subcommand again. The ed command must be run each time a set of lines from the original two files are to be merged into the output file. The byte count in this instance is 13.

Enter the q subcommand to save the changes. When all the lines of the two files have been merged into the output file, the q subcommand exits the ed and sdiff commands.

The staff.year file now contains the following:

 Members of the Accounting Department
Andrea
George
Karen
Fred
Mark
Sam
Thomas
Wendy

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.