I have many input files. Here are two example input files:
Input File #1:
5
-124.087 41.52
-124.092 41.54
-124.098 41.56
-124.103 41.58
-124.109 41.60
5
-122.041 41.52
-122.052 41.53
-122.056 41.55
-122.060 41.57
-122.050 41.60
Input File #2:
5
-124.087 41.52
-124.092 41.54
-124.098 41.56
-124.103 41.58
-124.109 41.60
-5
-122.050 41.60
-122.060 41.57
-122.056 41.55
-122.052 41.53
-122.041 41.52
As you can notice the only difference between the two input files is the second set of numbers is reversed order for Input #2 (and the number of points is -5 instead of 5).
What I want as an output file is the same for BOTH:
-124.087 41.52
-124.092 41.54
-124.098 41.56
-124.103 41.58
-124.109 41.60
-122.050 41.60
-122.060 41.57
-122.056 41.55
-122.052 41.53
-122.041 41.52
(which is essentially Input #2 minus the defining number of points (10 & -10))
This would be simple for Input #2 (Solution: nawk '{if (NF>1) print $0}' input.txt > output.txt). But, I want a command or set of commands that will print the first set of 5 numbers in order and then if the following integer is positive (e.g. 5) print the following 5 points in reversed order or if the integer is negative (e.g. -5) print the following points in the same order as input file.
I have tried a combination of sed, nawk and tac commands to try and accomplish this. I cannot get it to work. Any tips?
ADDITIONAL COMPLICATION AFTER ORIGINAL QUESTION POSTED:
Ok, so I want to second set of numbers reversed if the integer just prior to second set of numbers is positive. And the first set was to remain in same order no matter what for the original question. Andriy Makukha provided a perfect solution to this below, but I actually need to complicate the question with one more detail.
I want the first set of numbers to be reversed if the first number is negative.
Say my input file (#3) is:
-5
-124.109 41.60
-124.103 41.58
-124.098 41.56
-124.092 41.54
-124.087 41.52
-3
-122.050 41.60
-122.056 41.55
-122.041 41.52
Desired output for #3:
-124.087 41.52
-124.092 41.54
-124.098 41.56
-124.103 41.58
-124.109 41.60
-122.050 41.60
-122.056 41.55
-122.041 41.52
Update: Andriy's solution (part 2) works!
awk 'NF==1 { s++; rev=($1>0)?1:0; next; }
s==2 && rev { a[++i] = $0; next; }
1
END { while(i>0) print a[i--]; }' input.txt
Tested with nawk version 20121220.
If you want the code to reverse both sections:
awk '
NF==1 { # single element: new section encountered
while(i>0) print a[i--]; # print out previous section in reverse (if needed)
s++; # increment section counter
pos = ($1>0) ? 1 : 0; # single number is positive?
next; # proceed to next line
}
s==2 && pos || s==1 && !pos { # are we in "reversing state"? if yes:
a[++i] = $0; # remember this line in array
next; # proceed to next line
}
1 # else: print the line as is
END {
while(i>0) print a[i--]; # print out previous section in reverse (if needed)
}' input.txt