Specific row match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Specific row match
# 1  
Old 12-27-2014
Specific row match

Trying to match $1 of genematch.txt with $11 of design.txt, if there is a match then in the first row of the match $1 is copied to column 1,
row 2 of design.txt, $2 is copied to column 2, row 2 of design.txt, $3 is copied to column 3, row 2 of design.txt, $4 is copied to column 4, row 2 of
design.txt, and $5 is copied to column 5, row 2 of design.txt.

For example, SKI is the first name in genematch.txt and that matches row 1595 in design.txt
so in column 1, row 1595 $1, in column 2 row 1595 $2, in column 3 row 1595 $3, in column 4 row 1595 $4, in column 5 row 1595 $5.

The input and output is tab-delimited.

Code:
awk 'NR==FNR{A[$1]=$1; next}  A[$11]  {$11=$11 " " A[$11]}1' genematch.txt design.txt OFS="t" > output.txt

Thank you Smilie.
Moderator's Comments:
Mod Comment As I said before: CODE tags are needed around sample input, output, and code; not around descriptive text explaining what the tagged segments contain.

Last edited by Don Cragun; 12-27-2014 at 01:54 PM.. Reason: Remove extraneous CODE tags.
# 2  
Old 12-27-2014
Sorry, I can't figure out what you request. What is "row 2 of design.txt" when the match is in row 1595, and you print every single line according to your code snippet?

BTW, the OFS assignment must come before the input file that produces output, and the <TAB> char can be constructed with "\t" in some awks.
# 3  
Old 12-29-2014
I apologize, only the row in which the match is found does the new text need to be copied.
So in the example, SKI is the first name in genematch.txt and that matches row 1595 in design.txt so in column 1, row 1595 $1, in column 2 row 1595 $2, in column 3 row 1595 $3, in column 4 row 1595 $4, in column 5 row 1595 $5.

The additional columns in design.txt that have information in them just need to be copied. I am basically trying to add text to fields in which there is a match. Thank you Smilie.

Maybe:
Code:
 awk 'NR==FNR{a[$0]; next} {for(x in a) {if($0 ~ x) {print $1, $2, $3, $4, $5, x}}}' FS="\t" genematch.txt design.txt > output.txt

# 4  
Old 12-29-2014
Like so:
Code:
awk     'NR==FNR        {T2[$1]=$2
                         T3[$1]=$3
                         T4[$1]=$4
                         T5[$1]=$5
                         next}
         $11 in T2      {$1=   $11
                         $2=T2[$11]
                         $3=T3[$11]
                         $4=T4[$11]
                         $5=T5[$11]
                        }
         1
        ' FS="\t" OFS="\t" /tmp/genematch.txt /tmp/design.txt

This User Gave Thanks to RudiC For This Post:
# 5  
Old 12-29-2014
Thank you Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to change a specific column and in a specific row

I am trying to change the number in bold to 2400 01,000300032,193631306,190619,0640,1,80,,2/ 02,193631306,000300032,1,190618,0640,CAD,2/ I'm not sure if sed or awk is the answer. I was going to use sed and do a character count up to that point, but that column directly before 0640 might... (8 Replies)
Discussion started by: juggernautjoee
8 Replies

2. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

This is a question that is related to one I had last August when I was trying to sort/merge two files by millsecond time column (in this case column 6). The script (below) that helped me last august by RudiC solved the puzzle of sorting/merging two files by time, except it gets lost when the... (0 Replies)
Discussion started by: aachave1
0 Replies

3. Shell Programming and Scripting

awk to update specific value in file with match and add +1 to specific digit

I am trying to use awk to match the NM_ in file with $1 of id which is tab-delimited. The NM_ will always be in the line of file that starts with > and be after the second _. When there is a match between each NM_ and id, then the value of $2 in id is substituted or used to update the NM_. Each NM_... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

My original files are like this below and I distinguish them from the AP_ID (file1 has 572 and file2 has 544). Also, the header on file1 has “G_” pre-pended. NOTE: these are only snippets of very large files and much of the data is not present here. Original File 1: ... (36 Replies)
Discussion started by: aachave1
36 Replies

5. Shell Programming and Scripting

Search row by row from one file to another file if match is found print few colums of file 2

this is the requirement list.txt table1 table2 table3 testfile.txt name#place#data#select * from table1 name2#place2#data2#select * from table 10 innerjoin table3 name2#place2#data2#select * from table 10 output name place table1 name2 place table3 i tried using awk (7 Replies)
Discussion started by: vamsekumar
7 Replies

6. Shell Programming and Scripting

Grepping a specific row from a file

Hi I have output of a command saved in a file.. # cat /file.txt System: cu=4 ent=0.1 mode=on cu min u s w i 0 500 0.1 0.3 0.5 0.1 1 200 0.5 0.2 0.3 0.0 By using ksh, what I need to do is, I need to grep the u,s,w and i... (5 Replies)
Discussion started by: Priya Amaresh
5 Replies

7. Shell Programming and Scripting

delete a row with a specific value at a certain column

Hi, I want to delete rows that have 0 at column 4. The file looks like this: chr01 13 61 2 chr01 65 153 0 chr01 157 309 1 chr01 313 309 0 chr01 317 469 1 chr01 473 557 0 I want to delete all rows with a 0 at column 4 chr01 13 61 2 chr01 157 309 1 chr01 ... (3 Replies)
Discussion started by: kylle345
3 Replies

8. Shell Programming and Scripting

Identifying specific fields in a Row

Hi, I am new to UNIX. Can some one help me to solve the below. I have a requirement to to identify the specific fields in row and also some part of the field. In my file I have a record as sundra;10.44.48.65;10thstreet TCP packet out of state: First packet isn't SYN;telno:... (3 Replies)
Discussion started by: suneel.mekala
3 Replies

9. UNIX for Dummies Questions & Answers

return a specific row using awk

Hello, I am using awk to process a file, and need to return a row that meets specific criteria. awk 'BEGIN{sets variables} {processes file, updates variables} END{need to print a row that meets the criteria in one of the variables} I have tried code in the END block like {print NR==var}... (1 Reply)
Discussion started by: badPuppy
1 Replies

10. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies
Login or Register to Ask a Question