Selecting nearest pattern match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Selecting nearest pattern match
# 1  
Old 03-18-2015
Selecting nearest pattern match

I'm looking to match an error code against a list of possible codes and get the nearest match. The code would be a 6 character hexadecimal string.

I have a file of error codes all of which have a specific first 3 characters, however, after that the last 3 characters may be specific or generic as below.

Code:
2120xx
2180xy
2182xy
2193xy
2194xy
21A3xy
21A6xx
21A7xx
21Bxyz
21Cxyz
21D0xy
3073xy
3075xy
30A100
3A0xyy
43Bxxx
43Cyxx
453yxx
463yxx
47Dxxx
47E5xx
47E700
47EC00
BF1x1x
BF2x1x

y and x can be any hex character.

I need to be able to match up to the correct code and the only way I have come up with is rather convoluted. It would involve splitting the original error code into 4 parts, 1st 3 chars then char4, char5 and char6. I would then run a while loop that would read in each error code from the file, split it into the same 4 parts and run comparisons as follows,

Do 1st 3 chars match, if so does char4 match or is it x or y, if so does char5 match or is it x or y, if do does char6 match or is it x or y. If all match then that's the line I need.

I haven't worked out all the detail of that solution but I know I can do it that way. I also know there has to be a better way of doing it.

Can anyone help?

Last edited by Scrutinizer; 03-19-2015 at 07:15 AM.. Reason: code tags
# 2  
Old 03-18-2015
Have you tried using character classes in regular expression?
# 3  
Old 03-18-2015
This one returns the longest match.
It first builds a hashed array s[] with all their shorter variants.
Then it tries to match the full given search, then the shorter variants.
Code:
awk '{
  for (i=6; i>0; i--) s[substr($0,1,i)]
}
END {
  for (i=6; i>0; i--) if ((x=substr(search,1,i)) in s) {print x; exit}
}' search=BF2XXX file-of-error-codes

Code:
BF2

These 2 Users Gave Thanks to MadeInGermany For This Post:
# 4  
Old 03-19-2015
Thank you very much MadeInGermany. That does exactly what I needed and I sort of understand how it works as well. :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Selecting section and removing match

I have a file with contents as shown in file.texi Would like to keep only the sections that have inlineifset till the empty line is reached. Finally replace the following string with a space @inlineifset{mrg, @opar{@bullet{} I had written the following command but it messed my file ... (6 Replies)
Discussion started by: Danette
6 Replies

2. Shell Programming and Scripting

Comparing two one-line files and selecting what does not match

I have two files. One is consisting of one line, with data separated by spaces and each number appearing only once. The other is consisting of one column and multiple lines which can have some numbers appearing more than once. It looks something like this: file 1: 20 700 15 30 file2: 10... (10 Replies)
Discussion started by: maya3
10 Replies

3. Shell Programming and Scripting

Match and Grep the nearest value in last field

Gents I have this input file file1 (uniq records) 54503207851 170211240 54503207911 170210837 54503208111 170215105 54503208112 170215210 54655210011 170223140 54655210091 170223738 54655210172 170224355 54655210251 170224741 54655210331 170225039 54655210411 170225505 54655210492... (13 Replies)
Discussion started by: jiam912
13 Replies

4. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

5. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

6. Shell Programming and Scripting

AWK Match to nearest number

Hello Guys, I'm very new on here and require some help matching up and printing some columns using awk. I have two text files. The first file has Longitude data in column 1 (lon.txt) and the second one (node.txt) has again another Longitude data in column 1 (not exact as the first one) + in... (7 Replies)
Discussion started by: ian_gooch
7 Replies

7. Shell Programming and Scripting

Selecting a part of the text (regex pattern, awk, sed)

Hello, let's start by giving you guys a few examples of the text: "READ /TEXT123/ABC123" "READ /TEXT123/ABC123/" "READ TEXT123/ABC123" "READ TEXT123/ABC123/" "READ TEXT123/TEXT456/ABC123" "READ /TEXT123/TEXT456/ABC123" "READ /TEXT123/TEXT456/ABC123/" TEXT and ABC can be and I... (5 Replies)
Discussion started by: TehOne
5 Replies

8. UNIX for Advanced & Expert Users

sed match closest/nearest pattern

All i am struggling to raplace some text in a line between two (closest) patterns , line="/home/usr/bin/:/home/usr/devuser,n1.9/bin:/home/usr/root/bin" i want to replace "devuser,n1.9" with "NEWVAL", basically all teh text from "devuser" until nearest '/' with some new text. i tried teh... (1 Reply)
Discussion started by: sudheer1984
1 Replies

9. UNIX for Dummies Questions & Answers

match nearest

Hi, I'm trying to find the nearest match between two columns of numbers, e.g. 1,1 10,8 30,50 20,100 and the search could be e.g. 20,20 returning 10,8 - i.e. 20-10 = 10 and 20-8 = 12 totalling 22, and hence being the nearest match. any ideas? thanks a lot, (1 Reply)
Discussion started by: bogu0001
1 Replies

10. Shell Programming and Scripting

selecting only few lines from many based on a common pattern

Hi, I have a file having some thousand records with the following sort of lines: Error: Failed to get order data Order: PO-BBBTGZE Error: No CLI Error: Failed to get order data Order: PO-SBDJUZA Order: PO-XBBIDEN Error: No CLI Error: Failed to get order data Order: PO-BBDJUTQ Order:... (2 Replies)
Discussion started by: damansingh
2 Replies
Login or Register to Ask a Question