Need to weed out a list of servers from a list of servers with unix commands

I have a list of about 400 development servers and a list of about 1500 servers which include those servers and the rest of out unix environment. What is a good way to remove the servers in the first list from the second and create a new file with only those servers? The lists are line separated - eg:

server1
server2
server3
LVL 1
ThaidogAsked:
Who is Participating?
 
woolmilkporcConnect With a Mentor Commented:
Or like this:

sort development_servers all_servers |uniq -u > non_development_servers
0
 
Gerwin Jansen, EE MVEConnect With a Mentor Topic Advisor Commented:
There is an AWK trick to do this, put the servers you want to filter in filter.txt and the 1500 servers in servers.txt
awk 'NR==FNR{arr[$0];next}!($0 in arr)' filter.txt servers.txt >newfile.txt 

Open in new window

Sample:

$ cat filter.txt
server2
server3
$ cat servers.txt
server1
server2
server3
server4
server6
$ cat new_file.txt
server1
server4
server6
0
 
woolmilkporcConnect With a Mentor Commented:
sort development_servers > development_servers.sorted
sort all_servers > all_servers.sorted
comm -1 -3 development_servers.sorted all_servers.sorted  > non_development_servers
rm development_servers.sorted all_servers.sorted
0
 
Gerwin Jansen, EE MVEConnect With a Mentor Topic Advisor Commented:
Or like this:

grep -v -f filter.txt servers.txt
0
 
Gerwin Jansen, EE MVEConnect With a Mentor Topic Advisor Commented:
grep -v -f filter.txt servers.txt > new_file.txt
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.