Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Delete the first line of a file ?

by cored (Scribe)
on Oct 29, 2002 at 12:55 UTC ( [id://208720]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks, now i searching for delete the first line of a file with a regex, i know how to delete the first and last character of a line with ^$ but i dont know how to remove the first and last line can some body help me plz... thx this code above is for removing the putting # on unwanted deamons..
perl -i.bak -pe 's/^/#/' inetd.conf

Title edit by tye

Replies are listed 'Best First'.
Re: Regex
by Thelonius (Priest) on Oct 29, 2002 at 14:19 UTC
    Here's how I would remove the first line:
    perl -i.bak -ne 'print if $. > 1'
    and here's how I would remove the first and last line:
    perl -i.bak -ne 'print $prev if $. > 2; $prev=$_' test1
Re: Regex
by robartes (Priest) on Oct 29, 2002 at 13:12 UTC
    I'm not quite sure what you are asking, but I'll answer some questions that you might have been asking :)

    Removing first and last line of a file:

    use strict; open FILE, "</path/to/inputfile" or die "Blerch: $!\n"; # Set $/ to whatever your line terminating char is in inputfile my @lines=<FILE>; shift @lines; pop @lines; # @lines now contains the file contents without the first and last lin +es.

    Removing leading # from a line:

    $line=~s/^#//;

    Inserting a leading # in a line:

    $line=~s/^(.)/#$1/;
    Note that the above code is untested however, and might do all kinds of funny stuff, not excluding actually working as intended.

    CU
    Robartes-

Re: Regex
by atcroft (Abbot) on Oct 29, 2002 at 13:24 UTC

    If I may suggest, a regex is not the best tool for the job here. Unless there is an overriding reason, it might be easier just to do something along the lines of:

    my $filename = "blah.txt"; #replace as needed rename($filename, $filename . '.bak') or die("Unable to rename $filename to $filename.bak: $!\n"); open(INFILE, $filename . '.bak') or die("Can't open $filename.bak for input: $!\n"); open(OUTFILE, '>' . $filename) or die("Can't open $filename for output: $!\n"); my $linecount = 0; while ($line = <INFILE>) { print(OUTFILE $line) if ($linecount); $linecount++; } close(INFILE); close(OUTFILE);

    Two references that cover this and more that I use frequently are the perl FAQs and Perl Cookbook, and there are others here and about.

    Hope that helps. I look forward to seeing the responses of others as well.

      yes it worked
Re: Regex
by petral (Curate) on Oct 29, 2002 at 15:06 UTC
    Just for completeness, here's how one can do it with a regex:
    perl -p0777we 's/ .+? ( ^ .* ^ ) .* /$1/xms'


      p
Re: Delete the first line of a file ?
by thelenm (Vicar) on Oct 29, 2002 at 16:40 UTC
    Yet another way to remove the first line:
    perl -i.bak -ne 'print if 2..eof' myfile

    -- Mike

    --
    just,my${.02}

Re: Regex
by Basilides (Friar) on Oct 29, 2002 at 13:08 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-25 15:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found