Matched a pattern from multiple columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matched a pattern from multiple columns
# 1  
Old 02-07-2014
Matched a pattern from multiple columns

Hi,

I need to extract an info in $1 based on a matched pattern in $2,$3,$4, and $5.
The sample input file as follows:-

Code:
ID                 Pat1    Pat2    Pro1     use1 

add41              M                 M        M
add87              M        M        M        M
add32                       M        M        M
add66              M        
add55              M        M        M        M

The output should be:-
Code:
add87
add55

My input file is tab-delimited and i tried coding using awk:-

Code:
awk 'x[$2,$3,$4,$5]++' FS=="\t" inputfile

but it gives me the whole ID with 'M'. I just need those that have M in all columns (2,3,4,5). Appreciate your kind help. Thanks
# 2  
Old 02-07-2014
to start with:
Code:
awk 'NF==5 {print $1}' myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 02-07-2014
Hi vgersh99,

Your code does work. thanks so much!
But i have another issue. If i want to get output on a specified columns, how should i do it? if i set the NF==2, i will get the result for any 2 columns that have "M" but i would not know which columns they are. unless i print the whole thing and check which columns. But, this going to make me check all of them and i have huge files to work on. Therefore, i need to set the specific columns. Let say, i want to get ID which $2 and $3 have "M". How should i do it? thanks
# 4  
Old 02-07-2014
Code:
awk -f red.awk myFile
OR
awk -v cols='2 3' -f  red.awk myFile

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


Last edited by vgersh99; 02-07-2014 at 01:01 PM.. Reason: added NF==5
# 5  
Old 02-07-2014
Hi vgersh99,

i tried your above codes, but the result is wrong. if possible, can you please explain to me the coding?
also, just for clarification, for the example that i gave earlier ($2 and $3), it means that i want to extract ID in condition where both columns has "M" at the same time. i might need to extract ID where $2 and $5 have "M" at the same time too. thanks
# 6  
Old 02-07-2014
Could this help you ?

Code:
$ cat file
ID                 Pat1    Pat2    Pro1     use1 

add41              M                 M        M
add87              M        M        M        M
add32                       M        M        M
add66              M        
add55              M        M        M        M

Code:
awk 'NR==1{
           n=split(col,A," ")
          }
          {
           flag=0
           for(i=1;i<=n;i++)
           if($A[i]==key)++flag
           if(flag==n)print $1
          }' col='2 3 4 5' key='M' file

Resulting
Code:
add87
add55

# 7  
Old 02-07-2014
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.
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