You do not have to create a temp file to do some calculations on every pair of records. If you store each pair in an appropriate data structure, you could do the calculations in any scripting language or the shell itself (depending on the kind of calculations and the data structure). And then redirect the results to an output file.
Given below is a way to implement this approach in Perl:
- I have used an array to store every pair of lines.
- I have named the output files thusly: "myfile_<first_line#_of_pair>_<second_line#_of_pair>"
- Not sure what the calculations are, so I have just printed the array elements to the file.
$
$ cat -n f1
1 a,12,45,bn,c
2 a,16,46,bn1,c
3 a,18,47,bn2,c
4 a,12,47,bn3,c
5 a,11,49,bn4,c
6 a,19,79,bn8,c
$
$
$ ##
$ perl -lne 'chomp;
> if ($.%2 == 0) {
> $b = $.;
> push @x,$_;
> print "The pair of lines is now stored as first two elements of \@x";
> foreach $i (@x) {print "\t==> ",$i};
> print "Perform the calculations, print to file and reset the array";
> ## some calculations here...
> $file = "myfile_".$a."_".$b;
> print "Now writing to file: $file ";
> open (F, "> $file") or die "Cannot open $file for writing: $!";
> foreach $i (@x) {print F "\t==> ",$i};
> close (F) or die "Cannot close $file: $!";
> print "-"x60;
> @x=()}
> else { push @x,$_; $a=$. }' f1
The pair of lines is now stored as first two elements of @x
==> a,12,45,bn,c
==> a,16,46,bn1,c
Perform the calculations, print to file and reset the array
Now writing to file: myfile_1_2
------------------------------------------------------------
The pair of lines is now stored as first two elements of @x
==> a,18,47,bn2,c
==> a,12,47,bn3,c
Perform the calculations, print to file and reset the array
Now writing to file: myfile_3_4
------------------------------------------------------------
The pair of lines is now stored as first two elements of @x
==> a,11,49,bn4,c
==> a,19,79,bn8,c
Perform the calculations, print to file and reset the array
Now writing to file: myfile_5_6
------------------------------------------------------------
$
$ cat myfile_1_2
==> a,12,45,bn,c
==> a,16,46,bn1,c
$
$ cat myfile_3_4
==> a,18,47,bn2,c
==> a,12,47,bn3,c
$
$ cat myfile_5_6
==> a,11,49,bn4,c
==> a,19,79,bn8,c
$
$
This should at least get you started.
tyler_durden