Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

linux - Copy differences between two files in unix

Firstly, which is the best and fastest unix command to get only the differences between two files ? I tried using diff to do it (below).

I tried the answer given by Neilvert Noval over here - Compare two files line by line and generate the difference in another file

code -

diff -a --suppress-common-lines -y file1.txt file2.txt >> file3.txt

But, I get a lot of spaces and a > symbol also before the different lines. How do I fix that ? I was thinking of removing trailing spaces and the first '>', but not sure if that is a neat fix.

My file1.txt has -

Hello World!
Its such a nice day!
#this is a newline and not a line of text# 

My file1.txt has -

Hello World!
Its such a nice day!
Glad to be here!
#this is a newline and not a line of text# 

Output - " #Many spaces here# > Glad to be here:)"

Expected output - Glad to be here:)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Another way to get diff is by using awk:

awk 'FNR==NR{a[$0];next}!($0 in a)' file1 file2

Though I must admit that I haven't run any benchmarks and can't say which is the fastest solution.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...