Sponsored Content
Top Forums Shell Programming and Scripting How to read each 2 records from a file Post 302357016 by durden_tyler on Monday 28th of September 2009 12:14:16 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
IO::AtomicFile(3)					User Contributed Perl Documentation					 IO::AtomicFile(3)

NAME
IO::AtomicFile - write a file which is updated atomically SYNOPSIS
use IO::AtomicFile; ### Write a temp file, and have it install itself when closed: my $FH = IO::AtomicFile->open("bar.dat", "w"); print $FH "Hello! "; $FH->close || die "couldn't install atomic file: $!"; ### Write a temp file, but delete it before it gets installed: my $FH = IO::AtomicFile->open("bar.dat", "w"); print $FH "Hello! "; $FH->delete; ### Write a temp file, but neither install it nor delete it: my $FH = IO::AtomicFile->open("bar.dat", "w"); print $FH "Hello! "; $FH->detach; DESCRIPTION
This module is intended for people who need to update files reliably in the face of unexpected program termination. For example, you generally don't want to be halfway in the middle of writing /etc/passwd and have your program terminate! Even the act of writing a single scalar to a filehandle is not atomic. But this module gives you true atomic updates, via rename(). When you open a file /foo/bar.dat via this module, you are actually opening a temporary file /foo/bar.dat..TMP, and writing your output there. The act of closing this file (either explicitly via close(), or implicitly via the destruction of the object) will cause rename() to be called... therefore, from the point of view of the outside world, the file's contents are updated in a single time quantum. To ensure that problems do not go undetected, the "close" method done by the destructor will raise a fatal exception if the rename() fails. The explicit close() just returns undef. You can also decide at any point to trash the file you've been building. AUTHOR
Primary Maintainer David F. Skoll (dfs@roaringpenguin.com). Original Author Eryq (eryq@zeegee.com). President, ZeeGee Software Inc (http://www.zeegee.com). REVISION
$Revision: 1.2 $ perl v5.18.2 2005-02-10 IO::AtomicFile(3)
All times are GMT -4. The time now is 05:13 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy