Looping through only blank lines of a file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looping through only blank lines of a file.
# 1  
Old 10-10-2011
Question Looping through only blank lines of a file.

I am sorry if I am posting in wrong thread.

Experts,
I have 2 files
Code:
File 1              File 2
line1               line1              |
line2               line2              | group 1
line3               line3              |

line1               line1              |
line2               line2              | group 2 

line1               line1              | group 3
.                    .
.                    .
.                    .

line n              line n              | group n

My requirement is from file 2 i want to read the number of lines unless blank line is encountered and insert it in file 1 after the first group of lines in the blank line, and insert a blank line after inserting from file 2. similarly for second group of data, till group n.

Last edited by Franklin52; 10-10-2011 at 05:32 AM.. Reason: Please use code tags, thank you
# 2  
Old 10-10-2011
Hi,

Please provide sample output for this..

Cheers,
RangaSmilie
# 3  
Old 10-10-2011
Hi Ranga,

The output should be a new file, say file 3 with line of file 1 and file 2
file 3
Code:
line1 (file1)
line2 (file1)
line3 (file1)
line1 (file2)
line2 (file2)
line3 (file2)
<blank line>
line1 (file1)
line2 (file1)
line1 (file2)
line2 (file2)
<blank line>
line n (file1)
line n (file2)
<blank line>


Last edited by Franklin52; 10-11-2011 at 03:03 AM.. Reason: Please use code tags, thank you
# 4  
Old 10-12-2011
Question

Quote:
Originally Posted by rangarasan
Hi,

Please provide sample output for this..

Cheers,
RangaSmilie

provided the output
# 5  
Old 10-12-2011
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);
for (my $i = 0; $i<=$#file1; $i++) {
print "\n----- Group ", $i+1," ------\n", "$file1[$i]\n", "$file2[$i]\n";
}
sub open_file
{
 my ($fname)=@_;
 undef $/;
 open (FIN, "<", "$fname") || die "Cannot open the input file $fname : $!";
 my $fcontent=<FIN>;
 close (FIN);
 return "$fcontent\n";
}


Last edited by k_manimuthu; 10-12-2011 at 04:00 AM..
# 6  
Old 10-12-2011
I didn't understand the requirement itself.
Can you please paste input and required output? not with line1 line2 etc please

--ahamed
# 7  
Old 10-12-2011
Thank you guys,
k_manimuthu your code did work, but its appending file 2 to file 1, i need to write the data from both the file alternatively...

tried a different approach...
still fine tuning it..

Thanks for all your help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To check Blank Lines, Blank Records and Junk Characters in a File

Hi All Need Help I have a file with the below format (ABC.TXT) : ®¿¿ABCDHEJJSJJ|XCBJSKK01|M|7348974982790 HDFLJDKJSKJ|KJALKSD02|M|7378439274898 KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999 (cont......) I need to write a script where it will check for : blank lines (between rows,before... (6 Replies)
Discussion started by: chatwithsaurav
6 Replies

2. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

3. Shell Programming and Scripting

Removing blank lines from a file

Hi All, How do i remove continuos blank lines from a file. I have a file with data: abc; def; ghi; jkl; mno; pqr; In the above file, there are two blank lines. I want to remove, one out of them. My output should look like: (2 Replies)
Discussion started by: raosr020
2 Replies

4. Shell Programming and Scripting

Reform Lines in File without blank lines and spaces

Hello All, I have a file with data as below. Each line consists of 21 fields. I am not able to load them back to the database. 50733339,"834","834 ","005010X279A1","N","Y","007977163","0001 ",30,"2110D ","EB ","EB007 ","2 ","Conditional Required Data Element Miss ing... (3 Replies)
Discussion started by: Praveenkulkarni
3 Replies

5. Shell Programming and Scripting

Delete blank lines, if blank lines are more than one using shell

Hi, Consider a file named "testfile" The contents of file are as below first line added for test second line added for test third line added for test fourth line added for test fifth line added for test (5 Replies)
Discussion started by: anil8103
5 Replies

6. Shell Programming and Scripting

Delete blank lines from a file

Hi, I want to use diff to compare two files in a Perl file. But one of the files has some blank lines at the end. So I want to delete the blank lines from the file firstly and then use diff to compare them. But I dont know how to delete the blank lines from the files. Meanwhile, the system is... (5 Replies)
Discussion started by: Damon_Qu
5 Replies

7. UNIX for Dummies Questions & Answers

Removing blank lines in a file

Hi I have a text file that has blank lines at different places. How to remove all the blank lines in a file? Thanks Ashok (3 Replies)
Discussion started by: ashok.k
3 Replies

8. Shell Programming and Scripting

Can't remove blank lines from a file

Hi Guys, I have been trying to remove blank lines from a file with no success. I tried using all the following options on the file: tr -s '\n' < abc.txt grep -v "^$" abc.txt sed '/^$/d' abc.txt sed '/./!d' abc.txt awk '/./' abc.txt The file is a text file. (11 Replies)
Discussion started by: npatwardhan
11 Replies

9. UNIX for Dummies Questions & Answers

delete blank lines from a file

can anyone show me how to delete blank lines from a file. thanks in advance (2 Replies)
Discussion started by: sachin.gangadha
2 Replies

10. Shell Programming and Scripting

Blank Lines - End of file

Hi all I need to strip blank lines from the end of a file. I have searched and found topics on how to strip lines from the entirety of a file - however I need to limit this to only the last 3-4 lines. Any ideas? Thanks (4 Replies)
Discussion started by: saabir
4 Replies
Login or Register to Ask a Question