Matching column then append to existing File as new column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching column then append to existing File as new column
# 1  
Old 08-10-2016
Matching column then append to existing File as new column

Good evening

I have the below requirements, as I am not an experts in Linux/Unix and am looking for your ideas how I can do this.

I have file called file1 and file2.

I need to get the second column which is text1_random_alphabets and find that in file 2, if it's exists then print the 3rd column which is 300 in file 1 as 4th column.
Code:
Sample file1
ID KEY Time
01 text1_random_alphabets  400
02 text2_random_alphabets2 350
03 text3_random_alphabets3  1000

Sample file2:
ID KEY Time
01 text1_random_alphabets  300
02 text2_random_alphabets2 250
03 text3_random_alphabets3  400
09 textn_random_alphabets33  400


Desired Output

01 text1_random_alphabets  400 300
02 text2_random_alphabets2 350 250
03 text3_random_alphabets3  1000 400

I have read the file1 and in a loop and then for each line grep it in file2 then create a new file with 3rd column. But that seems to be lot of parsing and number of execution would be more as files contains more than 250000 records.

I am sure it would be better solutions than I have, Is there any other solutions/approach which I should be implement?. Appreciate your time.
# 2  
Old 08-10-2016
Try:

Code:
awk 'NR==FNR&&NR>1{A[$2]=$NF;next};($2 in A){print $0,A[$2]}' file2 file1

This User Gave Thanks to pilnet101 For This Post:
# 3  
Old 08-10-2016
Excellent. It works beautifully Smilie

Thank you

Could you please help me to understand this code.
# 4  
Old 08-11-2016
Code:
awk 'NR==FNR&&NR>1  # Whilst the condition NR==FNR is true (which it will only be for the first file being read) and NR is greater than 1 
{A[$2]=$NF;next};   # Build an array where the index is the 2nd field and the value is the last field, stop processing and go to the next record.
($2 in A)           # If the 2nd field being read is present in the array called A
{print $0,A[$2]}'   # Print the line ($0), field seperator (,) and the value of A[$2]
file2 file1         # Read file2 first and then file1

This User Gave Thanks to pilnet101 For This Post:
# 5  
Old 08-11-2016
Thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare 1st column from 2 file and if match print line from 1st file and append column 7 from 2nd

hi I have 2 file with more than 10 columns for both 1st file apple,0,0,0...... orange,1,2,3..... mango,2,4,5..... 2nd file apple,2,3,4,5,6,7... orange,2,3,4,5,6,8... watermerlon,2,3,4,5,6,abc... mango,5,6,7,4,6,def.... (1 Reply)
Discussion started by: tententen
1 Replies

2. Shell Programming and Scripting

awk script to append suffix to column when column has duplicated values

Please help me to get required output for both scenario 1 and scenario 2 and need separate code for both scenario 1 and scenario 2 Scenario 1 i need to do below changes only when column1 is CR and column3 has duplicates rows/values. This inputfile can contain 100 of this duplicated rows of... (1 Reply)
Discussion started by: as7951
1 Replies

3. Shell Programming and Scripting

awk to Sum columns when other column has duplicates and append one column value to another with Care

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (1 Reply)
Discussion started by: as7951
1 Replies

4. Shell Programming and Scripting

Matching column value from 2 different file using awk and append value from different column

Hi, I have 2 csv files. a.csv HUAWEI,20LMG011_DEKET_1296_RTN-980_IDU-1-11-ISV3-1(to LAMONGAN_M),East_Java,20LMG011_DEKET_1296_RTN-980_IDU-1,20LMG011,20LMG 027_1287_LAMONGAN_RTN980_IDU1,20LMG027,1+1(HSB),195.675,20LMG011-20LMG027,99.9995,202.6952012... (7 Replies)
Discussion started by: tententen
7 Replies

5. Shell Programming and Scripting

Increment existing column in file

Hi All, I have a file with 3 millions records in which 3rd column is same throughout say its value is 0 throughout.for example: Col1 Col2 Col3 Col4 A 1 0 5 B 2 0 6 C 3 0 7 D 4 0 9 I want my output as : Col1 Col2 Col3 Col4 A 1 ... (4 Replies)
Discussion started by: Pinky456
4 Replies

6. Shell Programming and Scripting

Find lines with matching column 1 value, retain only the one with highest value in column 2

I have a file like: I would like to find lines lines with duplicate values in column 1, and retain only one based on two conditions: 1) keep line with highest value in column 3, 2) if column 3 values are equal, retain the line with the highest value in column 4. Desired output: I was able to... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

7. Ubuntu

How to add a data column in existing file

Hi All I need to add a column on my existing data file. I know similar posts are there but none of them were meeting my requirement. My input is 1.20 3.44 4.88 5.11 4.99 3.22 1.89 3.89 2.90 Desired output 1 1.20 3.44 4.88 2 5.11 4.99 3.22 3 1.89 3.89 2.90 I will... (2 Replies)
Discussion started by: mahbub03
2 Replies

8. Shell Programming and Scripting

Adding a new column in a file with other existing columns

Hi All , Kindly help me with this soln awk '{printf "%s %7s \n", $1,$c}' infile where value of variable c I am externally giving input But executing the above command shows all the columns of infile where as I want only 1st column of infile and 2nd column should print value c (8 Replies)
Discussion started by: Pratik4891
8 Replies

9. Shell Programming and Scripting

How to rewrite a existing value in a column inside a file?

I am having 4 field in a file name age date status i want to update or rewrite a value of status with another value how it can be done i used awk & sed but it shows result but not updating in original file help me out... Thanks (4 Replies)
Discussion started by: ragavendar
4 Replies

10. Shell Programming and Scripting

how to add a new column in an existing file

Hi guys, Please help me if u have some solution. I have a file with three columns separated by ':' - INPUT_FILE C416722_2 : calin Dirigent : Dirigent AC4174_6 : Jac : cal_co TC4260_5 : [no : lin kite BC426302_1 : [no : calin Dirigent lin JC426540_3 : lin Pymo_bin : calin TC428_3 : no7... (4 Replies)
Discussion started by: sam_2921
4 Replies
Login or Register to Ask a Question