Google Answers Logo
View Question
 
Q: Simple Perl Script that Removes the Last Line of a Text File ( No Answer,   3 Comments )
Question  
Subject: Simple Perl Script that Removes the Last Line of a Text File
Category: Computers > Programming
Asked by: r0n1n13-ga
List Price: $5.00
Posted: 18 Jul 2005 11:35 PDT
Expires: 17 Aug 2005 11:35 PDT
Question ID: 544944
I have a text file <INPUT.txt> with pipe-delimited data and a footer
row.  All the rows have 36 fields of data, delimited by '|' (pipes),
except for the last (footer) row.

I need a perl script that:

(1) Reads the text file row-by-row and checks to see that there are 36
fields (separated by 35 pipes).

(2) If the row has 36 fields (or 35 pipes), write the row to another
text file <OUTPUT.txt>.  If the row does not have exactly 36 fields,
skip to the next row until the end of the file.

Alternatively (but less desirably), I will also accept a script that:

(1)  Reads through the data file <INPUT.txt> row-by-row.
(2)  Writes each row to an output file <OUTPUT.txt> except for the last row.
Answer  
There is no answer at this time.

Comments  
Subject: Re: Simple Perl Script that Removes the Last Line of a Text File
From: vladimir-ga on 18 Jul 2005 15:47 PDT
 
The script below should do the trick.

Save it in a file, say fields.pl (remember to chmod +x the file if
you're on Linux/UNIX) and use it like so (skip the "./" if you're NOT
on Linux/UNIX):

./fields.pl INPUT.txt > OUTPUT.txt

Here's the script:

#!/usr/bin/perl -n
                                                                                
print if ((split /\|/) == 36);
Subject: Re: Simple Perl Script that Removes the Last Line of a Text File
From: vladimir-ga on 18 Jul 2005 16:02 PDT
 
As a bonus, here's a script that prints all the lines from a file
except the last one (use it the same way):

#!/usr/bin/perl -n
 
print unless eof;
Subject: Re: Simple Perl Script that Removes the Last Line of a Text File
From: r0n1n13-ga on 19 Jul 2005 12:43 PDT
 
Both of these scripts work great, Vladimir!  Thanks for your help.

Please feel free to formally answer the question and claim the compensation.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy