Matched a pattern from multiple columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matched a pattern from multiple columns
# 8  
Old 02-07-2014
Quote:
Originally Posted by redse171
Hi Akhsay,

Your code does work for the first condition that i mentioned above. But if i choose $2 and $3 to have "M" at the same time, it didnt work. ok, i will give another example here:-

Code:
ID                 Pat1    Pat2    Pro1     use1 

add11              M        M        
add17              M                M        M
add32              M        M        
add66              M        M        
add55              M        M       M       
add47                       M       M

If i choose $2 and $3, the results should be
Code:
add11
add32
add66

thanks.
Why not ?

Code:
add11
add32
add66
add55

post output of cat -A from your new input.
# 9  
Old 02-07-2014
I just want "M" that exist in 2 columns not more or less than that. add55 has M in column 4, which is not what i want. thanks
# 10  
Old 02-08-2014
Quote:
Originally Posted by redse171
I just want "M" that exist in 2 columns not more or less than that. add55 has M in column 4, which is not what i want. thanks
Code:
awk -v cols='2 3' -f red.awk myFile

red.awk:
Code:
BEGIN {
  FS="\t"
  if (!cols) cols="2 3 4 5"
  if (!val) val="M"
  nc=split(cols, colsA, " ")
}
{
  p=0
  for(i=1;i<=nc;i++)
    ($colsA[i]==val)?p++:p--
  if (p==nc && p+1==NF) print $1
}

if it still doesn't work, please provide the output of cat -vet myFile using the code tags.
# 11  
Old 02-08-2014
Hi,

i guess it is the problem with FS. Below is cat results:-

Code:
ID                                 use1                           P00dfd|Pat_2          P00dddf|PRO1                        P00ddff|Pat1          $
Add7G00710                                                                                               M                                                                     $
Add6G11920                         M                                                                     M                                                                     $
Add6G00630                                                                                               M                                                                     $
Add5G10140                                                                                               M                                                                     $
Add4G00470                                                                                               M                                                                     $
Add3G02060                                                                                               M                                                                     $
Add2G17360                                                            M                                  M                                                                     $
Add2G12550                                                                                               M                                                                     $
Add2G05350                         M                                  M                                                                                                        $
Add6G00710                                                                                               M                                                                     $
Add5G01350                                                                                               M                                                                     $
Add4G14230                                                                                               M                                                                     $
Add4G06080                         M                                  M                                  M                                  M                                  $
Add4G03920                                                                                               M                                                                     $
Add3G11760                                                                                               M                                                                     $
Add3G02890                                                                                               M                                                                     $
Add1G11830                                                            M                                  M                                  M                                  $
Add2G12710                                                                                               M                                                                     $
Add5G13290                         M                                  M                                                                     $

Actually my data was in vertical. I used an awk script to make it horizontal so that it is easier for me to see where "M" is. But, when i do column -t to clean it, the alignment goes haywire. I am still trying to figure out how to deal with this. If you have suggestion, pls let me know. Thanks
# 12  
Old 02-08-2014
I don't know what you mean by vertical vs horizontal, but what you quoted is not TAB separated fields, but rather multi-space separated fields.
Pls post the original file before any transformations.
# 13  
Old 02-08-2014
Hi,

a sample of original data as follows:-

Code:
Add7G00710	213	235	P00dddf|PRO1	M
Add6G11920    	65	511	use1		M                                                                     
Add6G11920     	133	149	P00dddf|PRO1    M                                                                                                                                          
Add6G00630     	78	510	P00dddf|PRO1    M                                                                     
Add5G10140     	93	539	P00dddf|PRO1    M                                                                     
Add4G00470     	10	471	P00dddf|PRO1    M
Add4G00470     	109	351	P00dddf|PRO1    M                                                                     
Add3G02060     	1	299	P00dddf|PRO1    M
Add2G17360     	1	116	P00dfd|Pat_2    M                                                         
Add2G17360     	27	478	P00dddf|PRO1    M                                                                     
Add2G12550     	5	22	P00dddf|PRO1    M                                                                     
Add2G05350     	1	450	use1            M        
Add2G05350     88	624	P00dfd|Pat_2    M                                                                                                        
Add6G00710     11	447	P00dddf|PRO1    M                                                                     
Add5G01350     149	164	P00dddf|PRO1    M                                                                     
Add4G14230     11	447	P00dddf|PRO1    M                                                                     
Add4G06080     132	157	use1            M                                                                  
Add4G06080     113	514	P00dfd|Pat_2    M 
Add4G06080     24	481	P00dddf|PRO1    M 
Add4G06080     320	337	P00ddff|Pat1    M 
Add4G03920     75	507	P00dddf|PRO1    M                                                                     
Add3G11760     27	530	P00dddf|PRO1    M                                                                     
Add3G02890     19	473	P00dddf|PRO1    M                                                                     
Add1G11830     36	472	P00dddf|PRO1    M  
Add1G11830     66	561	P00ddff|Pat1    M     
Add1G11830     27	536	P00ddfd|Pat_2   M                                  
Add2G12710     23	543	P00dddf|PRO1    M                                                                     
Add5G13290     5	22	use1            M  
Add5G13290     37	54	P00dfd|Pat_2	M

