How to read each 2 records from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read each 2 records from a file
# 1  
Old 09-28-2009
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

Code:
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

Last edited by vgersh99; 09-28-2009 at 11:59 AM.. Reason: code tags, PLEASE!
# 2  
Old 09-28-2009
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 [code] and [/code] 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


Code:
awk 'NR%2' file

# 3  
Old 09-28-2009
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
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[i]}  < test2.dat
done
i=`expr $i + 1`
done

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

Code:
 syntax error at line 8 : `(' unexpected

Please guide me on this.
Thanks

Last edited by vgersh99; 09-28-2009 at 12:00 PM.. Reason: code tags, PLEASE!
# 4  
Old 09-28-2009
Quote:
Originally Posted by kavithakuttyk
...
My requirement is to assign the first 2 records in the temp file to do some calculations assign the output to another file and ...
...
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.

Code:
$
$ 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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read a particular records from a file?

Hi All Can anybody let me know the code to read a particular record from a file For e.g. File Name: File.txt File content: Script_path=/abc/def/script/ File_path=/xyz/data/ Business Date=19990905 SERVER_NAME=Server DATABASE_NAME=Database Login=NewUser Password=NewPassword ... (3 Replies)
Discussion started by: Siddhartha9833
3 Replies

2. Shell Programming and Scripting

Read File and check records for length

I need a script that will run in unix to: 1) Read and input file with 1 column that contains for ex: 0123456789 1234567890 ...etc 2) Checks the first column if it is: a. Numeric from 0 - 9 b. if it is not less... (4 Replies)
Discussion started by: mrn6430
4 Replies

3. Shell Programming and Scripting

How to read records in a file and sort it?

I have a file which has number of pipe delimited records. I am able to read the records....but I want to sort it after reading. i=0 while IFS="|" read -r usrId dataOwn expire email group secProf startDt endDt smhRole RoleCat DataProf SysRole MesgRole SearchProf do print $usrId $dataOwn... (4 Replies)
Discussion started by: harish468
4 Replies

4. Shell Programming and Scripting

Read a file with n records as one big string using linux

Hello! Is there a way i can read a file with n records as one big string using linux shell script? I have a file in the below format - REC1 REC2 REC3 . . . REC4 Record length is 3000 bytes per record and with a newline char at the end. What i need to do is - read this file as one... (5 Replies)
Discussion started by: mailme0205
5 Replies

5. UNIX for Dummies Questions & Answers

Grep specific records from a file of records that are separated by an empty line

Hi everyone. I am a newbie to Linux stuff. I have this kind of problem which couldn't solve alone. I have a text file with records separated by empty lines like this: ID: 20 Name: X Age: 19 ID: 21 Name: Z ID: 22 Email: xxx@yahoo.com Name: Y Age: 19 I want to grep records that... (4 Replies)
Discussion started by: Atrisa
4 Replies

6. Shell Programming and Scripting

how to read next records

Hello friends, I am newbie in programing. I am facing some problems in awk. Please help me. I have a file with many data sets. Each data set is separated by an empty line. For example Col1 Col2 Col3 Col4 Col5 0.85 0.07 Fre 42:86 25 0.73 0.03 frp 21:10 28 0.64 0.04 Fre 42:86 63 0.47 0.08... (2 Replies)
Discussion started by: ubeejani
2 Replies

7. Shell Programming and Scripting

need shell script to read particular records from a file

i am reading an i/p file input.txt as below and want to read all filenames as in highlighted in bold below and put them in a different file output.txt. can someone help me with a shell script to do this? thanks in advance regards brad input.txt --------- START TYPE:OPT INIT_SEQ:01... (8 Replies)
Discussion started by: bradc
8 Replies

8. Shell Programming and Scripting

read records from a file

Hi all, I have a requirement where I need to read records one by one from a file. I have tried this below code: while read mLine do echo 'Line = '${mLine} done < input_file --- But the problem here is im getting the records with removed spaces. --Supposer if the record is like... (3 Replies)
Discussion started by: srilaxmi
3 Replies

9. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies

10. Shell Programming and Scripting

How to read a particular line in a file with 500000 records init

I am trying to open a file and check a record. The file has 543267 records and I want to check the record 514455. I am trying to open in vi and go to that line :514455 but i get the error not enough lines in buffer How should i overcome this? Please help. (6 Replies)
Discussion started by: mhssatya
6 Replies
Login or Register to Ask a Question

Featured Tech Videos