Binary pattern matching in UNIX

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Binary pattern matching in UNIX
# 1  
Old 07-21-2016
Binary pattern matching in UNIX

I think what I'm trying to do is pretty straightforward but I just can't find a way to do it.

I'm trying to run a double pattern match in a three column file. If the first two columns match, I need to output the third.


So in the file

Code:
AAA BBB 1
BBC CCC 5
CCC DDD 7
DDD EEE 12

If the pattern match 'BBB' && 'CCC' is run, the output if '5'. Only one line in the file could ever match the precise pattern.

So far straightforward. But if no single line matches the pattern, I need it to output 'NaN' or something to indicate that there is no pattern in the file.

So the output is always a single line, either '5' or 'NaN'.

Conceptually this seems straightforward but I can't get it to work or find an example where you can generate a simple one-line output.

Thank you for any help

Moderator's Comments:
Mod Comment edit by bakunin: Please use CODE-tags also for file content. Thank you.

Last edited by bakunin; 07-21-2016 at 07:11 PM..
# 2  
Old 07-21-2016
Quote:
Originally Posted by matthewndavies1
Conceptually this seems straightforward but I can't get it to work or find an example where you can generate a simple one-line output.
In fact it is quite simple to implement. As i sense you want to do this in sedhere is how you do it:

1 - if a match is found (in any line, including the first) output that third field (or whatever) and immediately exit sed (you can use the "q" command to do so).

2) - for the last line, put a rule as the last into the script of writing "NaN" before quitting from the script. If the script gets thee the last line didn't match either.

Here is the implementation (supposing your desired match is "BBB" and "CCC"):

Code:
sed '/^BBB CCC/ {
         s/^BBB CCC//p
         q
      }
      $$ s/.*/NaN/p' /path/to/input

I hope this helps.

bakunin
# 3  
Old 07-21-2016
Hi bakunin

Thank you so much for your quick reply.

The only thing I don't really understand is how 'q' links the statements. At the moment when I try and implement it I get the error.

Code:
bad flag in substitute command: 'q'

Best wishes

Matthew
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 07-21-2016 at 09:00 PM.. Reason: Add CODE tags.
# 4  
Old 07-21-2016
Quote:
Originally Posted by matthewndavies1
Hi bakunin

Thank you so much for your quick reply.

The only thing I don't really understand is how 'q' links the statements. At the moment when I try and implement it I get the error.

Code:
bad flag in substitute command: 'q'

Best wishes

Matthew
Please show us the exact code you used for this and let us know what operating system and shell you're using.
There was no q flag in the substitute command in the script bakunin suggested. There was, however, a q command on the line after the substitute command.
# 5  
Old 07-22-2016
I'm afraid two small modifications are needed to make Scrutinizer's code run :

- use -n option to suppress printing all lines
- use one single $ only to indicate last line

Code:
sed -n '/^BBB CCC/ {
         s/^BBB CCC//p
         q
      }
      $ s/.*/NaN/p' /path/to/input

And, a little trick might save some typing: reuse the last regex in the first substitute command like s///p.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX command to get the latest file and also matching pattern

we want to fetch the latest file in a given directory and also the file name should match the below pattern Example file name ->hrdata-2015-10-13-16-45-26.xml(2015-10-13-16-45-26- it is not current timestamp, we just need to check for the pattern) We expect the file will have the... (3 Replies)
Discussion started by: vishwanath001
3 Replies

2. Shell Programming and Scripting

UNIX awk pattern matching and printing lines

I have the below plain text file where i have some result, in order to mail that result in html table format I have written the below script and its working well. cat result.txt Page 2015-01-01 2000 Colors 2015-02-01 3000 Landing 2015-03-02 4000 #!/bin/sh LOG=/tmp/maillog.txt... (1 Reply)
Discussion started by: close2jay
1 Replies

3. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

4. Shell Programming and Scripting

pattern matching on any special character in Unix

Hi, I have field in a file which would come with any special character, how do i check that field? Eg: @123TYtaasa>>>/ 131dfetr_~2 In the above example, how do I add pattern for any special character on the keyboard. Thanks (3 Replies)
Discussion started by: techmoris
3 Replies

5. Shell Programming and Scripting

Issue with Pattern Matching in Unix

Hi, I am trying to replace a specific column values in a csv file with double quotes. Example: SNO,NAME,ZIPCODE,RANK,CARE_OF 1,Robert,74538,12,RICHARD JOHNSON, P.C 2,Sam,07564,13,% R.S MIKE, V.K.S 3,Kim, Ed,12345,14,@90 KMS, %TK Desired Output: SNO,NAME,ZIPCODE,RANK,CARE_OF... (1 Reply)
Discussion started by: techmoris
1 Replies

6. Shell Programming and Scripting

Perl Pattern Matching:Unix Vs. Window (Is it OS dependent??)

Hi All, Below I am discussing the problem I am facing while using pattern matching in Unix & Window. Plz hv a look into it. ================ my $s="UPDATE A SET s="klkkk' ;" ; if ( $s =~ m/^*UPDATE+/i ) { print $s; } else { print "no match";} =================== Both should... (6 Replies)
Discussion started by: Niroj
6 Replies

7. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies

8. UNIX for Advanced & Expert Users

pattern matching in unix

Task is to identify files like code.1 , code.23 and so on ... (the files which are ending with a number) but it should not match files like code.123abc. So the search will normally search for files with "code." and at the end we should extract for the correct match. Now I have to remove these files... (6 Replies)
Discussion started by: diwakar_reddy
6 Replies

9. Shell Programming and Scripting

Pattern Matching problem in UNIX

Hello All, I need help I have a problem in searching the pattern in a file let us say the file contains the below lines line 1 USING *'/FILE/FOLDER/RETURN') ................. ................. line 4 USING *'/FILE/FOLDER/6kdat1') line 5 USING... (2 Replies)
Discussion started by: maxmave
2 Replies

10. Shell Programming and Scripting

Pattern Matching in UNIX

Hello, I have a pattern like "XXXXXX XXXXXX" which i need to make search in a input file and Replace the matched pattern to a another pattern. This is the code i tried .. #!/usr/bin/perl print "Enter a File name :"; chomp ($file = <STDIN>); print "\n Searching file :"; if (system ("ls... (2 Replies)
Discussion started by: maxmave
2 Replies
Login or Register to Ask a Question