Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

deleting special lines from files

by theroninwins (Friar)
on Sep 02, 2004 at 08:47 UTC ( #387838=perlquestion: print w/replies, xml ) Need Help??

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

Ok here is the situation... I have several files of data. Each line of the text in that file have several part.Those parts are sereated with ";". The problem is that some are missing the first part of data and start with ;. So here is the question how can i delete only those lines ?? I want to open the file then scan it and delete those lines and close it again. I am trying it with some replacing expression that puts a \n in that line, but then I have free lines. Any idear?? Thank you in advanced :-).

Replies are listed 'Best First'.
Re: deleting special lines from files
by Velaki (Chaplain) on Sep 02, 2004 at 08:55 UTC

    If you're merely trying to delete lines beginning with ';', then the following should work.

    perl -ni.bak -e 'print unless /^;/' file
    It also creates a backup file file.bak by using the -i flag. See perlrun for more options.

    Hope that helped,
    -v
    "Perl. There is no substitute."
      Thanks but i want to run it from within the program. I program does other things too. It is only part of the program, so i can't call the program like that. But thank it is still a very usefull hint you gave me.
        my @data = grep { /^\S+;/ } <FILEHANDLE>;

        Then the lines you want are in @data.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

        If the "other things" in your program are all happening inside a while loop like this:
        while (<>) { # or maybe using a file handle, like <IN> # do stuff... }
        and you just want to skip lines that start with a semi-colon, then just add one line at the top of the loop like this:
        while (<>) { next if ( /^\s*;/ ); # skip if first non-space char is ";" # now do stuff... }
        If you also want to delete lines with double semi-colons (because these represent empty fields), adjust that first line within the loop as follows:
        while (<>) { next if ( /^\s*;/ or /;;/ ); # now do stuff... }
        The "next" statement simply causes the script to skip everything that follows in the loop, and go right to the next iteration. The regular expressions in the "if" statement are saying:
        ^ -- line begins with... \s* -- zero or more white-space characters ; -- followed by a semi-colon or ;; -- there are two semi-colons together anywhere in the line
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2022-07-05 13:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?