Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Read Seq Text File, Write Text File W/O Specific Lines 2

Status
Not open for further replies.

canguro

Programmer
Sep 15, 2002
57
0
0
US
Hello,

I am a novice in Perl. Can someone be kind enough to suggest a 'quick' script to accomplish the following:

1. Input - sequential text file.
2. Output - sequential text file.
3. Process:
a. Read and Write out 1st record ('column headings').
b. Process remaining data records:
I. If 'column headings' record, skip and read next.
II. Otherwise, Write record.

Here is a sample of the typical data:

Ticker,Date,MFI14Day,Volume,Buy,Sell
ASTM,2/19/1997,-10000000000,16200,0,0
ASTM,2/20/1997,-10000000000,38300,0,0
ASTM,2/21/1997,-10000000000,76900,0,0
ASTM,2/24/1997,-10000000000,12100,0,0
ASTM,2/25/1997,34.8644,3200,0,0
ASTM,2/26/1997,65.1594,42300,0,0
ASTM,2/27/1997,53.4249,41300,0,0
ASTM,2/28/1997,52.8976,2300,0,0
Ticker,Date,MFI14Day,Volume,Buy,Sell
NOIZ,10/5/1993,-10000000000,1000,0,0
NOIZ,10/14/1993,-10000000000,1000,0,0
NOIZ,10/15/1993,86.8409,200,0,0
NOIZ,10/20/1993,81.5143,2500,0,0
NOIZ,10/22/1993,93.9497,77400,0,0
NOIZ,10/25/1993,96.9449,89900,0,0
NOIZ,10/26/1993,92.5823,8400,0,0
NOIZ,10/27/1993,98.2712,252500,0,0
NOIZ,10/28/1993,98.6015,90500,0,0
Ticker,Date,MFI14Day,Volume,Buy,Sell
STEM,8/10/1993,-10000000000,6500,0,0
STEM,8/11/1993,-10000000000,16300,0,0
STEM,8/12/1993,-10000000000,12300,0,0
STEM,8/13/1993,-10000000000,300,0,0
STEM,9/1/1993,70.4803,20800,0,0
STEM,9/2/1993,68.4455,12800,0,0
STEM,9/3/1993,66.7471,700,0,0
STEM,9/7/1993,61.5999,27600,0,0
STEM,9/8/1993,47.1489,94800,0,0


Your assistance is greatly appreciated. Many thanks!

Joe
 
What have you tried thus far? It's always better to learn how to do it by trying than for someone to give you the code. Do you have a good Perl book or website you're using?

________________________________________
Andrew

I work for a gift card company!
 
The following will work and will at least get you started:

CODE:
Code:
#!C:\\perl\\bin\\perl


my $infile ="c:\\temp\\infile.txt";
my $outfile ="c:\\temp\\outfile.txt";

open (INFILE,"$infile") or die ("Error opening file: $1");
open (OUTFILE,">$outfile") or die ("Error opening file: $1");

while (<INFILE>) {
	chomp;
	if (/^Ticker/) {
		print (OUTFILE "$_\n");
		last;
	}
}
my @arr=(<INFILE>);
foreach $line (@arr) {
	chomp $line;
	next if ($line =~ /^Ticker/);
	print (OUTFILE "$line\n");
}

Heres the input:

Code:
Ticker,Date,MFI14Day,Volume,Buy,Sell
ASTM,2/19/1997,-10000000000,16200,0,0
ASTM,2/20/1997,-10000000000,38300,0,0
ASTM,2/21/1997,-10000000000,76900,0,0
ASTM,2/24/1997,-10000000000,12100,0,0
ASTM,2/25/1997,34.8644,3200,0,0
ASTM,2/26/1997,65.1594,42300,0,0
ASTM,2/27/1997,53.4249,41300,0,0
ASTM,2/28/1997,52.8976,2300,0,0
Ticker,Date,MFI14Day,Volume,Buy,Sell
NOIZ,10/5/1993,-10000000000,1000,0,0
NOIZ,10/14/1993,-10000000000,1000,0,0
NOIZ,10/15/1993,86.8409,200,0,0
NOIZ,10/20/1993,81.5143,2500,0,0
NOIZ,10/22/1993,93.9497,77400,0,0
NOIZ,10/25/1993,96.9449,89900,0,0
NOIZ,10/26/1993,92.5823,8400,0,0
NOIZ,10/27/1993,98.2712,252500,0,0
NOIZ,10/28/1993,98.6015,90500,0,0
Ticker,Date,MFI14Day,Volume,Buy,Sell
STEM,8/10/1993,-10000000000,6500,0,0
STEM,8/11/1993,-10000000000,16300,0,0
STEM,8/12/1993,-10000000000,12300,0,0
STEM,8/13/1993,-10000000000,300,0,0
STEM,9/1/1993,70.4803,20800,0,0
STEM,9/2/1993,68.4455,12800,0,0
STEM,9/3/1993,66.7471,700,0,0
STEM,9/7/1993,61.5999,27600,0,0
STEM,9/8/1993,47.1489,94800,0,0

OUTPUT:

Code:
Ticker,Date,MFI14Day,Volume,Buy,Sell
ASTM,2/19/1997,-10000000000,16200,0,0
ASTM,2/20/1997,-10000000000,38300,0,0
ASTM,2/21/1997,-10000000000,76900,0,0
ASTM,2/24/1997,-10000000000,12100,0,0
ASTM,2/25/1997,34.8644,3200,0,0
ASTM,2/26/1997,65.1594,42300,0,0
ASTM,2/27/1997,53.4249,41300,0,0
ASTM,2/28/1997,52.8976,2300,0,0
NOIZ,10/5/1993,-10000000000,1000,0,0
NOIZ,10/14/1993,-10000000000,1000,0,0
NOIZ,10/15/1993,86.8409,200,0,0
NOIZ,10/20/1993,81.5143,2500,0,0
NOIZ,10/22/1993,93.9497,77400,0,0
NOIZ,10/25/1993,96.9449,89900,0,0
NOIZ,10/26/1993,92.5823,8400,0,0
NOIZ,10/27/1993,98.2712,252500,0,0
NOIZ,10/28/1993,98.6015,90500,0,0
STEM,8/10/1993,-10000000000,6500,0,0
STEM,8/11/1993,-10000000000,16300,0,0
STEM,8/12/1993,-10000000000,12300,0,0
STEM,8/13/1993,-10000000000,300,0,0
STEM,9/1/1993,70.4803,20800,0,0
STEM,9/2/1993,68.4455,12800,0,0
STEM,9/3/1993,66.7471,700,0,0
STEM,9/7/1993,61.5999,27600,0,0
STEM,9/8/1993,47.1489,94800,0,0
 
One-liner:
Code:
perl -ne "print unless /^Ticker/ && $. != 1" inputfile > outputfile
 
Very nice Mike. The -n option seems very useful.
 
Code:
awk '/^Ticker/&&NR>1{next}1' infile >outfile
 
I've got to say I love those one-liners from mike & cntr. As we all can see it does the same job as a 20-line (or so) Perl script. Lovely!


Kind Regards
Duncan
 
Some would say it's not Perl without proper code-density (awk cheaters excepting :p)

________________________________________
Andrew

I work for a gift card company!
 
We should start charging rent to these Awk boys :)

Mike

To err is human,
but to really foul things up -
you require a man Mike.

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top