fasoli has asked for the wisdom of the Perl Monks concerning the following question:
Hi all,
I am still new to Perl as I've only written about 10 very simple scripts, all with the help of a lot of Googling and reading two precious lifesaver books on Perl. Unfortunately I found myself stuck again at a simple task, I would be grateful if you could provide some hints towards how I should solve it.
I have a big text file with lots of whitespace separated columns in it. I want to ignore the first 12 lines and the two first columns and start copying the data from the third column onwards, until the end of the file. I have "written" (well, I tried one million things I read online and I found something that worked, this doesn't really count as "writing" code) this bit of code (copied below) but I have a few problems:
1. I have started copying data from where I find a match of (/^0.00000/), as this is the 13th line and this is where I want to start. I don't like that I'm matching the zeros though, as I realised that it's not sensitive enough. After checking what happens if I matched for (/^0.00/)) and (/^0.0/)) or even (/^0/)) I found that it's not looking for 5 decimal zeros, it's matching 0.00000 even when I'm looking for 0. As the file contains angle degrees, I'd like to match for something that only appears once, just to be safe.
In line 12, which is one line above where I want to start data copying, there is a unique pattern (@TYPE xy). I'd prefer to match this line and then tell the script to start copying from the next (13) line onwards. I've tried doing that in various different ways but I can't get it right. Unfortunately I don't have an example of what I wrote as I've changed the script more than 200 times, making different attempts until I stopped at matching the zeros since it worked.
2. I have managed to get my printf formatting right ("%8.3f%10.3f") but I'm stuck at another problem. I want to start importing data from column 3 onwards until the end, but the file is too big for me to count the columns, plus the column number might vary between files. So I've thought that I could ask Perl to count the columns for me, since I've already split them by whitespace, only I didn't have any success doing that. Also, I can't understand how I'll tell printf to print the first column in %8.3f format and the rest of them/unknown number in %10.3f format.
This is my script so far:
#/bin/perl/ use strict; my @files; @files = `ls 2kc29-out.txt`; my $file = $_; $_= $file; my $start = 0; open my $input, '<', './2kc29-out.txt' or die $!; open my $output, '>', 'test_mon.txt' or die $!; while (<$input>) { s/^\s+//; next unless (($start) || (/^0.00000/)); $start = 1; my @columns = split /\s+/, $_; printf $output ("%8.3f%10.3f","$columns[2] $columns[3]"); #here I' +m testing how columns 3 and 4 print #print $output (@columns[2..10]); #here I'm trying to see how to w +rite "print from column 3 until the end - pretending that 10 is the e +nd just as a test #printf $output ("%8.3f%10.3f",(@columns[2..10])); #same as above, + but trying to see how to use printf, obviously this only prints colu +mns 3 and 4 and nothing else printf $output "\n"; }
Any hints/guidelines would be much appreciated, I already feel ridiculous for spending 6 days on this and only getting this "far" :(
Update:
I've now tried the following:
print $output $columns[2] .. $columns[$#columns];...am I getting there? The format is so all over the place that I don't know if it's indeed printing the angle degrees, but does it make sense as in "from column 3 to the last column"?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: column counter and printf question
by Athanasius (Archbishop) on Oct 28, 2015 at 12:47 UTC | |
by fasoli (Beadle) on Oct 28, 2015 at 14:06 UTC | |
by Athanasius (Archbishop) on Oct 28, 2015 at 14:58 UTC | |
Re: column counter and printf question
by toolic (Bishop) on Oct 28, 2015 at 12:27 UTC | |
by fasoli (Beadle) on Oct 28, 2015 at 12:38 UTC | |
by Laurent_R (Canon) on Oct 28, 2015 at 14:35 UTC | |
Re: column counter and printf question
by scorpio17 (Canon) on Oct 28, 2015 at 14:12 UTC | |
Re: column counter and printf question
by Tux (Canon) on Oct 28, 2015 at 17:41 UTC | |
Re: column counter and printf question
by BillKSmith (Monsignor) on Oct 28, 2015 at 15:02 UTC | |
by Anonymous Monk on Oct 28, 2015 at 15:30 UTC | |
by scorpio17 (Canon) on Oct 28, 2015 at 17:23 UTC | |
by fasoli (Beadle) on Oct 28, 2015 at 15:53 UTC | |
by BillKSmith (Monsignor) on Oct 28, 2015 at 20:51 UTC | |
by fasoli (Beadle) on Oct 29, 2015 at 12:22 UTC | |
by Athanasius (Archbishop) on Oct 29, 2015 at 12:53 UTC | |
| |
by GotToBTru (Prior) on Oct 29, 2015 at 14:45 UTC | |
by ww (Archbishop) on Oct 28, 2015 at 18:25 UTC |