简体   繁体   中英

Linux : why I can't display the diffrents lines between these two files?

I want to compare two files and display the lines that are differents between these two files. I've tried some ways to do that but I'm not able to do that...

I've this first file:

frvmx0000423
ansible-test-relay-do-not-delete
frvmx0000434
frvmt0003036
frvmx0000661
rhel6918111243
frvmx0000416
frvmt0003040
mcds-test-303
jmt201
fb-demo5
frvmx0000635
cb003
rhel7511130951
rhel7511130956
vlpchh08
mcds-test-301
frvmx0000576
frvmx0000683
frvmx0000654
frvmx0000685
frvmx0000446
vlplin01
frvmx0000687
rhel69611917
frvmx0000622
rhel7511161022
vlpijl03
vlpedu03

And this second file:

frvmx0000683
frvmx0000576
ansible-test-relay-do-not-delete
vlpedu03
frvmx0000687
frvmx0000685
frvmx0000654
frvmt0003299
mcds-test-301
cb003
mcds-test-303
FRVMT0003040
cb004
frvmx0000661
frvmt0003036

I want this output which is the lines that are differents between the files:

frvmx0000423
frvmx0000434
rhel6918111243
frvmx0000416
jmt201
fb-demo5
frvmx0000635
rhel7511130951
rhel7511130956
vlpchh08
frvmx0000446
vlplin01
rhel69611917
frvmx0000622
rhel7511161022
vlpijl03

But when I try to use the diff command:

diff --suppress-common-lines file1 file2

The output is:

1,15c1,29
< frvmx0000683
< frvmx0000576
< ansible-test-relay-do-not-delete
< vlpedu03
< frvmx0000687
< frvmx0000685
< frvmx0000654
< frvmt0003299
< mcds-test-301
< cb003
< mcds-test-303
< FRVMT0003040
< cb004
< frvmx0000661
< frvmt0003036
---
> frvmx0000423
> ansible-test-relay-do-not-delete
> frvmx0000434
> frvmt0003036
> frvmx0000661
> rhel6918111243
> frvmx0000416
> frvmt0003040
> mcds-test-303
> jmt201
> fb-demo5
> frvmx0000635
> cb003
> rhel7511130951
> rhel7511130956
> vlpchh08
> mcds-test-301
> frvmx0000576
> frvmx0000683
> frvmx0000654
> frvmx0000685
> frvmx0000446
> vlplin01
> frvmx0000687
> rhel69611917
> frvmx0000622
> rhel7511161022
> vlpijl03
> vlpedu03

If I try with sort and uniq :

cat postman_list vcenter_list | sort | uniq -u

The output is:

FRVMT0003040
ansible-test-relay-do-not-delete
ansible-test-relay-do-not-delete
cb003
cb003
cb004
fb-demo5
frvmt0003036
frvmt0003036
frvmt0003040
frvmt0003299
frvmx0000416
frvmx0000423
frvmx0000434
frvmx0000446
frvmx0000576
frvmx0000576
frvmx0000622
frvmx0000635
frvmx0000654
frvmx0000654
frvmx0000661
frvmx0000661
frvmx0000683
frvmx0000683
frvmx0000685
frvmx0000685
frvmx0000687
frvmx0000687
jmt201
mcds-test-301
mcds-test-301
mcds-test-303
mcds-test-303
rhel6918111243
rhel69611917
rhel7511130951
rhel7511130956
rhel7511161022
vlpchh08
vlpedu03
vlpedu03
vlpijl03
vlplin01

Someone to show me how to do that?

I'm a bit confused with your definition of lines that are differents between the files because the shown expected output looks like lines unique to file1 . If so, please try:

comm -2 -3  <(sort file1) <(sort file2)

Output:

fb-demo5
frvmt0003040
frvmx0000416
frvmx0000423
frvmx0000434
frvmx0000446
frvmx0000622
frvmx0000635
jmt201
rhel6918111243
rhel69611917
rhel7511130951
rhel7511130956
rhel7511161022
vlpchh08
vlpijl03
vlplin01

Instead, if you want to print both "lines unique to file1" and "lines unique to file2", which will be the common interpretation of different lines , please try:

comm -3 --output-delimiter= <(sort file1) <(sort file2)

Output:

FRVMT0003040
cb004
fb-demo5
frvmt0003040
frvmt0003299
frvmx0000416
frvmx0000423
frvmx0000434
frvmx0000446
frvmx0000622
frvmx0000635
jmt201
rhel6918111243
rhel69611917
rhel7511130951
rhel7511130956
rhel7511161022
vlpchh08
vlpijl03
vlplin01

Use grep with -f (list of patterns from file) and -v (inverse match)

grep -v -f file2 file1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM