Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Is this possible to make a one liner?

by Grygonos (Chaplain)
on Aug 01, 2003 at 20:40 UTC ( [id://280132]=perlquestion: print w/replies, xml ) Need Help??

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

I have the following code to just strip a bad line ending character...
#!/Perl/bin/perl -w use IO::File; use strict; my $file = new IO::File; my $new_file = new IO::File; $file->open("<memgrp.dat"); $new_file->open(">>new_mem_grp.dat"); while(!$file->eof) { my $line = $file->getline; $line =~ s/}//g; $new_file->write($line,length($line)); } $new_file->close; $file->close;
any way to one line this?

Replies are listed 'Best First'.
Re: Is this possible to make a one liner?
by ChemBoy (Priest) on Aug 01, 2003 at 20:48 UTC

    A look at perlrun would probably enrich your day, but meanwhile (and I wonder how many people will have posted this while I type this sentence...) I believe (unless I'm missing something) that the effect you're looking for can be achieved with this:

    perl -pe 'tr/}//d' memgrp.dat >> new_mem_grp.dat

    or for extra jollies, just alter the original file in place (saving a backup, of course):

    perl -pe 'tr/}//d' -i.bak memgrp.dat

    So in short, the answer to your question is "yes". :-)



    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders

•Re: Is this possible to make a one liner?
by merlyn (Sage) on Aug 01, 2003 at 20:44 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Is this possible to make a one liner?
by mpd (Monk) on Aug 02, 2003 at 00:42 UTC

    Well, assuming the asker wants an answer written in Perl (this isn't unixmonks.org or shellscriptmonks.org) I'll give it a shot. The given code and the description don't seem to match. A one liner for the ACTUAL CODE GIVEN (i.e. will remove every occurance of } in the file):

    perl -i.old -pe 's/}//g;' memgrp.dat

    If you just want to remove } when it occurs at the end of the line, just change the regexp to something like s/}(\s*)$/$1/g
    You might need to use double quotes on windows.

    This will overwrite memgrp.dat with the new information, backing the old one up in memgrp.dat.old (see perlman:perlrun.) This may not be exactly what you want, but it's easy to massage so you get what you're looking for.

    This code has been lightly tested and "it works for me."

      thanks guys.. answered my question beautifully.. i'm still trying to learn more about this slick kind of perl scripting
Re: Is this possible to make a one liner?
by waswas-fng (Curate) on Aug 01, 2003 at 20:50 UTC
    your s/// seems to go against your discription.   $line =~ s/}//g; will remove any } char not just at line end.

    -Waswas
      right... that's the only place it was so i just slopped it.. but yeah it should be $line =~ s/}$//; right?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://280132]
Approved by Paladin
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-19 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found