rfransix has asked for the wisdom of the Perl Monks concerning the following question:

O Wise and Awesome Monks, a text file contains lines of fields seperated by some delimiter; space for example; some lines not all will have in field 2 a value like an inital (e.g., A. or B. or Jr.) for those lines with such a value in field 2, we wish to have that value removed or deleted. We also wish to do this as a one-line. Here's what we have so far. What say you?

c:\perl\bin\perl.exe -e "while (<>) { next if ($F[2] ~= /A-Za-z/\.;) print \"\"}" < text.txt > xt.txt

or

c:\perl\bin\perl.exe -e -i.bak "while (<>) { next if ($F[2] ~= /A-Za-z/\.;) print \"\"}" text.txt

Replies are listed 'Best First'.
Re: On Match Remove Field
by AnomalousMonk (Archbishop) on Mar 03, 2011 at 21:19 UTC

    I would suggest something like (untested):
        perl -ane "print unless $F[2] =~ m{ \A (?: [[:alpha:]] | Jr) \. \z }xms" in.file > out.file
    Update: or with in-place editing (which I didn't notice at first) (also untested):
        perl -i.orig -ane "print unless $F[2] =~ m{ \A (?: [[:alpha:]] | Jr) \. \z }xms" in.file

    See perlrun.

    Update: BTW: What did you say about the results you got when you tried the code in the OP? You did at least try it, didn't you?

      Code in the OP? What's that?

      Your code prints the lines desired, omits all other lines with a A-Z. in any other field, and prints all lines without a A-Z. (both undesired). We wish to delete only the second field if it contains a period; if it doesn't contain a period, we do nothing and move to the next line.

Re: On Match Remove Field
by TomDLux (Vicar) on Mar 03, 2011 at 21:27 UTC

    Perl already has the -pi options to edit files line by line, either editing in place or saving a backup file if you use -p -i.bak. Add -a to split each line, same as awk does. if you want to split on a different character other than a space, you can specify it with -F, but you want space splits

    perl -pia -e"splice @F, 1, 1 if $F[1] =~ /[A-Z]\./i or $F[1] eq 'jr.' + "

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

    A reply falls below the community's threshold of quality. You may see it by logging in.