LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-18-2012, 10:20 AM   #1
karamaz0v
LQ Newbie
 
Registered: Feb 2009
Posts: 4

Rep: Reputation: 0
Perl: how to replace blank lines in a file with given lines from another


Hi all,

I have two textfiles, one with N lines (N.dat), and other with a header and N blocks of data (block.dat) separated each with a blank line.

I want to insert each line from N.dat into each corresponding blank line in block.dat (also remove the header in the same process), i.e. I have

Code:
N.dat
1
2
3
Code:
block.dat
HEADER
HEADER

AAAA
BBBB
CCCC

AAAA
BBBB
CCCC

AAAA
BBBB
CCCC
and i want to obatin

Code:
1
AAAA
BBBB
CCCC
2
AAAA
BBBB
CCCC
3
AAAA
BBBB
CCCC

which would be one way to achive this with perl?

thanks,
 
Old 04-18-2012, 10:37 AM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,057

Rep: Reputation: 284Reputation: 284Reputation: 284
Do all blocks consist of three lines, or was that merely an accident of making a simple example? Are the "breaks" between blocks always a single blank line or possibly more than one blank line?

Daniel B. Martin
 
Old 04-18-2012, 12:35 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,412

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
Code:
awk '/^$/{getline < "N.dat"}1' block.dat
 
1 members found this post helpful.
Old 04-18-2012, 04:29 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,057

Rep: Reputation: 284Reputation: 284Reputation: 284
Quote:
Originally Posted by grail View Post
Code:
awk '/^$/{getline < "N.dat"}1' block.dat
Nitpick: OP wanted to discard the HEADER lines and you didn't do that. Should the awk be preceded by a grep? Can the awk be elaborated to ditch those HEADERs?

Daniel B. Martin
 
Old 04-18-2012, 08:03 PM   #5
karamaz0v
LQ Newbie
 
Registered: Feb 2009
Posts: 4

Original Poster
Rep: Reputation: 0
thank for the replies,

the awk one liner works with my example but not with my real case, i'm looking at it.

About the header I can remove it before or after inserting the lines using something like
sed '1,xd'
for x number of header lines.

Daniel: all blocks consist of the same number of lines, more than 3 but always the same in a given block.dat file (also in the header i have that number) and each "break" is only a single blank line.

To go further, I also need to invert each block, i.e. obtain

Code:
1
CCCC
BBBB
AAAA
2
CCCC
BBBB
AAAA
3
CCCC
BBBB
AAAA
I asked for a perl option, because I have a fortran program to do this now, but for portability I'm trying to learn perl scripting, sorry if I'm asking too much, maybe I have to keep writing it and come back here posting a semi working script.
anyway, an awk one liner would be of great help also.

thank for your help.
 
Old 04-18-2012, 09:23 PM   #6
karamaz0v
LQ Newbie
 
Registered: Feb 2009
Posts: 4

Original Poster
Rep: Reputation: 0
the problem with my real problem is that each break is not exactly a blank line, but a line with only a Single whitespace, for that reason the awk /^$/ pattern don't match those.
I solved that using previously
sed 's/^[ \t]*$//' filename
which will also convert every line containing tabs or more whitespaces to a "real" blank line.

After applying this sed command, the awk one liner works great, now I only need to know how to invert each block.

regards.
 
Old 04-19-2012, 02:17 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,412

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
As I would guess that the example data still looks nothing like the actual, I will leave the swapping of lines 1 and 3 to you (reference: http://www.gnu.org/software/gawk/man...e/index.html):
Code:
awk '/^ $/ && ++x{getline < "N.dat"}x' block.dat
 
Old 04-19-2012, 05:23 AM   #8
karamaz0v
LQ Newbie
 
Registered: Feb 2009
Posts: 4

Original Poster
Rep: Reputation: 0
ok,
the actual example is almost the same (after converting blank lines to real blank with no whitspaces), only difference is the number of lines at each block is not 3, is around fifty always.
since my original question was answered I marked it as solved, i'll figure out how to do the inversion.

thanks.
 
Old 04-19-2012, 06:48 AM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,412

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
btw. The last solution accounts for the space on the line, ie no need to convert prior. You could also throw an asterisk in there so it can contain zero to many spaces and your covered
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Blank Lines in a Perl Script fusion1275 Programming 5 05-04-2011 06:21 PM
replace several lines in a file with other lines in another file if condition yara Linux - General 12 10-27-2009 03:46 PM
Perl: testing for blank lines Garda Programming 4 11-16-2006 07:39 PM
Replace line in file with two lines?! Possible? eur0dad Linux - General 2 09-07-2006 02:15 PM
Replace blank/almost blank lines in file Wynd Linux - General 3 01-27-2004 04:49 PM


All times are GMT -5. The time now is 12:31 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration