Repeating groups problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Repeating groups problem
# 1  
Old 11-03-2008
Repeating groups problem

Hi

I am trying to locate all strings seperated by the string -CR-, the string will have an unknown number of these -CR- strings, I've used the regex:

^(?:(.*?)-CR-)+$


As shown in the test code:

my ($myString)="This is the first line-CR-second line-CR-third line-CR-fourth line-CR-fifth line-CR-";
if ($myString =~ /^(?:(.*?)-CR-)+$/)
{
print "String1 $1 String2 $2 String3 $3 String4 $4 String5 $5\n";
}

However I only ever get the last line "fifth line" which goes to $1, it would seem that only $1 is ever set, and keeps getting overwritten. for example if I replace:

if ($myString =~ /^(?:(.*?)-CR-)+$/)


with:

if ($myString =~ /^(?:(.*?)-CR-){7,}$/)

As I expected I get no output as there only five repeats possible. If I change the 7 to number 5 or less than I again get the same last line found in $1, anyone got any ideas?

Thanks

Jon
# 2  
Old 11-03-2008
Hi,
I have a different idea using cut.

> echo $myString
This is the first line-CR-second line-CR-third line-CR-fourth line-CR-fifth line-CR-

> echo $myString | sed 's/-CR-/:/g' | cut -d':' -f1-
This is the first line:second line:third line:fourth line:fifth line:

or you could get individual parts using f1,f2 etc....
i replaced the -CR- with : as a delimiter, because cut somehow refuses to accepts -CR- as a delimiter...
# 3  
Old 11-04-2008
Thanks for your reply, unfortunately it doesn't help me, as I don't have a choice as to the delimiter, and I also have to do everything using a regex. It's a "very limited" programming language that uses perl regex,but has non of the other useful facilities.
# 4  
Old 11-04-2008
BYTW I think if the - had been escaped then it would be accepted with cut.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

repeating test several times

echo "Enter Number of times u want this to be run" read RUN for i in {1..$RUN} do echo "i am going to market" done now the issue is that when i run this script it ask me "Enter Number of times u want this to be run" and then i put the value ,say i put 10, but when the script runs it gives... (1 Reply)
Discussion started by: bharat8007
1 Replies

2. UNIX for Advanced & Expert Users

prtdiag -v problem :Memory Module Groups status not Showing

Hi Friends, I need a help from you all. In my machine which is on Solaris 9. the command prtdiag -v shows the complete output but it doesn't show "Memory Module Groups status" status. I have tried restarting the picl daemon, but still it doesn't work. Memory Module Groups:... (2 Replies)
Discussion started by: vivek.goel.piet
2 Replies

3. UNIX for Dummies Questions & Answers

Remove groups of repeating lines

I know uniq exists, but am not sure how to remove repeating lines when they are groups of two different lines repeating themselves, without using sort. I need them to be sorted in the original order, just to remove repeats. cd /media/AUDIO/WAVE/9780743518673/mp3 ~/Desktop/mp3-to-m4b... (1 Reply)
Discussion started by: glev2005
1 Replies

4. Shell Programming and Scripting

Repeating loop between dates

Hi , I need to execute set of commands between two parameterized dates. Suppose, If parameter1 is Feb1st-2010 and parameter2 is November15th-2010. I need to execute a set of commands within these dates . can any one help me to build a loop so that it should execute for 28days in February... (4 Replies)
Discussion started by: Raamc
4 Replies

5. Shell Programming and Scripting

Repeating Substitution Command on VI

Hello Folks, how to write a command on vi that allow to repeat last substitution command? Here what I want to do : 1 2 3 1 2 3 1 2 3 :.,+2s/\n/ /And I obtain : 1 2 3 1 2 3 1 (5 Replies)
Discussion started by: gogol_bordello
5 Replies

6. Shell Programming and Scripting

Problem with Sed when repeating characters

Hi all, I'm learning sed (and regular expressions) - My first little program is to replace 3 numbers in a row with 'XXX' This is what I am trying: echo '511' | sed 's/{3}/XXX/' Here is the output: defunct-macbook-pro:~ defunct$ echo '511' | sed 's/{3}/XXX/' 511For some reason, it doesnt... (2 Replies)
Discussion started by: Defunct
2 Replies

7. Shell Programming and Scripting

Repeating awk command

Hi all, I have an awk command that needs to be ran multiple times in a script on one file containing lots of fields of data. The file look like this (the numbers are made up): 1234 2222 2223 2222 123 2223 3333 2323 3333 3321 3344 4444 The... (2 Replies)
Discussion started by: nistleloy
2 Replies

8. Shell Programming and Scripting

Value repeating problem in columns

Hi, I have a file like this 0817 0201364 1 866 . . . . . . . 574 . 100.0 100.0 5529737 1 TV 0817 0201364 2 1440 . . . . . . . . . . . 5529737 1 TV 0817 0201364 6 1323 . . . . ... (2 Replies)
Discussion started by: Sandeep_Malik
2 Replies

9. Shell Programming and Scripting

Repeating commands in a script

I have written a script that I want to be repeated. The script that I wrote outputs the date, how many people are on the system, how many people logged in before me, and how many people logged in after me. Than what I want it to do is after it outputs the 4 lines I want it to go back to the... (4 Replies)
Discussion started by: Dave2874
4 Replies

10. UNIX for Dummies Questions & Answers

Omit repeating lines

Can someone help me with the following 2 objectives? 1) The following command is just an example. It gets a list of all print jobs. From there I am trying to extract the printer name. It works with the following command: lpstat -W "completed" -o | awk -F- '{ print $1}' Problem is, I want... (6 Replies)
Discussion started by: TheCrunge
6 Replies
Login or Register to Ask a Question