Thursday, December 23, 2010

Sorting contents of two files in Linux

You got 2 files which contains a list of servernames (as shown below) and want to compare the entries between two files and get the list of server names which aren’t present in both files:

1st file:
server_a
server_b
server_c
server_d
server_e

2nd file:
server_c
server_d
server_g
server_a

Output should be:

server_b
server_g
server_e



Solution 1:  # sort file1 file2 | uniq –u

Solution 2:

# sort file1 > file1.sorted
# sort file2 > file2.sorted
# comm -3 file1.sorted file2.sorted

No comments:

Post a Comment

Popular Posts

About Me

My photo
The intent of this blog is to share my work experience and spread some smart solutions on Linux to System Administrators. I'm hoping the solutions shared in this Blog would be helpful and come as a handy for Viewers. Brief about me: I have 18+ years work experience in System and Cloud Administration domain, primarily works on VMware Cloud Products (vSphere, vCloud Director, vRealize Automation, NSX Adv. Load Balancer, vROps).