Sponsored Content
Top Forums Shell Programming and Scripting Print matching fields (if they exist) from two text files Post 302988528 by gacanepa on Tuesday 27th of December 2016 02:57:03 PM
Old 12-27-2016
Tools Print matching fields (if they exist) from two text files

Hi everyone,
Given two files (test1 and test2) with the following contents:
test1:
Code:
80263760,I71
80267369,M44
80274628,L77
80276793,I32
80277390,K05
80277391,I06
80279206,I43
80279859,K37
80279866,K35
80279867,J16
80280346,I14

and test2:
Code:
80263760,PT18
80279867,PT01

I need to do some kind of join to obtain this result:
Code:
80263760,PT18
80267369,M44
80274628,L77
80276793,I32
80277390,K05
80277391,I06
80279206,I43
80279859,K37
80279866,K35
80279867,PT01
80280346,I14

In short:
  1. Join on 1st field of both files.
  2. If a value in field #1 exists in test2, replace field #2 in test1 with field #2 in test2.
  3. Else, leave field #2 in test1 intact.
I tried with the following join command:
Code:
join -t, -1 1 -2 1 -o 1.1 2.2 -a 1 -a 2 test1 test2

but it only complies with requirement 2 above and leaves non-matching fields empty:
Code:
80263760,PT18
80267369,
80274628,
80276793,
80277390,
80277391,
80279206,
80279859,
80279866,
80279867,PT01
80280346,

Any ideas will be more than welcome.
Thanks in advance.
This User Gave Thanks to gacanepa For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

AWK Matching Fields and Combining Files

Hello! I am writing a program to run through two large lists of data (~300,000 rows), find where rows in one file match another, and combine them based on matching fields. Due to the large file sizes, I'm guessing AWK will be the most efficient way to do this. Overall, the input and output I'm... (5 Replies)
Discussion started by: Michelangelo
5 Replies

2. Shell Programming and Scripting

comparing two files for matching fields

I am newbie to unix and would please like some help to solve the task below I have two files, file_a.text and file_b.text that I want to evaluate. file_a.text 1698.74 1711.88 6576.25 899.41 3205.63 4187.98 697.35 1551.83 ... (3 Replies)
Discussion started by: gameli
3 Replies

3. Shell Programming and Scripting

Matching multiple fields from two files and then some?

Hi, I am working with two tab-delimited files with multiple columns, formatted as follows: File 1: >chrom 1 100 A G 20 …(10 columns) >chrom 1 104 G C 18 …(10 columns) >chrom 2 28 T C ... (4 Replies)
Discussion started by: mbp
4 Replies

4. UNIX for Advanced & Expert Users

awk print all fields except matching regex

grep -v will exclude matching lines, but I want something that will print all lines but exclude a matching field. The pattern that I want excluded is '/mnt/svn' If there is a better solution than awk I am happy to hear about it, but I would like to see this done in awk as well. I know I can... (11 Replies)
Discussion started by: glev2005
11 Replies

5. Shell Programming and Scripting

How to merge two or more fields from two different files where there is non matching column?

Hi, Please excuse for often requesting queries and making R&D, I am trying to work out a possibility where i have two files field separated by pipe and another file containing only one field where there is no matching columns, Could you please advise how to merge two files. $more... (3 Replies)
Discussion started by: karthikram
3 Replies

6. Shell Programming and Scripting

awk to combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

7. UNIX for Beginners Questions & Answers

Awk: matching multiple fields between 2 files

Hi, I have 2 tab-delimited input files as follows. file1.tab: green A apple red B apple file2.tab: apple - A;Z Objective: Return $1 of file1 if, . $1 of file2 matches $3 of file1 and, . any single element (separated by ";") in $3 of file2 is present in $2 of file1 In order to... (3 Replies)
Discussion started by: beca123456
3 Replies

8. Shell Programming and Scripting

awk to print fields that match using conditions and a default value for non-matching in two files

Trying to use awk to match the contents of each line in file1 with $5 in file2. Both files are tab-delimited and there may be a space or special character in the name being matched in file2, for example in file1 the name is BRCA1 but in file2 the name is BRCA 1 or in file1 name is BCR but in file2... (6 Replies)
Discussion started by: cmccabe
6 Replies

9. UNIX for Beginners Questions & Answers

Matching fields between two files, repeated records

In two previous posts (here) and (here), I received help from forum members comparing multiple fields across two files and selectively printing portions of each as output based upon would-be matches using awk. I had been fairly comfortable populating awk arrays with fields and using awk's special... (3 Replies)
Discussion started by: jvoot
3 Replies

10. Shell Programming and Scripting

Comparing two files by two matching fields

Long time listener first time poster. Hope someone can advise. I have two files, 1000+ lines in each, two fields in each file. After performing a sort, what is the best way to find exact matches where field $1 and $2 in file1 are also present in file2 on the same line, then output only those... (6 Replies)
Discussion started by: bstaff
6 Replies
ECACCESS-FILE-MOVE(1p)					User Contributed Perl Documentation				    ECACCESS-FILE-MOVE(1p)

NAME
ecaccess-file-move - Move or Rename ECaccess Files SYNOPSIS
ecaccess-file-move -version|-help|-manual ecaccess-file-move [-debug] source-ecaccess-file target-ecaccess-file DESCRIPTION
Allow moving an ECaccess File. When the move occurs within the same ECaccess File System then it is a rename. When the move occurs across two different ECaccess File Systems then first a copy is done and then a delete of the original file is done (e.g. similar to ecaccess- file-copy ... -erase). The source-ecaccess-file and target-ecaccess-file are in the form [domain:][/user-id/]path. Please read the "Shell commands -> File Management" section of the "ecaccess" guide for more information on the ECaccess File System. ARGUMENTS
source-ecaccess-file The source ECaccess File name. target-ecaccess-file The target ECaccess File name. OPTIONS
-version Display version number and exits. -help Print a brief help message and exits. -manual Prints the manual page and exits. -debug Display the SOAP messages exchanged. EXAMPLES
ecaccess-file-move ec:test1.txt ec:test2.txt Rename the test1.txt file in the ECFS directory of the authenticated user in test2.txt. ecaccess-file-move ec:test1.txt home:test2.txt Copy the test1.txt file from the ECFS directory of the authenticated user to the test2.txt file in the $HOME directory of the authenticated user, then delete the test1.txt file from ECFS. SEE ALSO
ecaccess-file-delete, ecaccess-file-get, ecaccess-file-mget, ecaccess-file-modtime, ecaccess-file-mput, ecaccess-file-rmdir, ecaccess-file- copy, ecaccess-file-dir, ecaccess-file-mdelete, ecaccess-file-mkdir, ecaccess-file-chmod, ecaccess-file-put, ecaccess-file-size and ecaccess. perl v5.14.2 2012-04-16 ECACCESS-FILE-MOVE(1p)
All times are GMT -4. The time now is 08:18 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy