Comparing two columns in two files and printing a third based on a match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing two columns in two files and printing a third based on a match
# 1  
Old 09-18-2018
Comparing two columns in two files and printing a third based on a match

Hello all, First post here. I did not notice a previous post to help me down the right path. I am looking to compare a column in a CSV file against another file (which is not a column match one for one) but more or less when a match is made, I would like to append a third column that contains a name to the end of the matching row. For instance:


File 1:
Code:
Hostname,Port,Name,Status,VLAN,Duplex,Speed,Type
switch1,gi2/1,trunk,active,1,a-100,full,10g,10GBase-CU 3M
switch1,gi3/1,link to data closet,disabled,2,half,a-1000,No XCVR
etc..

File 2:
Code:
VLAN,Name,Status,Ports,Device
1,default,active,gi1/1,switch5
2,voice,active,gi2/1,switch5
etc...

Desired Output File:


Code:
Hostname,Port,Name,Status,VLAN,Duplex,Speed,Type,VLAN Name
switch1,gi2/1,trunk,active,1,a-100,full,10g,10GBase-CU 3M,default
switch1,gi3/1,link to data closet,disabled,2,half,a-1000,No XCVR,voice


To condense what Im trying to accomplish, I want to create a third file that will have an additional column that appends the vlan name when a match is made. When a match is not made, NA is fine.

Thanks for any help, I've been struggling with SED and AWK
# 2  
Old 09-18-2018
Quote:
Originally Posted by dis0wned
Thanks for any help, I've been struggling with SED and AWK
Welcome.

Please post what you have tried so far.
# 3  
Old 09-18-2018
Most everything that I started with is garbage and I hit a wall when I tried to compare to the second file. My approach was to combine the two files and compare which was a mess and didnt work right. At that point I punted and decided to post.
# 4  
Old 09-18-2018
Quote:
Originally Posted by dis0wned
Most everything that I started with is garbage and I hit a wall when I tried to compare to the second file. My approach was to combine the two files and compare which was a mess and didnt work right. At that point I punted and decided to post.
whare are/is the common key(s) between the 2 files to match on?

I see that Hostname in file1 and Device in file2, refer to 2 diff hosts...
Not sure how you arrived to your desired output, unless I'm missing how to match entries between 2 files (which is probably is true)

Last edited by vgersh99; 09-18-2018 at 01:58 PM..
# 5  
Old 09-18-2018
The key field to match on is the vlan field. For awk in file1 it is $5 and in file2 it is field 1. In the last file I would append it to the end of all matching rows.

------ Post updated at 05:16 PM ------

Code:
awk -F',' 'NR==FNR{vlan[$1]=$1;name[$1]=$2;next}; ($2==vlan[$5]){print $0 "," name[$2]}' <(sort -k1 file2.csv) <(sort -k5 file1.csv) &> file3.csv

This did not work for me,

------ Post updated at 05:16 PM ------

code(
Code:
awk -F',' 'NR==FNR{vlan[$1]=$1;name[$1]=$2;next}; ($2==vlan[$5]){print $0 "," name[$2]}' <(sort -k1 file2.csv) <(sort -k5 file1.csv) &> file3.csv

)

------ Post updated at 05:17 PM ------

Code:
awk -F',' 'NR==FNR{vlan[$1]=$1;name[$1]=$2;next}; ($2==vlan[$5]){print $0 "," name[$2]}' <(sort -k1 file2.csv) <(sort -k5 file1.csv) &> file3.csv


Last edited by Scrutinizer; 09-18-2018 at 02:26 PM..
# 6  
Old 09-18-2018
something along these lines:
Code:
awk -F, 'FNR==NR{if (FNR==1){h=$0}f2[$1]=$2;next} FNR==1{print h, "VLAN Name";next} $5 in f2 {print $0,f2[$5]}' OFS=, file2 file1

# 7  
Old 09-18-2018
Nope. Ultimately I am trying to print file1 to a new file with an additional column (VLAN Name), and the matching vlans have the name field appended to the end of the row. If a match is not made, then NA or a blank space is fine. This awk command appended the "vlan name" to the first line but did not return any of the fields


