Match patterns from another file and tag


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Match patterns from another file and tag
# 1  
Old 01-05-2015
Match patterns from another file and tag

Hi all,

I have a file , which has 6 tab delimited fields, with $3 and $4 subfielded with spaces. I wamt to match cols $2,$3,$4 of tmp1 with tmp2, ..and then flag the 5th col if found.

tmp1
Code:
1756    Xerm    XermA XermB XermC XermD AA TT AA GG     A       1
1763    Xerm    XermA XermB XermC XermD AA TT AA GG     A       1
18078   Xerm    XermA XermB XermC XermD AA TT AA GG     A       1
18115   Xerm    XermA XermB XermC XermD GG AA AA GG     B       2
18251   Xerm    XermA XermB XermC XermD GG AA AA GG     B       2
19352-005       Xerm    XermA XermB XermC XermD AA TT AA GG     A       1
19352-006       Xerm    XermA XermB XermC XermD AA TT AA GG     A       1
195A    Xerm    XermA XermB XermC XermD AA TT AA GG     A       1
A02     Xerm    XermA XermB XermC XermD GG TT GG GG     C       3
A04     Xerm    XermA XermB XermC XermD GG TT AA GG     D       4

tmp2

Code:
Xerm    XermA XermB XermC XermD AA TT AA GG



Expected output

Code:
1756    Xerm    XermA XermB XermC XermD AA TT AA GG     *A*       1
1763    Xerm    XermA XermB XermC XermD AA TT AA GG     *A*       1
18078   Xerm    XermA XermB XermC XermD AA TT AA GG     *A*       1
18115   Xerm    XermA XermB XermC XermD GG AA AA GG     B       2
18251   Xerm    XermA XermB XermC XermD GG AA AA GG     B       2
19352-005       Xerm    XermA XermB XermC XermD AA TT AA GG     *A*       1
19352-006       Xerm    XermA XermB XermC XermD AA TT AA GG     *A*      1
195A    Xerm    XermA XermB XermC XermD AA TT AA GG     *A*       1
A02     Xerm    XermA XermB XermC XermD GG TT GG GG     C       3
A04     Xerm    XermA XermB XermC XermD GG TT AA GG     D       4


Here is what I tried, not producing desired output

Code:
awk -F'\t' 'NR==FNR{a[$0]=$0;next} $2"\t"$3"\t"$4 in a {$5="*"$5"*"}1' OFS="\t"  tmp2 tmp1

# 2  
Old 01-05-2015
Do you really have <TAB> delimiters in both files? For your one line tmp2 file try
Code:
awk     'FNR == NR              {P=$0; next}
         P == $2"\t"$3"\t"$4    {$5="*"$5"*"}
         1
        ' FS="\t" OFS="\t" file2 file1

This User Gave Thanks to RudiC For This Post:
# 3  
Old 01-05-2015
Thanks Rudy, you are right.. there are some space and tab issues with tmp2 original file... to get around this is there a way to match the strings without spaces and tabs?
# 4  
Old 01-05-2015
With spaces inside your fields there's no chance to have spaces as delimiters and tell one field from the other reliably. What you could do is replace all whitespace in tmp2 by a regex that would allow you to match the relevant data in tmp1. Try
Code:
awk     'FNR == NR              {gsub (/[ \t]+/, "[ \t]+"); P=$0; next}              
        $2"\t"$3"\t"$4 ~ P      {$5="*"$5"*"}
         1
        ' FS="\t" OFS="\t" file2 file1

If you have multiple lines in tmp2 and read that file into an array a, you need to run a for (i in a) loop and check matches in that loop.
This User Gave Thanks to RudiC For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to match file1 and extract specific tag values

File2 is tab-delimeted and I am trying to use $2 in file1 (space delimeted) as a search term in file2. If it is found then the AF= in and the FDP= values from file2 are extracted and printed next to the file1 line. I commented the awk before I added the lines in bold the current output resulted. I... (7 Replies)
Discussion started by: cmccabe
7 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

Egrep patterns in a file and limit number of matches to print for each pattern match

Hi I need to egrep patterns in a file and limit number of matches to print for each matched pattern. -m10 option is not working out in my sun solaris 5.10 Please guide me the options to achieve. if i do head -10 , i wont be getting all pattern match results as output since for a... (10 Replies)
Discussion started by: ananan
10 Replies

4. Shell Programming and Scripting

Match 2 patterns together

How can I quickly print out lines in a datafile which has presence of both patterns in a row of another file. Maybe awk can do it much faster than bash. Patternfile ID1 PAT11 PAT12 ID1 PAT21 PAT22 ID2 PAT31 PAT32 datafile headerline... (2 Replies)
Discussion started by: abh.kumar
2 Replies

5. Shell Programming and Scripting

Match 2 different patterns and print the lines

Hi, i have been trying to extract multiple lines based on two different patterns as below:- file1 @jkm|kdo|aas012|192.2.3.1 blablbalablablkabblablabla sjfdsakfjladfjefhaghfagfkafagkjsghfalhfk fhajkhfadjkhfalhflaffajkgfajkghfajkhgfkf jahfjkhflkhalfdhfwearhahfl @jkm|sdf|wud08q|168.2.1.3... (8 Replies)
Discussion started by: redse171
8 Replies

6. Shell Programming and Scripting

Match multiple patterns in a file and then print their respective next line

Dear all, I need to search multiple patterns and then I need to print their respective next lines. For an example, in the below table, I will look for 3 different patterns : 1) # ATC_Codes: 2) # Generic_Name: 3) # Drug_Target_1_Gene_Name: #BEGIN_DRUGCARD DB00001 # AHFS_Codes:... (3 Replies)
Discussion started by: AshwaniSharma09
3 Replies

7. Shell Programming and Scripting

script to match patterns in 2 different files.

I am new to shell scripting and need some help. I googled, but couldn't find a similar scenario. Basically, I need to rename a datafile. This is the scenario - I have a file, readonly.txt that has 2 columns - file# and name. I have another file,missing_files.txt that has id and name. Both the... (3 Replies)
Discussion started by: mathews
3 Replies

8. Shell Programming and Scripting

Removing file lines that each match to a different patterns

I have a very large file (10,000,000 lines), that contains a sample id and a property of that sample. I have another file that contains around 1,000,000 lines with sample ids that I want to remove from the original file (create a new file without these lines). I know how to do this in Perl, but it... (9 Replies)
Discussion started by: Jo_puzzled
9 Replies

9. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

10. Shell Programming and Scripting

sed/awk help to match list of patterns and remove from org file

Hi, From the pattern mentioned below remove lines based on pattern range. Conditions 1 Look For all lines starting with ALTER TABLE and Ending with ; and contains the word MOVE.I wanto to remove these lines from the file sample below. Note : The above pattern list could be found in... (1 Reply)
Discussion started by: rajan_san
1 Replies
Login or Register to Ask a Question