Match based on criteria to file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match based on criteria to file
# 1  
Old 05-13-2015
Match based on criteria to file

Trying to match $1 of target.txt to $5 of file.txt. If there is a match then in an output.txt file $1,$1 (row underneath),$6,$4,$7 from file.txt are printed on the same line as $1 of target.txt. The input is from excel and the output should be tab-deliminated. Thank you Smilie.

target.txt
Code:
PXL-A0000005
PXL-A0000007
PXL-A0000014

file.txt
Code:
RefPrimer	ref	antiref	omosome	PrimerSet	SeqRxn	Link 
AntirefPrimer	antiref	ref	omosome			 
PXL-A0000005ref	69066	69311	1	PXL-A0000005	SeqRxn4	 https://www.xxxx.com/add_2_cart.php?catno=PXL-A0000005&storage=lyophilized&rxns=100&num=1&test=mixed&format=tube
PXL-A0000005antiref	69311	69066	1			 
PXL-A0000007ref	69700	70033	1	PXL-A0000007	SeqRxn4	 https://www.xxxx.com/add_2_cart.php?catno=PXL-A0000007&storage=lyophilized&rxns=100&num=1&test=mixed&format=tube
PXL-A0000007antiref	70033	69700	1			 
PXL-A0000014ref	861093	861418	1	PXL-A0000014	SeqRxn4	 https://www.xxxx.com/add_2_cart.php?catno=PXL-A0000014&storage=lyophilized&rxns=100&num=1&test=mixed&format=tube
PXL-A0000014antiref	861418	861093	1

Desired output
Code:
PrimerSet	RefPrimer	AntirefPrimer	SeqRxn	Chr	Link
PXL-A0000005 PXL-A0000005ref PXL-A0000005antiref SeqRxn4 1 https://www.xxxx.com/add_2_cart.php?catno=PXL-A0000005&storage=lyophilized&rxns=100&num=1&test=mixed&format=tube
PXL-A0000007 PXL-A0000007ref PXL-A0000007antiref SeqRxn4 1 https://www.xxxx.com/add_2_cart.php?catno=PXL-A0000007&storage=lyophilized&rxns=100&num=1&test=mixed&format=tube
PXL-A0000014 PXL-A0000014ref PXL-A0000014antiref SeqRxn4 1 https://www.xxxx.com/add_2_cart.php?catno=PXL-A0000014&storage=lyophilized&rxns=100&num=1&test=mixed&format=tube

Code:
 awk '$1 = $5  && $1 != "" {print $1,$1,$6,$4,$7}' FS="\t" OFS="," target.txt file.txt . output.txt

# 2  
Old 05-13-2015
A Perl solution
Code:
perl -ne 'BEGIN{open $refs ,"<","target.txt";@refs=<$refs>;chomp(@refs);$regex=join"|",@refs;}
@r=split/\s+/,$_;if ($r[4]=~/^$regex$/ ){$next = readline; @n=split/\s+/,$next;print join "\t",$r[4],$r[0],$n[0],@r[5,3,6],"\n"}' file.txt
PXL-A0000005    PXL-A0000005ref    PXL-A0000005antiref    SeqRxn4    1    https://www.xxxx.com/add_2_cart.php?catno=PXL-A0000005&storage=lyophilized&rxns=100&num=1&test=mixed&format=tube    
PXL-A0000007    PXL-A0000007ref    PXL-A0000007antiref    SeqRxn4    1    https://www.xxxx.com/add_2_cart.php?catno=PXL-A0000007&storage=lyophilized&rxns=100&num=1&test=mixed&format=tube    
PXL-A0000014    PXL-A0000014ref    PXL-A0000014antiref    SeqRxn4    1    https://www.xxxx.com/add_2_cart.php?catno=PXL-A0000014&storage=lyophilized&rxns=100&num=1&test=mixed&format=tube

This User Gave Thanks to Skrynesaver For This Post:
# 3  
Old 05-15-2015
Thank you Smilie.... works great
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove lines from File.A based on criteria in File.B