Code:
$ awk -F, 'FNR==NR{if (FNR==1){h=$0}f2[$1]=$2;next} FNR==1{print h, "VLAN Name";next} $5 in f2 {print $0,f2[$5]}' OFS=, file2 file1
VLAN,Name,Status,Ports,Device,VLAN Name


Last edited by Scrutinizer; 09-18-2018 at 11:42 PM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Data match 2 files based on first 2 columns matching only and join if match

Hi, i have 2 files , the data i need to match is in masterfile and i need to pull out column 3 from master if column 1 and 2 match and output entire row to new file I have tried with join and awk and i keep getting blank outputs or same file is there an easier way than what i am... (4 Replies)
Discussion started by: axis88
4 Replies

2. Shell Programming and Scripting

awk pattern match not printing desired columns

Hi all, I'm trying to match the following two files with the code below: awk -F, 'NR==FNR {a=$0; next} ($12,$4) in a {print $12,$1,a}' OFS="," file4.csv file3.csv but the code does not print the entire row from file4 in addition to column 12 and 1 of file3. file4: o,c,q,co,ov,b... (1 Reply)
Discussion started by: bkane3
1 Replies

3. Shell Programming and Scripting

Comparing Select Columns from two CSV files in UNIX and create a third file based on comparision

Hi , I want to compare first 3 columns of File A and File B and create a new file File C which will have all rows from File B and will include rows that are present in File A and not in File B based on First 3 column comparison. Thanks in advance for your help. File A A,B,C,45,46... (2 Replies)
Discussion started by: ady_koolz
2 Replies

4. Shell Programming and Scripting

Match files based on either of the two columns awk

Dear Shell experts, I have 2 files with structure: File 1: ID and count head test_GI_count1.txt 1000094 2 10039307 1 10039641 1 10047177 11 10047359 1 1008555 2 10120302 1 10120672 13 10121776 1 10121865 32 And 2nd file: head Protein_gi_GeneID_symbol.txt protein_gi GeneID... (11 Replies)
Discussion started by: smitra
11 Replies

5. UNIX for Dummies Questions & Answers

Comparing two test files and printing out the values that do not match

Hi, I have two text files with matching first columns. Some of the values in the second column do not match. I want to write a script to print out the rows (only the first column) where the values in the second column do not match. Example: Input 1 A 1 B 2 C 3 D 4 Input 2 A 2 B 2... (6 Replies)
Discussion started by: evelibertine
6 Replies

6. UNIX for Dummies Questions & Answers

Comparing two text files by a column and printing values that do not match

I have two text files where the first three columns are exactly the same. I want to compare the fourth column of the text files and if the values are different, print that row into a new output file. How do I go about doing that? File 1: 100 rs3794811 0.01 0.3434 100 rs8066551 0.01... (8 Replies)
Discussion started by: evelibertine
8 Replies

7. UNIX for Dummies Questions & Answers

Comparing the 2nd column in two different files and printing corresponding 9th columns in new file

Dear Gurus, I am very new to UNIX. I appreciate your help to manage my files. I have 16 files with equal number of columns in it. Each file has 9 columns separated by space. I need to compare the values in the second column of first file and obtain the corresponding value in the 9th column... (12 Replies)
Discussion started by: Unilearn
12 Replies

8. Shell Programming and Scripting

Comparing two files and printing 2nd column if match found

Hi guys, I'm rather new at using UNIX based systems, and when it comes to scripting etc I'm even newer. I have two files which i need to compare. file1: (some random ID's) 451245 451288 136588 784522 file2: (random ID's + e-mail assigned to ID) 123888 xc@xc.com 451245 ... (21 Replies)
Discussion started by: spirm8
21 Replies

9. Shell Programming and Scripting

printing words based on column match

pls help Input: file1 word1 text1 word2 text2 word3 text3 file2 word1 text11 word3 text13 can u pls help in getting the same output: file1 text1 text2 text3 (1 Reply)
Discussion started by: bha148
1 Replies

10. Shell Programming and Scripting

Comparing Columns and printing the difference from a particular file

Gurus, I have one file which is having multiple columns and also this file is not always contain the exact columns; sometimes it contains 5 columns or 12 columns. Now, I need to find the difference from that particular file. Here is the sample file: param1 | 10 | 20 | 30 | param2 | 10 |... (6 Replies)
Discussion started by: buzzusa
6 Replies
Login or Register to Ask a Question