Matching multiple values in a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching multiple values in a string
# 1  
Old 12-27-2010
Matching multiple values in a string

I've been battling with parsing a comma-delimited string, and have had what I would call B- success. I'm using perl and trying to parse out specific identifiers from a string, into a new string. When things are "normal," my regex works fine. When things get complicated, my script fails miserably. I think a loop is in order, but often find that someone here has a more elegant way to do things.

Specifically, my script has 2 variables that I'm concerned with: $identifier and $key. $key is just an integer - it could be 7, 77, etc. $identifier is a string that contains a number of codes. I'm trying to pull out the codes that match $identifier and concatenate them in a new string.

For example, (this is one of the difficult ones),

$key = 7
$identifier = "CODES: L7,BT7-1,CP87,E7"

The desired result would be: $parsed = "L7,BT7-1,E7" since they are all of the "7" codes. 87 is a separate identifier. If it's just 7s, I'm OK. When there are other numbers containing 7, or they appear at the beginning or end, I have problems.

Any help or suggestions would be greatly appreciated. Just need something to break the mental log jam that is preventing me from seeing the obvious!

Thanks, Doug
# 2  
Old 12-27-2010
May we see the regex you are using?

---------- Post updated at 11:31 AM ---------- Previous update was at 11:24 AM ----------

If you don't mind breaking up the tasks, something like this may work for you:

Code:
sub parse { my $key = shift; my $identifier = shift; my ($list) = $identifier =~ m{^CODES: (.*)}; my @list = (); foreach my $e (split /,/, $list) { push @list, $e if $e =~ m{\D$key(?:-\d+)?}; } return @list if wantarray; return join ',', @list; } my $parsed = parse(7, "CODES: L7,BT7-1,CP87,E7"); $\ = "\n"; print $parsed;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do delete certain lines alone which are matching with start and end string values in file?

Hi, In my previous post ( How to print lines from a files with specific start and end patterns and pick only the last lines? ), i have got a help to get the last select statement from a file, now i need to remove/exclude the output from main file: Input File format: SELECT ABCD, DEFGH,... (2 Replies)
Discussion started by: nani2019
2 Replies

2. UNIX for Beginners Questions & Answers

Concatenate column values when header is Matching from multiple files

there can be n number of columns but the number of columns and header name will remain same in all 3 files. Files are tab Delimited. a.txt Name 9/1 9/2 X 1 7 y 2 8 z 3 9 a 4 10 b 5 11 c 6 12 b.xt Name 9/1 9/2 X 13 19 y 14 20 z 15 21 a 16 22 b 17 23 c 18 24 c.txt Name 9/1 9/2... (14 Replies)
Discussion started by: Nina2910
14 Replies

3. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

4. Shell Programming and Scripting

Problems with Multiple Pattern String Matching

I am facing a problem and I would be grateful if you can help me :wall: I have a list of words like And I have a datafile like the box of the box of tissues out of of tissues out of the book, the the book, the pen and the the pen and the I want to find Patterns of “x.*x” where... (2 Replies)
Discussion started by: A-V
2 Replies

5. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

6. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

7. Shell Programming and Scripting

Search for multiple string and replace with respective values

Hi, Can anyone help me to search for multiple strings within specified position and replace with respective string value. For example I need to search the string from the position 11 to 20 and if it contain ABC and then replace it by BCDEFGHIJ ... find AABZSDJIK and replace with QWE. and... (4 Replies)
Discussion started by: zooby
4 Replies

8. Shell Programming and Scripting

conditional multiple string matching

I need to print fileNames that contains multiple search strings conditionally. Condition: Any file in the folder that has search strings ---> (b1b && d1d) && ( a1a || c1c ). i.e File should have both (b1b AND d1d) AND (either a1a or c1c). Where && is logical AND || is logical OR. ... (10 Replies)
Discussion started by: kchinnam
10 Replies

9. Shell Programming and Scripting

matching multiple values in awk

How will you change the 5th column in the data file with the value in the second column in the error_correction.txt file. You have to match an extra variable, column 3 of the error_correction file with column 6 of the data.txt file. data.txt: vgr,bugatti veron,,3.5,Maybe,6,.......,ax2,....... (0 Replies)
Discussion started by: VGR
0 Replies

10. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies
Login or Register to Ask a Question