Match and copy


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match and copy
# 1  
Old 11-15-2014
Match and copy

Code:
Trying to match $4 in LCH_exons.txt with $1 in refseq_genes.txt, if there is a match then the value in $4 in refseq_exons is copied to $5 of LCH_exons.

For example, $4 in LCH_exons.txt is NRAS and that matches $1 row 11270 and the value in $4 is -, so the - would be copied to LCH_exons.txt in $5.

Code:
 
chr1	115247084	115250671	NRAS
chr1	115250774	115250813	NRAS
chr1	115251151	115251275	NRAS
chr1	115252189	115252349	NRAS
chr1	115256420	115256599	NRAS
chr1	115258670	115258798	NRAS
chr1	115259278	115259515	NRAS

After the match
Code:
chr1	115247084	115250671	NRAS	-
chr1	115250774	115250813	NRAS	-
chr1	115251151	115251275	NRAS	-
chr1	115252189	115252349	NRAS	-
chr1	115256420	115256599	NRAS	-
chr1	115258670	115258798	NRAS	-
chr1	115259278	115259515	NRAS	-

Is this possible? Thank you Smilie.
# 2  
Old 11-15-2014
Please check your sample data, the 1968 lines in LCH_exons.txt is a subset of refseq_exons.txt.
# 3  
Old 11-16-2014
Quote:
Originally Posted by cmccabe
. . . Is this possible? Thank you Smilie.
Almost certainly - provided you get your act together.

Four file names (LCH_exons.txt, refseq_genes.txt, refseq_exons, and LCH_exons) are supplied of which only one matches one in your attached samples, of which, as derekludwig points out, none meets the structure of your second file.

Would you like to receive a proposal of the same quality as the request?
# 4  
Old 11-17-2014
I apologize for the confusion and have attached the proper files.

Code:
Trying to match $4 in LCH_exons.txt with $1 in refseq_genes.txt, if there is a match then the value in $4 in refseq_exons is copied to $5 of LCH_exons.

For example, $4 in LCH_exons.txt is NRAS and that matches $1 row 11270 and the value in $4 is -, so the - would be copied to LCH_exons.txt in $5.

Thank you Smilie.

Maybe:
Code:
 awk 'NR==FNR{A[$1]=$1}A[$5]{sub($5,A[$5]);print}' refseq_genes.txt LCH_exons.txt > output.txt


Last edited by cmccabe; 11-17-2014 at 10:26 AM..
# 5  
Old 11-17-2014
That refseq_genes file is pretty big. Anyhow, adapt your script like (untested)
Code:
 awk 'NR==FNR{A[$1]=$4; next}  A[$4]  {$4=$4 " " A[$4]}1' refseq_genes.txt LCH_exons.txt > output.txt

This User Gave Thanks to RudiC For This Post:
# 6  
Old 11-17-2014
I get this error:

Code:
 
awk: cmd. line:1: NR==FNR{A[$1]=$4; next} A[$4] {$4=$4 " " A[$4])}1
awk: cmd. line:1: ^ syntax error

Thank you Smilie.

---------- Post updated at 12:33 PM ---------- Previous update was at 08:46 AM ----------

Never mind, I must have copied an extra space... it works great. Thank you Smilie
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 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

3. Shell Programming and Scripting

Match and copy by text

In the attached .txt file, I am trying to match $5 with $1 and copy $4 to $7. That file was created using: awk 'BEGIN {OFS="\t"} {sub("chr","",$1)} {print $5,$2,$3,$1,$4,$7} {print $6,$3,$2,$1}' matrix_pxlence.txt > output.txt but I can not seem to add this last bit to the command. ... (1 Reply)
Discussion started by: cmccabe
1 Replies

4. Shell Programming and Scripting

Match text in a range and copy value

In the files attached, I am trying to: if Files.txt $1 is in the range of Exons.txt $1, then in Files.txt $4 the value from Exons.txt $3 is copied else if no match is found Exons.txt $3 = "Intron" For example, the first value in File.txt $1 is chr1:14895-14944 and is not found in any range... (4 Replies)
Discussion started by: cmccabe
4 Replies

5. Shell Programming and Scripting

Match pattern1 in file, match pattern2, substitute value1 in line

not getting anywhere with this an xml file contains multiple clients set up with same tags, different values. I need to parse the file for client foo, and change the value of tag "64bit" from false to true. cat clients.xml <Client type"FIX"> <ClientName>foo</ClientName>... (3 Replies)
Discussion started by: jack.bauer
3 Replies

6. Shell Programming and Scripting

Need Help - match file name and copy to Directory

I am trying to sort the following files from folder Bag to Apple, Cat Food, Dog Food. I can get all of the files I want into a new folder, but not sure of the best approch to get them to their final directory My Files ========== apple.1234.ext apple.1235.ext cat food 101.ext Cat Food... (2 Replies)
Discussion started by: mtschroeder
2 Replies

7. UNIX for Dummies Questions & Answers

Two files; if cells match then copy over other columns

My current issue is dealing with two space delimited files. The first file has column 1 as the sample ID's, then columns 2 - n as the observations. The second file has column 1 as the sample ID's, column 2 as the mother ID's, column 3 as the father ID's, column 4 as the gender, and column 5... (3 Replies)
Discussion started by: Renyulb28
3 Replies

8. Shell Programming and Scripting

Match File and Copy File Script (Homework, Closed)

Can you please help on this? I am looking for the shell script which does following:- step 1: It should open the file /u/manish/input/FileIndex.dat and read line by line step 2: Once first line is read (for ex: File1), we have to find a file, that contains this matching... (4 Replies)
Discussion started by: teteguru1
4 Replies

9. UNIX for Advanced & Expert Users

Match the string and copy records to another location

Hi, I have particular set of files which have the below contents: ****** PBX TYPE:ID6 PBX-id: A11 rolled on 123456 368763 00 >>>>>> A11,2008-07-01 21:31:00.000,42,42112, ,XXXXXXXX A11,2008-07-01 21:40:00.000,6, , ,XXXXXXX A12,2008-07-01 21:53:00.000,68, , ,XXXXXXXX... (12 Replies)
Discussion started by: bsandeep_80
12 Replies

10. Shell Programming and Scripting

Match a pattern and copy above two lines

Dear experts, i want to search pattern tre and copy this line and above two lines in a seperate file:: Thanks for the help SEV="MAJOR": RX-TX HW-FAILURE DOMAIN="alcomc2_BSS_20 unit-type % bts nbr % 24 SBL-type % tre nbr % 4 subnb % 255 BR, Danish (16 Replies)
Discussion started by: Danish Shakil
16 Replies
Login or Register to Ask a Question