Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

killlastdot.pl

by hash (Scribe)
on Jul 31, 2003 at 18:36 UTC ( [id://279711]=sourcecode: print w/replies, xml ) Need Help??
Category: text processing
Author/Contact Info hash h4sh@globo.com
Description: Code usefull to edit files automaticly removing the last "." in the selected string lines.
#!/usr/bin/perl 
# Get files and skip "." in the selected strings.
# jeffa is right. killlastdot.pl :)
# Usage: ./killlastdot <ENTER> file word

my $FILE = <STDIN> ;
chop $FILE ;
my $VAR = <STDIN> ;
chop $VAR ;
open(GETARQ, "$FILE") ;
my @ARQ = <GETARQ> ;
close(GETARQ) ;
        foreach (@ARQ) {
                if ($_=~/$VAR/) {
                        my $x = $_ ;
                        chop($x) ;
                        chop($x) ;
                        print "$x\n" ;
                        }
        else
        {
        print $_ ;
        }
}
Replies are listed 'Best First'.
(jeffa) Re: ident.pl
by jeffa (Bishop) on Aug 01, 2003 at 02:27 UTC
    Since this program removes any trailing dot on lines that contain some particular string, i don't see why you named it ident.pl ... but that's okay. Here is some code that does the same thing as yours, but does so more consisely:
    #!/usr/bin/perl use strict; use warnings; my ($file,$var) = @ARGV; die "USAGE: $0 file word\n" unless $file and $var; open GETARG, '<', $file or die "Can't open $file: $!\n"; while (<GETARG>) { s/\.$// if /$var/; print; } close GETARG or die "Couldn't close $file: $!\n";
    Also, if you don't mind "hard coding" the word you want to match in the code, this can be trimmed down to a one liner: (remember to replace the double quotes with single quotes if you use Win32)
    perl -pe's/\.$// if /word/' file
    Perl rules. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Also, if you don't mind "hard coding" the word you want to match in the code, this can be trimmed down to a one liner

      Nevermind "hard coding" it... and on Unix, you might as well replace it with an alias. Here's how I'd do that in my shell (bash):

      alias rmdots="perl -pe 's/\.\$// if /@{[shift]}/'"
      Uh... (test first!) Make that:
      alias rmdots="perl -pe 'BEGIN{\$p=shift}s/\.$// if /\$p/'"
      Usage would be: rmdots pattern file ...

      -sauoq
      "My two cents aren't worth a dime.";
      
      perl -pi -e's/(word.*)\.$/$1/' file
      :-)

      Makeshifts last the longest.

      jeffa, your code is much more consisely, becouse it shows error messages if the user try a wrong way to input file and word. But it works for me. Anyway i would call this script as "one-way script" becouse i used killlastdot.pl for an especific usage, that i should remove dots from a text file in SQL.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://279711]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found