How to read each 2 records from a file

Hi,

I have 10000 records in my test.dat file

All records are under the following format

a,12,45,bn,c
a,16,46,bn1,c
a,18,47,bn2,c
a,12,47,bn3,c
a,11,49,bn4,c

I have to read each 2 records and assign it into a temp file
.Can anybody help me in this?

Thanks

To keep the forums high quality for all users, please take the time to format your posts correctly.

  1. Use Code Tags when you post any code or data samples so others can easily read your code.
    You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags and by hand.)
  2. Avoid adding color or different fonts and font size to your posts.
    Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
  3. Be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Reply With Quote

awk 'NR%2' file

Sorry .This command takes only the first three records .My requirement is to assign the first 2 records in the temp file to do some calculations assign the output to another file and then read the next two records from test1.dat and keep that in the temp file ,after doing the calculation assign it to the output file.

actually I tried the below mentioned code

#! /bin/ksh
set -x
set -A abc `cat test1.dat`
n=`wc -l test1.dat`
while [[ $i -lt $n ]]
do
for(i=0;i<=n;i+=1)
do
echo ${abc}  < test2.dat
done
i=`expr $i + 1`
done

but it reads and displays only the first record.I am getting the following error message .

 syntax error at line 8 : `(' unexpected

Please guide me on this.
Thanks

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