Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: How can I replace the pattern in the 6 th field?

by jwkrahn (Monsignor)
on Jun 18, 2018 at 18:23 UTC ( #1216876=note: print w/replies, xml ) Need Help??


in reply to How can I replace the pattern in the 6 th field?

$ echo "Jun 12 09 mail (sender@sender.com) - (recip1@domain.com) 0.075 + 9387 Jun 12 10 mail (sender@sender.com) - (recip1@domain.com),(recip2@domai +n.com) -1.889 25623" | perl -pe's/(?:\S+\s+){6}\K(.+)/($x=$1)=~tr,(), +,d;$x/e' Jun 12 09 mail (sender@sender.com) - recip1@domain.com 0.075 9387 Jun 12 10 mail (sender@sender.com) - recip1@domain.com,recip2@domain.c +om -1.889 25623

Replies are listed 'Best First'.
Re^2: How can I replace the pattern in the 6 th field?
by AnomalousMonk (Chancellor) on Jun 18, 2018 at 19:56 UTC
    s/(?:\S+\s+){6}\K(.+)/($x=$1)=~tr,(),,d;$x/e

    This removes the dependence on Perl version 5.14 for the  /r modifier for the  tr/// operator, but does not satisfy the latest approximation to the real spec as given here. (Don'tcha just love receiving specs via drip-feed?) The problem is easily fixed with a  (\S+) vice  (.+) capture (still requires Perl version 5.10+):

    c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; print 'perl version: ', $]; ;; my $rec = 'Jun 12 10 mail (sender@sender.com) - (recip1@domain.com),(recip2@d +omain.com) -1.889 25623, queued_as: B67837C0052 Subject goes here Sen +der(sender@sender.com )'; print qq{'$rec'}; ;; $rec =~ s/(?:\S+\s+){6}\K(\S+)/(my $x=$1)=~tr,(),,d;$x/e; print qq{'$rec'}; " perl version: 5.010001 'Jun 12 10 mail (sender@sender.com) - (recip1@domain.com),(recip2@doma +in.com) -1.889 25623, queued_as: B67837C0052 Subject goes here Sender +(sender@sender.com)' 'Jun 12 10 mail (sender@sender.com) - recip1@domain.com,recip2@domain. +com -1.889 25623, queued_as: B67837C0052 Subject goes here Sender(sen +der@sender.com)'
    That still leaves a dependence on version 5.10 for the  \K regex operator, again easily fixed with another capture:
    c:\@Work\Perl\monks>perl -wMstrict -le "print 'perl version: ', $]; ;; my $rec = 'Jun 12 10 mail (sender@sender.com) - (recip1@domain.com),(recip2@d +omain.com) -1.889 25623, queued_as: B67837C0052 Subject goes here Sen +der(sender@sender.com)'; print qq{'$rec'}; ;; $rec =~ s/((?:\S+\s+){6})(\S+)/(my $x=$2)=~tr,(),,d;$1.$x/e; print qq{'$rec'}; " perl version: 5.008009 'Jun 12 10 mail (sender@sender.com) - (recip1@domain.com),(recip2@doma +in.com) -1.889 25623, queued_as: B67837C0052 Subject goes here Sender +(sender@sender.com)' 'Jun 12 10 mail (sender@sender.com) - recip1@domain.com,recip2@domain. +com -1.889 25623, queued_as: B67837C0052 Subject goes here Sender(sen +der@sender.com)'


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1216876]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2018-07-22 22:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (456 votes). Check out past polls.

    Notices?