Read 2 files alternatively based on condition.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read 2 files alternatively based on condition.
# 1  
Old 10-13-2011
Question Read 2 files alternatively based on condition.

Experts,

What would be the code in ksh/perl to read 2 files alternatively, based on the following condition.

while reading file 1 we check if a blank line is encountered,
if yes, then we read file 2 unless a blank line is encountered in file 2
if we have a blank line in file 2 we come back to file 1 and continue reading file 1 unless a blank line is encountered, and then continue to file 2, till reading both the files have complete.

Thanks in advance,
Smilie
# 2  
Old 10-13-2011
Solution already provided there

The script give the output to console. if you want to save the output as a file, use the write file handler at end of the program or redirect the output to a file.

Code:
cmd#   perl scriptname.pl > your_output_file_name

or

Code:
open (FOUT, ">output.txt") || die "Cannot create the output file";
for (my $i = 0; $i<=$#file1; $i++) {
print FOUT "\n----- Group ", $i+1," ------\n", "$file1[$i]\n", "$file2[$i]\n";
}
close (FOUT);


Last edited by k_manimuthu; 10-13-2011 at 02:15 AM.. Reason: add another one example
# 3  
Old 10-13-2011
Quote:
Originally Posted by k_manimuthu
Solution already provided there

The script give the output to console. if you want to save the output as a file, use the write file handler at end of the program or redirect the output to a file.

Code:
cmd#   perl scriptname.pl > your_output_file_name

or

Code:
open (FOUT, ">output.txt") || die "Cannot create the output file";
for (my $i = 0; $i<=$#file1; $i++) {
print FOUT "\n----- Group ", $i+1," ------\n", "$file1[$i]\n", "$file2[$i]\n";
}
close (FOUT);

Thanks for you reply,
But the code you provided doesn't work...
it doesn't print anything.
# 4  
Old 10-13-2011
I just show the piece of code how to write the output to a file. I expect you collate code in the existing example. OK, tried the below code.
Code:
#!/usr/bin/perl
use strict;
use warnings;
my $file1=open_file("file1.txt");
my $file2=open_file("file2.txt");
my @file1 = split(/\n{2,}/, $file1);
my @file2 = split(/\n{2,}/, $file2);
open (FOUT, ">output.txt") || die "Cannot create the output file";
for (my $i = 0; $i<=$#file1; $i++) {
print FOUT "\n----- Group ", $i+1," ------\n", "$file1[$i]\n", "$file2[$i]\n";
}
close (FOUT);
sub open_file
{
 my ($fname)=@_;
 undef $/;
 open (FIN, "<", "$fname") || die "Cannot open the input file $fname : $!";
 my $fcontent=<FIN>;
 close (FIN);
 return "$fcontent\n";
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I have this list of files . Now I will have to pick the latest file based on some condition

3679 Jul 21 23:59 belk_rpo_error_**po9324892**_07212014.log 0 Jul 22 23:59 belk_rpo_error_**po9324892**_07222014.log 3679 Jul 23 23:59 belk_rpo_error_**po9324892**_07232014.log 22 Jul 22 06:30 belk_rpo_error_**po9324267**_07012014.log 0 Jul 20 05:50... (5 Replies)
Discussion started by: LoneRanger
5 Replies

2. Shell Programming and Scripting

Deleting lines based on a condition for a group of files

hi i have a set of similar files. i want to delete lines until certain pattern appears in those files. for a single file the following command can be used but i want to do it for all the files at a time since the number is in thousands. awk '/PATTERN/{i++}i' file (6 Replies)
Discussion started by: anurupa777
6 Replies

3. Shell Programming and Scripting

compare 2 files and return unique lines in each file (based on condition)

hi my problem is little complicated one. i have 2 files which appear like this file 1 abbsss:aa:22:34:as akl abc 1234 mkilll:as:ss:23:qs asc abc 0987 mlopii:cd:wq:24:as asd abc 7866 file2 lkoaa:as:24:32:sa alk abc 3245 lkmo:as:34:43:qs qsa abc 0987 kloia:ds:45:56:sa acq abc 7805 i... (5 Replies)
Discussion started by: anurupa777
5 Replies

4. Shell Programming and Scripting

Help in Logic - How to move files based on a condition

Hi All, I have 2 LINUX scripts (Lets say ScriptA & ScriptB) which copies files (lets say 'date_abc.txt , it goes to a while loop & checks for the files between 2 date varialbles) to directory X. (as Shown Below) These 2 scripts are called from 2 different Autosys schedules & runs at the... (3 Replies)
Discussion started by: dsfreddie
3 Replies

5. UNIX for Dummies Questions & Answers

moving files based on condition

hi i have to move files and send an email and attached the bad files to inform the developer about that. #!/bin/ksh BASE_DIR=/data/SrcFiles cd $BASE_DIR ## finding the files from work directory which are changed in 1 day find -type f -name "*.csv" –ctime 0 > /home/mydir/flist.txt ##... (14 Replies)
Discussion started by: awais290
14 Replies

6. Shell Programming and Scripting

Compare columns of 2 files based on condition defined in a different file

I have a control file which tells me which are the fields in the files I need to compare and based on the values I need to print the exact value if key =Y and output is Y , or if output is Y/N then I need to print only Y if it matches or N if it does not match and if output =N , then skip the feild... (7 Replies)
Discussion started by: newtoawk
7 Replies

7. Shell Programming and Scripting

Merging of all files based on a condition

Hi Friends, I am new to UNIX. I need to merge all the files(to FINAL.txt) in single directory based one condition. Out of all the files one of file will have specific value like :GF01: at any where in the file. so the file which is having :GF01: should be appended at the last. EX:... (5 Replies)
Discussion started by: arund_01
5 Replies

8. Shell Programming and Scripting

How to compare 2 files & get only few columns based on a condition related to both files?

Hiiiii friends I have 2 files which contains huge data & few lines of it are as shown below File1: b.dat(which has 21 columns) SSR 1976 8 12 13 10 44.00 39.0700 70.7800 7.0 0 0.00 0 2.78 0.00 0.00 0 0.00 2.78 0 NULL ISC 1976 8 12 22 32 37.39 36.2942 70.7338... (6 Replies)
Discussion started by: reva
6 Replies

9. Shell Programming and Scripting

command for deleting log files based on some condition

Hello, Can anyone pls. provide me with the command for deleting files older then 15 days with a restriction to keep at least 5 files in a directory even if they are older then 15 days. Any help will be highly appreciated. Thanks, Pulkit (4 Replies)
Discussion started by: pulkit
4 Replies

10. Shell Programming and Scripting

Read file based on condition

Hi Friends, Can any one help with this: I have a huge file with the format as A SAM 4637 B DEPT1 4758 MILAN A SMITH 46585 B DEPT2 5385 HARRYIS B SAMUL 63547 GEORGE B DANIEL 899 BOISE A FRES 736 74638 I have to read this file and write only the records that starts with "B" only ... (5 Replies)
Discussion started by: sbasetty
5 Replies
Login or Register to Ask a Question