[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 422
  • Last Modified:

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
0
Thaidog
Asked:
Thaidog
  • 3
  • 2
5 Solutions
 
Gerwin Jansen, EE MVETopic 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
 
woolmilkporcCommented:
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 MVETopic Advisor Commented:
Or like this:

grep -v -f filter.txt servers.txt
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
grep -v -f filter.txt servers.txt > new_file.txt
0
 
woolmilkporcCommented:
Or like this:

sort development_servers all_servers |uniq -u > non_development_servers
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now