Matched a pattern from multiple columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matched a pattern from multiple columns
# 15  
Old 02-08-2014
Hi Scrutinizer,

Sorry for the confusion. The first sample (#1) is just a toy example. When things didnt work out, then i gave the real sample post (#7 and #11). However, since there is issue with field separator, vgersh99 asked for the original file. Therefore, the latest data is the original data before being transformed into #7 or #11 as per request. Thanks

Moderator's Comments:
Mod Comment OK Noted. Thanks for the explanation

Last edited by Scrutinizer; 02-08-2014 at 03:24 PM.. Reason: duplicates
# 16  
Old 02-10-2014
Hi,
From what I understand, you want to print first column whenever there are exactly two "M"s in 2nd, 3rd, 4th and 5th columns combined. If that is the case (and file is tab delimited), I would create a korn shell script as below and name it as script.ksh :

Code:
#!/bin/ksh

infile=$1

nawk -F"        " <${infile} \
     'BEGIN{cnt=0}           \
      NR>=2{hdr=$1; if ( $2 == "M" ) cnt++;
                    if ( $3 == "M" ) cnt++;
                    if ( $4 == "M" ) cnt++;
                    if ( $5 == "M" ) cnt++;
                    if ( cnt == 2 ) print hdr; cnt=0}'

Then I execute this script as below:

script.ksh infile
where infile is the tab delimited file of interest.
Hope this helps Smilie
This User Gave Thanks to juzz4fun For This Post:
# 17  
Old 02-10-2014
Hi juzz4fun,

thanks so much for the codes. Smilie I have been thinking about the FS issue. I have decided to work on the original file post #13 (before transformation) to get what i wanted instead of input file in post #11. It is much easier i guess as the original file is tab delimeted. 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