Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: removing lines that are in the end of a file

by kcott (Abbot)
on Apr 05, 2013 at 19:02 UTC ( #1027201=note: print w/ replies, xml ) Need Help??


in reply to removing lines that are in the end of a file

G'day bhargavkanakiya,

You say you're OK with the file handling. Here's my solution for the data handling.

  1. Read a line and put it in a buffer.
  2. If line ends with "SIL", go back to 1 (i.e. read another line).
  3. Output all lines in the buffer.
  4. Clear the buffer.
  5. Go back to 1 (i.e. read another line).
  6. When all lines read, output the first line in the buffer.

Here it is on the commandline:

$ perl -Mstrict -Mwarnings -E '
    my @buffer;
    my @input = <>;
    say "========\n Output\n========";
    for (@input) {
        push @buffer, $_;
        next if /SIL\s*$/m;
        print shift @buffer while @buffer;
    }
    print $buffer[0] if @buffer;
'
0.642375 125 SIL 
1.0705 125 ઔર્ 
1.3651875 125 આત્ 
1.519875 125 મ
7.2140627 125 સે 
7.478125 125 હટ્ 
7.622625 125 જા 
7.956125 125 ઓ 
8.192375 125 SIL 
8.252 125 SIL 
8.464 125 SIL 
8.706 125 SIL
========
 Output
========
0.642375 125 SIL 
1.0705 125 ઔર્ 
1.3651875 125 આત્ 
1.519875 125 મ
7.2140627 125 સે 
7.478125 125 હટ્ 
7.622625 125 જા 
7.956125 125 ઓ 
8.192375 125 SIL 

I found some lines had additional whitespace at the end: this may be valid data; a result of your cut-n-paste operation; related to all those special characters; or, something else. Anyway, I used the /SIL\s*$/m regexp to get around this — you may want to modify that for your real-world application.

-- Ken


Comment on Re: removing lines that are in the end of a file
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (18)
As of 2015-07-28 19:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (258 votes), past polls