
Karazam
User
Mar 8, 2011, 8:52 AM
Post #4 of 17
(13656 views)
|
Re: [rfransix] Matching Unique Field with Action
[In reply to]
|
Can't Post
|
|
That's odd, when I run it it takes care of the Jr., and III., cases. Not the P.J., or A.B.C. cases though (they didn't appear in your example data). I made some changes to cover more cases, it looks like this in a script:
#!/usr/bin/perl use warnings; use strict; while (<DATA>) { s/ (\w+,) # one or more word characters follow by a comma, captured into $1 \s+ # one ore more white spaces (?: # non-capturing group \w # word character \.? # 0 or 1 period )+ # one or more of the "word character-maybe period" pattern [,.]+ # one or more periods or commas \s+ # one or more white spaces (.*) # everything else, captured into $2 /$1 $2/x; # matches in first and second capturing group print; } __DATA__ Thomas, A. Alexandria S. Perl Programmer Las Vegas NV. Williams, Jr., Michael A. C Programmer New York N.Y. Silver, Susan Java Programmer San Jose CA Altips, Alvin C. Tcl Programmer Chicago IL. Doe, III., John B. Lisp Programmer San Francisco CA Smith, P.J., Jessica Ruby Programmer Las Vegas NV. Norris, A.B.C. Chuck Python Programmer Chicago IL. The oneliner version would be
perl -pi.orig -e "s/(\w+,)\s+(?:\w\.?)+[,.]+\s+(.*)/$1 $2/;" in.file Unless I'm missing something obvious it should work. If it doesn't, could you please attach your original data file? Might be something in there that's different from the examples.
|