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

Hi, Thanks for your prompt reply. My ACTUAL data has more than that. Since data is sensitive I did NOT want to post everything. anyway, This is my DATA. sir, Pls forgive me.

__DATA__ Jun 12 09 mail (sender@sender.com) - (recip1@domain.com) 0.075 9387, q +ueued_as: C77837C0050 Subject goes here Sender(sender@sender.com) Jun 12 10 mail (sender@sender.com) - (recip1@domain.com),(recip2@domai +n.com) -1.889 25623, queued_as: B67837C0052 Subject goes here Sender( +sender@sender.com)

Pls look at the last field. I don't want to replace last filed. Is there a way to capture ONLY 6 the field?

waiting for your inputs.

Replies are listed 'Best First'.
Re^3: How can I replace the pattern in the 6 th field?
by choroba (Cardinal) on Jun 18, 2018 at 16:11 UTC
    OK, then use split and run the substitution on the seventh (the dash is a field, too) field only:
    while (<DATA>) { chomp; my @F = split / /; $F[6] =~ s/[()]//g; say join ' ', @F; }
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      It works as I expected.

      Sir, Thanks a LOT for your effort. due to your help, I Could go ahead. I also appreciate everyone who helped me in this mail thread.

Re^3: How can I replace the pattern in the 6 th field?
by AnomalousMonk (Archbishop) on Jun 18, 2018 at 16:52 UTC

    If whitespace actually is the field delimiter, another way to operate on only the seventh field of the entire record without first extracting all fields would be this (needs Perl version 5.14+):

    c:\@Work\Perl\monks>perl -wMstrict -le "use 5.014; ;; 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{ \A \s* (?: \S+ \s+){6} \K (\S+) }{ $1 =~ tr/()//dr }xmse; print qq{'$rec'}; " '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)'
    Whether this is faster/better than a split/join approach is left as an exercise for theravadamonk; I doubt it really is. (This approach requires Perl version 5.14+ due to use of the  /r modifier in the  tr/// operator and version 5.10+ for the  \K regex operator. These can be avoided in earlier versions of Perl fairly simply; let me know if a workaround is needed. (Update: See this reply for workarounds.))


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

Re^3: How can I replace the pattern in the 6 th field?
by Laurent_R (Canon) on Jun 18, 2018 at 19:34 UTC
    You could split on the dash and run the substitution operator on the second field.
      ... split on the dash ...

      That seems to depend on the eighth or other subsequent field being a negative number. In the following, doesn't  Sender get its parens zapped?

      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 Send +er(sender@sender.com)'; print qq{'$rec'}; ;; my @F = split /-/, $rec; $F[1] =~ s/[()]//g; my $fixed = join '-', @F; print qq{'$fixed'}; " 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 Sendersende +r@sender.com'
      But it has no dependence on any Perl version above 5.8 — nor indeed, I think, above 5.0!


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

        That seems to depend on the eighth or other subsequent field being a negative number. In the following, doesn't Sender get its parens zapped?
        Yeah, you're right++, the solution really depends on the actual detailed data format, which we don't really know.

        Splitting the input on the first dash (with the split /PATTERN/,EXPR,LIMIT syntax with an appropriate LIMIT (I guess this should be 2, I can't test right now) should probably yield the desired result.