Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How do I delete/modify one line from test file ?

by Anonymous Monk
on Jun 22, 2000 at 18:02 UTC ( #19436=perlquestion: print w/replies, xml ) Need Help??

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

I have data file:

a|a|a|a|a|a
b|b|b|b|b|b
c|c|c|c|c|c

I want remove b|b|b|b|b|b line. How can I do that ?

Originally posted as a Categorized Question.

  • Comment on How do I delete/modify one line from test file ?

Replies are listed 'Best First'.
Re: How do I delete/modify one line from test file ?
by devslashneil (Friar) on Jun 19, 2003 at 01:33 UTC
    Another way of removing the line would be:

    #!/usr/bin/perl use Tie::File; my $filename = 'foo.dat'; tie @array, 'Tie::File', $filename or die "Cannot open $filename\n"; splice @array,1,1

Re: How do I delete/modify one line from test file ?
by marcos (Scribe) on Jun 22, 2000 at 19:04 UTC
    perl -ni.bak -e 'print unless /(b\|)*b/' filename
    This line edit in place (-n option) filename removing any line like b|b|b|b, and save the original filename as filename.bak (-i.bak option).
    I'm not sure this is what you need, but I hope it helps
    marcos
Re: How do I delete/modify one line from test file ?
by draco_iii (Acolyte) on Jun 22, 2000 at 22:18 UTC
    open(FILEHANDLE,"+< $FILENAME) or die print $!; while(<FILEHANDLE>){ if ($_ ne 'b|b|b|b|b|b'){ push(@outtext,$_); } Do what you want from here. Of course it always a good idea to lock the file if you are on a UNIX system.
      I can't get that to work how. Could you post a more complete answer. I am feeding the push command the line that I want removed but it doesn't do anything. What am I doing wrong?

        well, if you're pushing the line you don't want onto the array, it's going to end up in the array. It might help to think of another way of doing the same thing:

        push @array, $line unless $line eq 'b|b';

        Read the fine manual! => perldoc -f push

        Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2021-04-21 06:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?