MLUG: Re: [MLUG] String manipulation in C
Re: [MLUG] String manipulation in C
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
On Sun, 1 Jul 2007, Mike Miller wrote:

On Sun, 1 Jul 2007, Mike Miller wrote:

perl -pi -e 's/^(.*?)ABC.*ABC(.*?)$/$1ABC$2/' file*.txt


If you just wanted the lengths of the two strings you could do this:

perl -pi -e 's/^(.*?)ABC.*ABC(.*?)$/length ($1) ." ABC ". length ($2)/e' file*.txt


One more:

perl -pi -e 's/(.*?)ABC(.*?)/length ($1) ." ABC ". $2/eg ; s/ ([^ ]+)$/" ". length ($1). "\n"/e' file*.txt

Testing:

echo 'asdfasdABCasdfasdfasdfABCasdfasdfasdfasdfasdfABCasdfasdfasdf' | perl -pe 's/(.*?)ABC(.*?)/length ($1) ." ABC ". $2/eg ; s/ ([^ ]+)$/" ". length ($1). "\n"/e'
7 ABC 12 ABC 20 ABC 13

That replaces all non-matching sequences with their lengths. I used 'asdf' because it is easy to type and you can still readily see the ABCs. With consecutive ABC strings it gives '0', as it should.

I know you want to do this in C, and I'm not trying to change your mind. I just wanted to try a few tricks to see what I might have done. I don't do C programming but I am into learning perl and regexes, though I'm certainly very far from having mastered them. In fact, I'll bet both Jon and Stephen would have better ways of doing what I did above.

Mike

_______________________________________________
members mailing list
EMAIL:PROTECTED
http://mlug.missouri.edu/mailman/listinfo/members