LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Substitute specific lines with lines from another file (https://www.linuxquestions.org/questions/programming-9/substitute-specific-lines-with-lines-from-another-file-696258/)

rahmathullakm 01-10-2009 03:00 AM

Substitute specific lines with lines from another file
 
Hello All,


I have two files called old-matter and new-matter

# cat old-matter

abc: this, is a, sample, entry
byi: white board, is white in color
rtz: black, board is black
qty: i tried, a lot
asd: no solutions, found for this task

abc: this is, the second, sample entry
byi: second, entry of white board
rtz: second, entry of black board
qty: second, entry of trying
asd: second, entry of no solutions

abc: this is, the third, sample entry
byi: third, entry of white board
rtz: third, entry of black board
qty: third, entry of trying
asd: third, entry of no solutions

......
......
... and so on up to 1517 entries


in this file, from the line begining with asd: to line ends with asd: is considered as one entry (or a paragraph).

now i had created another file called new-matter, which is having ONLY the first line of the above said each entries(paragraph) in the newly formatted style, as the following;

abc: newly updated, this, is a, sample, entry
abc: newly updated, this is, the second, sample entry
abc: newly updated, this is, the third, sample entry, with more info
....
....
1517 lines



Now i need to replace all the lines in the old-matter, which starts with abc: with the newly formatted abc: lines from the new-matter file. Here the critical thing, is, it should be replaced correctly (first paragraphs abc: should replace with first abc: in the new-matter and so on...), bcoz all the new entries are Uniq (this is actually an address list).


Also i need to put a blank line above all the line starts with abc:



Thanks in Advance...

jcookeman 01-10-2009 04:22 AM

Code:

#!/usr/bin/env perl
use strict;
use warnings;

my $oldfile=$ARGV[0];
my $newfile=$ARGV[1];

open(OLD,"<$oldfile") or die "file: cannot open $oldfile\n\n";
open(NEW,"<$newfile") or die "file: cannot open $newfile\n\n";

NEW: foreach my $newline (<NEW>) {
    OLD: while(my $oldline = readline(*OLD)) {
        print ($oldline =~ /^abc/ ? $newline : $oldline);
        next NEW if $oldline =~ /^\s*$/;
    }
}
close OLD;
close NEW;


rahmathullakm 01-10-2009 04:38 AM

Really thanking you
 
jcookeman,

Really Really Really thanks a million.....:):):) It was an urgent need for me and i was trying lot of things on the same. Now you had saved my time... Thanks dear.... thanks a lot.


Now can u send me one more info, how to put a blank line over all the lines which starts with abc:



Once again thanks a lot...

jcookeman 01-10-2009 04:53 AM

When you did `cat old-matter` it shows lines already there.

If there are no blank lines there it substantially changes the logic.

chakka.lokesh 01-10-2009 05:47 AM

it can also be solved using the following command:
Quote:

sed -e '/^abc:.*/R newfile' oldfile | sed -e '/^abc:.*/d;n'
PS: first line is missing. please add it manually.:D


All times are GMT -5. The time now is 10:23 AM.