LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-05-2004, 06:22 PM   #1
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Rep: Reputation: 30
saerch and replace - with a twist


i need to do the classic search and replace on a file. i know about the
sed 's/whatever/hello/g' filein > fileout .
however, this doesnt work if either the search or replace strings have a / in them. for example, if either of them are paths it doesnt work.

is there another way i can do this, which using a path as one of the strings will work?

thanks.
 
Old 06-05-2004, 06:29 PM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Escape the / with a \

's/\//foo/g'

or change the separator.
 
Old 06-05-2004, 06:30 PM   #3
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
try escaping the forward slash with a backslash. i.e / becomes \/
 
Old 06-05-2004, 06:46 PM   #4
red_over_blue
LQ Newbie
 
Registered: Feb 2003
Location: MB, Canada
Posts: 26

Rep: Reputation: 15
You can also use whatever you want as a separator with sed, and the -i will edit files in place (makes the changes to the filename supplied).

sed -i "s:foo:bar:g" filename
sed -i "s|foo|bar|g" filename

Last edited by red_over_blue; 06-05-2004 at 06:47 PM.
 
Old 06-06-2004, 06:36 AM   #5
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
ok so it still doesnt work. heres the problem. this is the current script:
Code:
#!/bin/bash

echo "ED2K GUI Preview"
echo "----------------"
echo
echo "Got the following parameter: $1"
echo "File extension             : $ED2K_PREVIEW_EXTENSION"
echo
echo "Enter command to run on the file, typing [] in place"
echo "of the file path:"
read PREVIEW_BIN
echo $PREVIEW_BIN > pretemp
sed -i "s|[]|"$1"|g" pretemp
TO_RUN=`cat pretemp`
rm pretemp
echo "Running: $TO_RUN"
$TO_RUN

echo "Finished. Hit <enter> to close this window"
read
basically, this should get a file as a parameter, print some stuff about it, then recieve a command to run. it will find a [] in this and then replace it with the file. it will then run the command. supposedly. here is the current output:
Code:
ED2K GUI Preview
----------------

Got the following parameter: /home/drigz/music/Goldfrapp Black - Cherry/03-goldfrapp-black_cherry-esc.mp3
File extension             :

Enter command to run on the file, typing [] in place
of the file path:
hello [] bye
sed: -e expression #1, char 32: Unterminated `s' command
Running: hello [] bye
ed2k_gui_preview: line 19: hello: command not found
Finished. Hit <enter> to close this window
i was using hello [] bye as an example, i know it wont work as a command. the problem is the replacing doesnt work.

can anyone suggest either a way to make it work, or a better script (ie one which doesnt need to create files and stuff (im not great at bash scripting))

thanks.

btw, i know that generally just running:
command file
would work - this is what it used to be. the change is so that i can mount isos when they are incomplete, to check if they are real or fake (mount -t iso9660 -o loop [] /mnt/cdrom0)
 
Old 06-06-2004, 09:45 AM   #6
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
That's not the question you asked. You still need to escape the brackets.

sed -e 's/\[\]/"$1"/g'

I'm just assuming the script is run with an argument which is a filename in the first place? I'm not sure what you're trying to do but wouldn't something simple like
Code:
#!/bin/bash

echo "ED2K GUI Preview"
echo "----------------"
echo
echo "Got the following parameter: $1"
echo "File extension             : $ED2K_PREVIEW_EXTENSION"
echo
echo "Enter command to run on the file"
read PREVIEW_BIN
echo "Running: $PREVIEW_BIN"
$PREVIEW_BIN $1

echo "Finished. Hit <enter> to close this window"
read
work? Or just running the command without the script? Not sure where $ED2K_PREVIEW_EXTENSION comes in, either. Anyway - escape the brackets, I guess.
 
Old 06-06-2004, 12:57 PM   #7
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
ok lets start from scratch. here is my problem:

i need a script which will accept the full path of a file (probably containing spaces, but it all seems to fit into $1) as a parameter. it will then read in a line of input from the user. within this line of input, it will replace a certain thing ([] or %FILE% or whatever) with the path of the file, and then run the command. for example:

./myscript.sh /home/drigz/music/artist\ -\ track\ title.mp3
(input to script)
mplayer []

would run:
mplayer /home/drigz/music...
ie play the mp3 file htat was input

or:
./myscript.sh /home/drigz/cd\ image\ file.iso
(input to script)
mount -t iso9660 -o loop [] /mnt/cdrom0

would run:
mount -t iso9660 -o loop /home/drigz/cd\ image\ file.iso /mnt/cdrom0
ie mount the iso file at /mnt/cdrom0

how could i do this?
 
  


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
my distro's better then yours with a twist! berrance General 22 03-03-2005 01:30 PM
another which distro? with a twist wrat Linux - Newbie 13 05-10-2004 07:06 AM
a twist on the old Nvidia problem goosegg Red Hat 8 09-19-2003 05:45 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM
New twist on modems Darrell Willis Linux - Hardware 5 07-24-2003 06:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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