Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by jwkrahn (Abbot)
on Jun 18, 2018 at 18:23 UTC ( [id://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 (Archbishop) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1216876]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-23 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found