Trying to make script to insert file into tmp.txt

Using applications, configuring, problems
Post Reply
Message
Author
toowoombalinux
Posts: 95
Joined: Tue 16 Feb 2010, 00:22

Trying to make script to insert file into tmp.txt

#1 Post by toowoombalinux »

G'day,
After trawling through the Internet I've come to a standstill with the following script. And I'm not a scripting guru....
I want to insert the file exit1.tx into the middle of tmp.txt.

Code: Select all

#!/bin/sh
file=tmp.txt
lines=`wc -l $file |awk '{print $1}'`
middle=`expr $lines / 2`
sed -i '${middle}r exit1.txt' tmp.txt
I know that the expressions for lines and middle work however it doesn't want to play ball with sed. If I substitute ${middle} with a number then the sed expression works

exit1.txt has this:

Code: Select all

<icon x="704" y="704" label="Exit User Desktop">/usr/share/Audio-lauchers/desktop/User/exit</icon>
so it's chock full of characters which interfere with sed's syntax.

The answer is probably simple but so am I...

Cheers
Martin

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#2 Post by 01micko »

Try it with double quotes, singles take things as literal.Might work.

Code: Select all

#!/bin/sh 
 file=tmp.txt 
 lines=`wc -l $file |awk '{print $1}'` 
 middle=$(($lines / 2))  #bash math thrown in
 sed -i "${middle}r exit1.txt" tmp.txt
There might even be a better (if speed is an issue) way with a "while read line; do this to $line; done < that" loop

What exactly are you trying to do?
Puppy Linux Blog - contact me for access

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#3 Post by seaside »

toowoombalinux,

Since placing some xml code in the middle of a file might break up an existing xml sequence, you may find it better to put it following a known line. For instance, if an existing tag in "tmp.txt" ends with </icon> , you could place "exit1.txt" in the line following that tag end by -

Code: Select all

 sed -i '/<\/icon>/r exiit1.txt'  tmp.txt
Cheers,
s

Post Reply