Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

match a word and delete last line

by ArifS (Beadle)
on May 23, 2017 at 13:26 UTC ( [id://1190966]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I am trying to match the word (sample-text) and delete previous line if has a single word that might include (digit,_,-).
use strict; use warnings; my $filename = 'test1.txt'; open(my $fh, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!"; my $line; my $flag=1; while($line=<$fh>) { if($flag == 0) { print $line; next; } if($line=~/sample-text/) { $flag=0; print $line; } }
Content of the test1.txt
Text_1 sample-text file Text_1 matching the 1st line matching the last line Text text_1 text-2 sample-text file text-2 matching the 1st line matching the last line
Expected output
sample-text file Text_1 matching the 1st line matching the last line text text_1 sample-text file text-2 matching the 1st line matching the last line
But it only removes the 1st line, and doesn't look for other lines. Please let me know.

Replies are listed 'Best First'.
Re: match a word and delete last line
by tybalt89 (Monsignor) on May 23, 2017 at 13:40 UTC
    #!/usr/bin/perl # http://perlmonks.org/?node_id=1190966 use strict; use warnings; $_ = do { local $/; <DATA> }; s/^(\w+[-_]\d+)\n(?=.*\1\b)//gm; print; __DATA__ Text_1 sample-text file Text_1 matching the 1st line matching the last line Text text_1 text-2 sample-text file text-2 matching the 1st line matching the last line
      tybalt89, how do I modify to remove the previous line word if doesn't include (d,_,-) for example if only says Text.
      sample-text file Text-1 matching the 1st line matching the last line text text_1 sample-text file text-2 matching the 1st line matching the last line text_1 text-2 text sample-text file text matching the 1st line matching the last line
      I need to remove the following ( text ) too-
      ..................... text_1 text-2 text sample-text file text matching the 1st line matching the last line

        Try this, but your question is a little unclear. Please give an exact input and output test case, not a partial one.

        #!/usr/bin/perl # http://perlmonks.org/?node_id=1190966 use strict; use warnings; $_ = do { local $/; <DATA> }; s/^(\w+(?:[-_]\d+)?)\n(?=.*\1\b)//gm; print; __DATA__ Text_1 sample-text file Text_1 matching the 1st line matching the last line Text text_1 text-2 sample-text file text-2 matching the 1st line matching the last line text_1 text-2 text sample-text file text matching the 1st line matching the last line
      That works. Thank you.
Re: match a word and delete last line
by hippo (Bishop) on May 23, 2017 at 13:49 UTC
    But it only removes the 1st line, and doesn't look for other lines. Please let me know.

    That's because once $flag is set to zero, it will always be zero. There is no place in your code where it can be reset to 1. You will have to re-think the fine detail of your algorithm.

Re: match a word and delete last line
by BillKSmith (Monsignor) on May 23, 2017 at 22:59 UTC
    Your task is trivial if you process the file backwards and then reverse the result.
    use strict; use warnings; use File::ReadBackwards; my $INFILE = File::ReadBackwards->new( 'infile.txt' ) or die "Cannot open infile.txt\n$!"; open my $TMPFILE, '>', 'backward.txt'; while (defined( $_ = $INFILE->readline)) { print {$TMPFILE} $_; next if !/sample-text/; $INFILE->readline; } $INFILE->close; close $TMPFILE; my $BCKFILE = File::ReadBackwards->new( 'backward.txt' ); while (defined( my $line = $BCKFILE->readline)) { print $line; } $BCKFILE->close; unlink 'backward.txt';
    Bill

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-25 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found