Hello, I have two files of the following form. I would like to remove from File.A where the first three colum matches values in File.B to give the output in File.C File.A 121 54321 PQR CAT 122 765431 ABC DOG 124 98765 ZXY TIGER 125 86432 GEF LION File.B 122 765431 ABC 125 86432 GEF... (4 Replies)
Discussion started by: Gussifinknottle
4 Replies

2. Shell Programming and Scripting

Awk/sed/cut to filter out records from a file based on criteria

I have two files and would need to filter out records based on certain criteria, these column are of variable lengths, but the lengths are uniform throughout all the records of the file. I have shown a sample of three records below. Line 1-9 is the item number "0227546_1" in the case of the first... (15 Replies)
Discussion started by: MIA651
15 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 print specific line in file based on criteria

In the file below I am trying to extract a specific instance of path, if the adjacent plugin": "/rundb/api/v1/plugin/49/. Thank you :). file "path": "/results/analysis/output/Home/Auto_user_S5-00580-4-Medexome_65_028/plugin_out/FileExporter_out.52", "plugin": "/rundb/api/v1/plugin/49/",... (8 Replies)
Discussion started by: cmccabe
8 Replies

5. Shell Programming and Scripting

Copying section of file based on search criteria

Hi Guru's, I am new to unix scripting. I have a huge file with user details in it(file2) and I have another file with a list of users(file1). Script has to search a user from file1 and get all the associated lines from file2. Example: fiel1: cn=abc cn=DEF cn=xyx File 2: dn:... (10 Replies)
Discussion started by: Samingla
10 Replies

6. Shell Programming and Scripting

Select lines from a file based on a criteria

Hi I need to select lines from a txt file, I have got a line starting with ZMIO:MSISDN= and after a few line I have another line starting with 'MOBILE STATION ISDN NUMBER' and another one starting with 'VLR-ADDRESS' I need to copy these three lines as three different columns in a separate... (3 Replies)
Discussion started by: Tlcm sam
3 Replies

7. Shell Programming and Scripting

Extract error records based on specific criteria from Unix file

Hi, I look for a awk one liner for below issue. input file ABC 1234 abc 12345 ABC 4567 678 XYZ xyz ght 678 ABC 787 yyuu ABC 789 7890 777 zxr hyip hyu mno uii 678 776 ABC ty7 888 All lines should be started with ABC as first field. If a record has another value for 1st... (7 Replies)
Discussion started by: ratheesh2011
7 Replies

8. Shell Programming and Scripting

Print lines that match certain criteria

Hi all I have a text file with the following format: id col1 col2 col3 col4 col5 col6 col7 ... row1 0 0 0 0 0 0 0 row2 0 0 0 0 0 0 0 row3 0 0 0 0 0 0.2 0 row4 0 0 0 0 0 0 0 row5 0 0 0 0 0 0 0 row6 0 0 0 0.1 0 0 0 row7 0 0 0 0 0 0 0 row8 0 0 0 0 0 0 0 row9 0 0 0 0 0 0 0 ... The file... (2 Replies)
Discussion started by: gautig
2 Replies

9. Shell Programming and Scripting

split large file based on field criteria

I have a file containing date/time sorted data of the form ... 2009/06/10,20:59:59.950,XAG/USD,Q,1,1115, 14.3025,100,1,1 2009/06/10,20:59:59.950,XAG/USD,Q,1,1116, 14.3026,125,1,1 2009/06/10,20:59:59.950,XAG/USD,R,0,0, , 0,0,0 2009/06/10,20:59:59.950,XAG/USD,R,1,0, 14.1910,100,1,1... (6 Replies)
Discussion started by: asriva
6 Replies

10. UNIX for Advanced & Expert Users

need to get a portion of entries in file based on a criteria --- Help please

All, Below is the file, what i need to do is take the text in between the /*-- and --*/ , i mean the jobs. Then i have grep for system name . If the job is there in system 1 i have to print to a file. Basically i want to take all the jobs that are in system1 to another file . because... (7 Replies)
Discussion started by: arunkumar_mca
7 Replies
Login or Register to Ask a Question