Register
It is currently Sat Oct 25, 2014 10:01 am

Simple SED from within a Bash Script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Thu Jan 07, 2010 1:25 am   

Joined: Wed May 30, 2007 9:22 pm
Posts: 39
Location: California
I need help! I'm trying to write a bash script that asks the user two questions and takes the users answers and sends them and some other text to a particular line of a file that is in a different directory. Am I wrong in thinking that sed is the only way to add text to a particular line of a file?

The text that I want to add looks like this:

Code:
<track><path>$answer1</path><title>$answer2</title></track>


I want to add this line of text to line 3 of the xml file that is in a different directory.

The script looks something like this so far:

Code:
#!/bin/bash

read -p "Question 1? " $answer1;
read -p "Question 2? " $answer2;

# here's where the sed part will happen.  I've tried the following

sed -i 3i"<track><path>'$answer1'</path><title>'$answer2'</title></track>" ../player/playlist.xml;


This isn't working. What am I doing wrong? Should I even be using sed for this? I have NO sed experience! I've been reading up on sed all day and I feel more confused and overwhelmed by it than I did when I knew literally nothing about it. Any help would be much appreciated! Thanks!


Top
 Profile  
 PostPosted: Thu Jan 07, 2010 2:04 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
Code:
cat Dex.xml
foo
bar
baz

Code:
#!/bin/bash

read -p "Question 1? " answer1
read -p "Question 2? " answer2

sed -i 3i\
'\<track>\<path>'"$answer1"'\</path>\<title>'"$answer2"'\</title>\</track>' Dex.xml

cat Dex.xml


Code:
./Dex.sh
Question 1? ga bu
Question 2? zo meuh
foo
bar
<track><path>ga bu</path><title>zo meuh</title></track>
baz


Last edited by Watael on Thu Jan 07, 2010 3:39 am, edited 1 time in total.

Top
 Profile  
 PostPosted: Thu Jan 07, 2010 2:14 am   

Joined: Wed May 30, 2007 9:22 pm
Posts: 39
Location: California
Thank you for your reply. I'm now even more confused. I will try it, but would you mind explaining what this is doing? How can I get this to change a file in a different directory? Do I need two scripts to do this? Sorry I'm so confused.


Top
 Profile  
 PostPosted: Thu Jan 07, 2010 3:46 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
what this is doing?
It does what you asked: insert a string at line #3 of a file.

How can I get this to change a file in a different directory?
Know where the file is, put its 'path/name' in a variable, and replace 'Dex.xml' with this variable

Do I need two scripts to do this?
No.


Top
 Profile  
 PostPosted: Thu Jan 07, 2010 3:30 pm   

Joined: Wed May 30, 2007 9:22 pm
Posts: 39
Location: California
I get an error that says that -i isn't a valid option for sed.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 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:  
cron


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