There are duplicates as they are different in $2 and $3. But, whichever duplicates, i just take one. for example, Add4G00470 in $1 has appeared twice due to value in $2 and $3. Since they are in the same group (in $4), i take it as 1 only to my output. Thanks.

Last edited by redse171; 02-08-2014 at 01:32 PM.. Reason: additional notes
# 14  
Old 02-08-2014
Please note that your latest sample differs considerably from your samples in posts #1, #7 and #11 and these also differ from one another. Is this the correct sample this time?

*snip*

Last edited by Scrutinizer; 02-08-2014 at 02:23 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare two files when pattern matched

I have two files say FILE1 and FILE2. FILE1 contains 80,000 filename in sorted order and another file FILE2 contains 6,000 filenames is also in sorted order. I want to compare the filename for each file and copy them in to a folder when filename is matched. File1.txt contain 80,000... (8 Replies)
Discussion started by: imranrasheedamu
8 Replies

2. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

3. UNIX for Advanced & Expert Users

To print from the first line until pattern is matched

Hi I want to print the line until pattern is matched. I am using below code: sed -n '1,/pattern / p' file It is working fine for me , but its not working for exact match. sed -n '1,/^LAC$/ p' file Input: LACC FEGHRA 0 LACC FACAF 0 LACC DARA 0 LACC TALAC 0 LAC ILACTC 0... (8 Replies)
Discussion started by: Abhisrajput
8 Replies

4. Shell Programming and Scripting

Putting together substrings if pattern is matched

What I would like to do is if the lines with % have the same name, then combine the last 9 letters of the string underneath the last occurrence of that ID with the first 9 letters of the string underneath the first occurrence of that ID. I have a file that looks like this: %GOGG... (12 Replies)
Discussion started by: verse123
12 Replies

5. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

6. UNIX for Dummies Questions & Answers

How convert space separated list to matched columns?

Hi I have been racking my (limited) brains to get this to work without success I have a file output which is a list of lists - ie a single column of data that is separated by space into sub lists below - I need to both split this so that each list is in a separate column (eg tab or semicolon... (8 Replies)
Discussion started by: Manchesterpaul
8 Replies

7. Shell Programming and Scripting

Insert certain field of matched pattern line above pattern

Hello every, I am stuck in a problem. I have file like this. I want to add the fifth field of the match pattern line above the lines starting with "# @D". The delimiter is "|" eg > # @D0.00016870300|0.05501020000|12876|12934|3||Qp||Pleistocene||"3 Qp Pleistocene"|Q # @P... (5 Replies)
Discussion started by: jyu3
5 Replies

8. Shell Programming and Scripting

removing lines around a matched pattern

I have an ugly conf file that has the string I'm interested in searching for in the middle of a block of code that's relevant, and I'm trying to find a way to remove that entire block based on the matched line. I've googled for this problem, and most people helping are only interested in... (9 Replies)
Discussion started by: tamale
9 Replies

9. Shell Programming and Scripting

print last matched pattern using perl

Hi, If there exist multiple pattern in a file, how can I find the last record matching the pattern through perl. The below script searches for the pattern everywhere in an input file. #! /usr/bin/perl -s -wnl BEGIN { $pattern or warn"Usage: $0 -pattern='RE' \n" and exit 255;... (5 Replies)
Discussion started by: er_ashu
5 Replies

10. Shell Programming and Scripting

Replacing a word after a matched pattern

Hello, Actually i want to replace the word after a matched pattern. For Ex: lets say that i am reading a file line by line while read line do echo $line # i need to search whether a pattern exists in the file and replace the word after if the pattern exist. # for example :... (1 Reply)
Discussion started by: maxmave
1 Replies
Login or Register to Ask a Question