LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 01-10-2009, 03:00 AM   #1
rahmathullakm
LQ Newbie
 
Registered: Jul 2008
Posts: 15

Rep: Reputation: 0
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...
 
Old 01-10-2009, 04:22 AM   #2
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
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;
 
Old 01-10-2009, 04:38 AM   #3
rahmathullakm
LQ Newbie
 
Registered: Jul 2008
Posts: 15

Original Poster
Rep: Reputation: 0
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...
 
Old 01-10-2009, 04:53 AM   #4
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
When you did `cat old-matter` it shows lines already there.

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

Last edited by jcookeman; 01-10-2009 at 05:06 AM.
 
Old 01-10-2009, 05:47 AM   #5
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 32
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.

Last edited by chakka.lokesh; 01-10-2009 at 05:49 AM.
 
  


Reply

Tags
awk, processing, sed, text


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
Delete specific lines of a file beginning with a certain letter docaia Programming 4 08-24-2008 11:04 PM
Asterisk box to connect VoIP lines with normal telephone lines. sraju Linux - Software 1 11-08-2006 12:38 AM
Read specific lines from a text file chobin Programming 8 06-14-2006 11:14 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 02:03 PM


All times are GMT -5. The time now is 04:11 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