Awk Help - duplicates in $1 that match x & y in $2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk Help - duplicates in $1 that match x & y in $2
# 1  
Old 12-22-2011
Awk Help - duplicates in $1 that match x & y in $2

I'm primarily a "Windows" systems administrator whose been getting his toes in the Linux waters. I am new to programming and advanced scripting so please bear with me and my incomplete example below.

I have exported all entries from our DNS zones. I used sed to remove everything other than the fqdn from the files. We have a few hundred hosts across a few dozen zones. Some hosts are not in all zones though.

I would like to use something like awk to determine which hosts are (or are not) in the domains I specify. Bonus might also be to accept input from a file (with the various domain names) but the simpler the better.

the file looks something like this:

host1.domain1.com
host1.domain2.com
host1.domain3.com
host1.domain4.com
host2.domain1.com
host2.domain2.com
host2.domain3.com
host2.domain4.com

and so on. And again, some hosts are not in all zones.

I would like to do something like:
Code:
awk -F"." '($2 == "domain2" && $2 == "domain3") {print $1}' filename

to figure out which hosts are (or maybe, are not) in the zones specified.

Obviously this script is incomplete. I can't figure out how to find duplicates from $1 and then search $2 for domain2 and domain3.
I want all duplicate (or triplicate or so on) hosts from $1 that match a few possible domain inputs from $2 to return, and nothing else. Since I may have the same host returned per domain a few times, if the result could be truncated to just the host once, even better.

I am not necessarily looking to be spoon fed a few completed examples though I wouldn't object. If you have a few ideas for me, I'd appreciate being pointed closer in the right direction.
# 2  
Old 12-22-2011
Something like this?
Code:
awk -F"." '$2 ~ "domain2|domain2" {a[$1]} END{for(i in a)print i}' input_file

#or

awk -F"." '$2 ~ "domain2|domain3" {print $1}' input_file | sort | uniq

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 12-22-2011
Quote:
Originally Posted by ahamed101
Something like this?
Code:
awk -F"." '$2 ~ "domain2|domain2" {a[$1]} END{for(i in a)print i}' input_file

#or

awk -F"." '$2 ~ "domain2|domain3" {print $1}' input_file | sort | uniq

--ahamed
Genius!
The second one did exactly what I was looking for as it sorted and summarized more cleanly. This was a lot simpler looking than I imagined. I appreciate your help and I will be able to modify this for use in other ways so I appreciate it very much!

---------- Post updated at 11:51 AM ---------- Previous update was at 11:25 AM ----------

Oh, one thing I also need to be able to do is use && for $2, not just |. So I would like to be able to filter by host1.temp.local host1.temp2.org etc etc.
That way if I get the request, which servers are in these four domains, I can use the &&, specify the domains, and get a list of the servers.
With the | I may get servers in some but not all of the specified domains. Thanks so far!

Last edited by Omaplata; 12-22-2011 at 01:58 PM..
# 4  
Old 12-22-2011
Quote:
Originally Posted by Omaplata
Oh, one thing I also need to be able to do is use && for $2, not just |. So I would like to be able to filter by host1.temp.local host1.temp2.org etc etc.
That way if I get the request, which servers are in these four domains, I can use the &&, specify the domains, and get a list of the servers.
With the | I may get servers in some but not all of the specified domains. Thanks so far!
I didn't understand what your second requirement was.

btw, you can use awk ... | sort -u instead of awk ... | sort | uniq

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

Find duplicates in 2 & 3rd column and their ID

with below given format, I have been trying to find out all IDs for those entries with duplicate names in 2nd and 3rd columns and their count like how many time duplication happened for any name if any, 0.237788 Aaban Aahva 0.291066 Aabheer Aahlaad 0.845814 Aabid Aahan 0.152208 Aadam... (6 Replies)
Discussion started by: busyboy
6 Replies

3. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

awk to match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

Match the value & print lines from the match

Hello, I have a file contains two columns. I need to print the lines after “xxx” so i'm trying to match "xxx" & cut the lines after that. I'm trying with the grep & cut command, if there any simple way to extract this please help me. Sample file : name id AAA 123 AAB 124 AAC 125... (4 Replies)
Discussion started by: Shenbaga.d
4 Replies

6. Shell Programming and Scripting

Match a char with duplicates in a line and replace one of them

Hi, i have a huge file that need to check for a pattern that occur more than once in a line like below:- #lkk>cd-m>A0DV0>192.134.1.1 blablabladsdjsk jshdfskfslfs #lqk>cd-m>A1SV0>192.14.11.1 blalalbnalablab balablablajakjakjakja #pldqw>sf-w>PH67FR>168.55.1.1 balablabala... (5 Replies)
Discussion started by: redse171
5 Replies

7. Shell Programming and Scripting

awk help: Match data fields from 2 files & output results from both into 1 file

I need to take 2 input files and create 1 output based on matches from each file. I am looking to match field #1 in both files (Userid) and create an output file that will be a combination of fields from both file1 and file2 if there are any differences in the fields 2,3,4,5,or 6. Below is an... (5 Replies)
Discussion started by: ambroze
5 Replies

8. Shell Programming and Scripting

sed & remove duplicates on output

sed -e '1d' -e 's/^\(]\{2\}\)-\(]\{3\}\)-\(]\{4\}\).*/"0000020\1\200\3"\,/g' abc.txt This script returns many duplicates due to the duplciates in the .txt file. i.e. ... "000002012149000060", "000002012149000064", "000002012149000064", "000002012149000064", "000002012149000064",... (9 Replies)
Discussion started by: Daniel Gate
9 Replies

9. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

10. Shell Programming and Scripting

Match a Pattern & Replace The value Using AWK

I have a csv file in which i have to search a particular string and replace the data in any column with something else. How do i do it using awk. file ------ 2001,John,USA,MN,20101001,29091.50,M,Active,Y 2002,Mike,USA,NY,20090130,342.00,M,Pending,N... (3 Replies)
Discussion started by: Sheel
3 Replies
Login or Register to Ask a Question