Register
It is currently Sat Nov 22, 2014 6:59 pm

Replace place holder with external file (sed?)


All times are UTC - 6 hours


Post new topic Reply to topic  [ 10 posts ] 
Author Message
 PostPosted: Tue Feb 14, 2012 1:13 pm   
User avatar

Joined: Sat Jul 02, 2011 10:43 am
Posts: 23
Location: Devon,UK
I need to replace a place holder in a file with the contents of an external file like so:

1st file:
Code:
line 1 file 1
line 2 file 1
...

###MAIN###

...

line n file 1


2nd file:
Code:
line 1 file 2
line 2 file 2
...
line n file 2


result:
Code:
line 1 file 1
line 2 file 1
...

line 1 file 2
line 2 file 2
...
line n file 2

...

line n file 1


The best i have come up with so far is this:
Code:
#!/bin/bash

cnt=0
theline=0

while read
   do
      if [ X"$REPLY" = "X###MAIN###" ];then
         theline=$cnt
         cnt=-2
      fi
      ((cnt=cnt+1))
   done < <(cat "$1")

head -n $theline "${1}"
cat main.inc
tail -n $cnt "${1}"

I want to keep the formatting in both files in the final result, been googling and can't find an obvious solution, I would prefer sed but that 's not set in stone.
Any takers?


Top
 Profile WWW  
 PostPosted: Tue Feb 14, 2012 8:37 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
hi,

Code:
sed '/MAIN/{r file2
d}' file1
line 1 file 1
line 2 file 1
...

line 1 file 2
line 2 file 2
...
line n file 2

...

line n file 1


Top
 Profile  
 PostPosted: Wed Feb 15, 2012 1:08 pm   
User avatar

Joined: Sat Jul 02, 2011 10:43 am
Posts: 23
Location: Devon,UK
Quote:
sed '/MAIN/{r file2
d}' file1

Works perfectly just what I was looking for but why the newline and d and the curly braces? This does the same:
Code:
sed '/###MAIN###/r file2' file1

Thanks !


Top
 Profile WWW  
 PostPosted: Wed Feb 15, 2012 1:15 pm   
User avatar

Joined: Sat Jul 02, 2011 10:43 am
Posts: 23
Location: Devon,UK
Never mind just looked at the output from the two different lines and realized that your version removes the place holder as well


Top
 Profile WWW  
 PostPosted: Fri Mar 30, 2012 12:48 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Code:
ed -s <<< $'/###MAIN###/r file2\n//d\nw' file1


Top
 Profile  
 PostPosted: Sun Apr 01, 2012 3:56 pm   

Joined: Mon Feb 16, 2009 10:07 am
Posts: 19
Location: Paris - France
I find watael's solution simpler - on one line :
Code:
sed $'/###MAIN###/{r file2\nd}' file1

or, parameterized :
Code:
sed '/'$mark'/{r '$file2$'\nd}' "$file1"


Top
 Profile ICQ YIM  
 PostPosted: Sun Apr 01, 2012 5:20 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
sed(1) is meant to modify a stream, and ed(1) to edit a file, so the
latter appears to be the more suitable solution for the said purpose.


Top
 Profile  
 PostPosted: Mon Apr 02, 2012 2:54 am   

Joined: Mon Feb 16, 2009 10:07 am
Posts: 19
Location: Paris - France
the 'sed' solution is a bit faster than the 'ed' solution - which of both appears to be more suitable for us poor mortals ?


Top
 Profile ICQ YIM  
 PostPosted: Mon Apr 02, 2012 11:24 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Well, their execution time is quite similar, but the main factor that differentiates them
is the fact that sed(1) uses a temporary file while ed(1) does not. (It's an editor!)


Doing this (using the -i switch to edit the file in place):
Code:
sed -i $'/###MAIN###/{r file2\nd}' file1

is theorically like doing:
Code:
_file_tmp=$(mktemp)
sed $'/###MAIN###/{r file2\nd}' file1 > "$_file_tmp"
mv -f "$_file_tmp" "$_"


Top
 Profile  
 PostPosted: Mon Apr 02, 2012 4:26 pm   

Joined: Mon Feb 16, 2009 10:07 am
Posts: 19
Location: Paris - France
yes, yes, jsz, for purity's sake you're right :)


Top
 Profile ICQ YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP