LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 08-04-2010, 10:59 AM   #1
fur
Member
 
Registered: Dec 2003
Distribution: Debian, FreeBSD
Posts: 310

Rep: Reputation: 35
replace txt between 2 strings.


I am trying to replace a section of a file between the first instances of the strings {}, with the contents of another file.

Example of the format of the file I'm trying to modify

Code:
Servername=something.com

     hosts {
       macaddress1
       macaddress2
       macaddress3
       macaddress4
       macaddress5
       macaddress6
     }
other stuff that
doesn't matter { }

So I would like to replace
Code:
     hosts {
       macaddress1
       macaddress2
       macaddress3
       macaddress4
       macaddress5
       macaddress6
     }
With data from another file that contains,

Code:
       macaddress7
       macaddress8
       macaddress9
       macaddress10
       macaddress11
       macaddress12
Tried to set,
IFS=""

Then captured all the "macaddress#"s to a variable and used sed to swap

sed "s/$CURRENTDATA/$NEWDATA/" filename

However I get 1 of 2 errors,

Using a small number of macs in "$NEWDATA"

sed: command garbled: s/ macaddresshere

Or when using a large number of macs in the $NEWDATA variable get

bash: /usr/bin/sed: Arg list too long.

Any other ideas on how to replace a large block of txt in one file with another large block from another?

Thanks
 
Old 08-04-2010, 11:09 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
$ cat file1
       macaddress7
       macaddress8
       macaddress9
       macaddress10
       macaddress11
       macaddress12

$ cat file
Servername=something.com

     hosts {
       macaddress1
       macaddress2
       macaddress3
       macaddress4
       macaddress5
       macaddress6
     }
other stuff that
doesn't matter { }

$ awk -vRS="}" 'FNR==NR{s=s$0;next}/hosts/{ gsub(/{.*$/,"");$0=$0"{\n"s}RT{print}' ORS="}" file1 file
Servername=something.com

     hosts {
       macaddress7
       macaddress8
       macaddress9
       macaddress10
       macaddress11
       macaddress12
}
other stuff that
doesn't matter { }
 
Old 08-04-2010, 03:26 PM   #3
fur
Member
 
Registered: Dec 2003
Distribution: Debian, FreeBSD
Posts: 310

Original Poster
Rep: Reputation: 35
Hrm, I tried that on a Debian, and Solaris machine using awk and nawk, and I don't get any output.

Just drops to the next prompt.

Code:
$ cat file1
       macaddress7
       macaddress8
       macaddress9
       macaddress10
       macaddress11
       macaddress12
$ cat file
Servername=something.com

     hosts {
       macaddress1
       macaddress2
       macaddress3
       macaddress4
       macaddress5
       macaddress6
     }
other stuff that
doesn't matter { }

$ awk -vRS="}" 'FNR==NR{s=s$0;next}/hosts/{ gsub(/{.*$/,"");$0=$0"{\n"s}RT{print}' ORS="}" file1 file
$
 
Old 08-05-2010, 09:38 AM   #4
fur
Member
 
Registered: Dec 2003
Distribution: Debian, FreeBSD
Posts: 310

Original Poster
Rep: Reputation: 35
Using it with gawk works.

Thanks
 
Old 08-28-2010, 01:31 PM   #5
fur
Member
 
Registered: Dec 2003
Distribution: Debian, FreeBSD
Posts: 310

Original Poster
Rep: Reputation: 35
How could you use the same command, and have it match only the first instance in a file?
 
Old 08-29-2010, 12:33 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Here is an alternative:
Code:
awk 'f{getline < "f2"; print}!f;/hosts/{f=1}/}/{f=0}' file
Quote:
How could you use the same command, and have it match only the first instance in a file?
First instance of what? Do you mean the first mac address? Or do you have more than one 'hosts{}' grouping?
 
Old 08-29-2010, 02:08 AM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

this will replace anything inside the first pair of '{}', regardless if it starts with
hosts {
guests {
or whatever {
Code:
sed -e '/{/ {P;r file2
 :mark /}/ ! {N;b mark}; /}/ {s/.*\n\([         ]*}\)/\1/;};: endloop /\$/ ! {N;b endloop}; }' file
It is important that there mustn't be anything after 'r file2'. Otherwise it will be interpreted as part of the filename. Yes, it's a oneliner that spans over two lines.
The [] contains a tab and space.
 
  


Reply



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
in shell scripting how to compare strings from two different simple .txt files tej16 Linux - Newbie 1 07-29-2010 02:34 AM
Question About Replace Command With Strings Un!x85 Linux - Newbie 5 10-21-2009 05:47 AM
Using tr to replace strings in a group of filenames Lemmings Linux - Newbie 7 05-21-2008 11:00 AM
Bash - txt replace clpl1980 Fedora 2 01-22-2007 05:44 AM
strings > /mnt/hda9/HrdDskB7.txt suguru Debian 3 02-23-2004 09:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:16 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration