More perl/awk compu...
 

MegaSack DRAW - This year's winner is user - rgwb
We will be in touch

[Closed] More perl/awk computer related help please

7 Posts
4 Users
0 Reactions
75 Views
Posts: 0
Free Member
Topic starter
 

Hi,

I know it may be taking the biscuit a bit, but further to this thread:

[url] http://www.singletrackworld.com/forum/topic/computer-question-awk-sed-grep-or-anything [/url]

I'm making great use of Aidy's / RichP's perl / awk commands.

To further optimise my life, any help on the following would be awesome:

The original column of numbers:

0
0
...
0
1
1
...
1
...
n
n
...
n

where n is any positive integer. This column is the third, where the first two represent lat and long.

I would to take the first occurrence of "n", copy that entire line and replace it after the last occurrence of "n". For example - in the following simple example we have

x_1 y_1 0
x_2 y_2 0
x_3 y_3 0
x_4 y_4 1
x_5 y_5 1
x_6 y_6 1
x_7 y_7 1

to turn into

x_1 y_1 0
x_2 y_2 0
x_3 y_2 0
x_1 y_1 0
x_4 y_4 1
x_5 y_5 1
x_6 y_6 1
x_7 y_7 1
x_4 y_4 1

And then I can apply the command to the third column to finish the job.

Any help would be much appreciated, it's either this or cutting and pasting 273 times. I realise this method will add 273 lines to my file.

Many thanks in advance,

Chris


 
Posted : 15/04/2011 3:50 pm
Posts: 27
Free Member
 

Here you go, apologies for the appearance, can seem to get lines of code to indent.

#!perl

open FID, "$ARGV[0]" or die "Can't open file - $ARGV[0]\n";

$current = -1;

while (<FID>) {
$index = (split(' ', $_))[2];
if ($index != $current) {
print $saved;
print $_;
$saved = $_;
$current = $index;
} else {
print $_;
}
}

print $saved;


 
Posted : 15/04/2011 8:49 pm
Posts: 117
Full Member
 

I think that this should work
awk "{if (NR==1){ind=$3;line=$0};if ( NR>0 ) { if ($3!=ind) {print line;{print $0};line=$0} else{print $0}};ind=$3}" in_filename.txt > out_filename.txt

You could probably combine the two operations into one script.


 
Posted : 15/04/2011 9:34 pm
Posts: 0
Free Member
Topic starter
 

mancjon - that works an absolute treat thank you very much.

richP - it doesn't seem to work, I'll play around though and hopefully get something out of it.

Thank you both so much. I need to learn perl. Out of interest, where/how did you learn it?

Cheers

Chris


 
Posted : 18/04/2011 8:08 am
Posts: 28
Free Member
 

The best way to learn perl ( outside of a training course ) would be to get O'Reilley's set of books...

[url= http://oreilly.com/pub/topic/perl ]http://oreilly.com/pub/topic/perl[/url]

These 3 should help you on your way:

Learning Perl
The Perl cookbook
Programming Perl


 
Posted : 18/04/2011 8:41 am
Posts: 27
Free Member
 

Chris

I would second cranberry's recommendation. "Learning Perl" will definitely get you to the stage where you could write the above sort of programs.

One slight note of warning. Perl is not the most structured language and usually there are multiple ways of achieving the same thing. From my experience, people can get hung up on the "best" way to do something in Perl. My advice would be to just ignore all that and simply learn enough to do what you want.

Jon


 
Posted : 18/04/2011 10:49 am
Posts: 117
Full Member
 

did you get the awk script to work?
What was the problem?


 
Posted : 19/04/2011 2:47 pm
Posts: 0
Free Member
Topic starter
 

Hi richP, I wasn't able to get your script to work, it works, but only for the first set of numbers.

mancjon's script has been working well though so I've been sticking with that.

Cheers again guys 🙂


 
Posted : 28/04/2011 2:47 pm