Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

diff two files and keep just new or changed lines

Tags:

file

bash

diff

I would like to write a bash script or a few shell commands that solves my problem. I have two files, old.txt, new.txt. I would like to produce a file, diff.txt, that has only the lines that have changed or are new. For instance if I had:

old.txt:

Sierra
Tango
Oscar
Victor
Whiskey
Yankee

new.txt:

Sierra
Tango
Echo
Osc__ar
Victor
Uniform
Whiskey
Yan__kee

I would want a diff.txt that looks like this:

Echo
Osc__ar
Uniform
Yan__kee

For perspective, I am writing this script to help me create a differential Motorolla S2 record for loading programs over serial port to an embedded computer. I know bash fairly well, I just don't know where to get started.

like image 477
benathon Avatar asked Mar 01 '12 02:03

benathon


People also ask

Which command gives all differences between two files?

diff stands for difference. This command is used to display the differences in the files by comparing the files line by line. Unlike its fellow members, cmp and comm, it tells us which lines in one file have is to be changed to make the two files identical.

How to find the difference between two configuration files in Linux?

To compare files in Linux and even in macOS, a utility used is called the “diff”. The “diff” utility compares two files and gives information about the differences between the two files. The developers primarily use the “diff” command to create patch files.


1 Answers

$ grep -v -f old.txt new.txt
Echo
Osc__ar
Uniform
Yan__kee
like image 121
chrisaycock Avatar answered Nov 03 '22 02:11

chrisaycock