LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
LinkBack Search this Thread
Old 11-06-2004, 10:36 PM   #1
gmartin
Member
 
Registered: Mar 2003
Location: PA
Distribution: Slackware 13.37 Linux Reg # 341245
Posts: 285

Rep: Reputation: 40
bash script to replace char in variable


I have a variable $ipath that contains a path "/usr/local" which I need to convert to "_usr_local"

I built the following at the shell prompt to do this

Code:
export tpath=`echo $ipath | sed -e 's/\//_/g'`
That worked.

Now I need to move it into a script so I dropped the export and tried:

Code:
for ipath in $backupdirs
#replace slashes with underscores
tpath=`echo $ipath| sed -e 's/\//_/g'`
I get an error:
line 30: syntax error near unexpected token `tpath=`echo $ipath| sed -e 's/\//_/g'`'

Can you help me fix this or suggest a better approach?
 
Old 11-06-2004, 10:46 PM   #2
gmartin
Member
 
Registered: Mar 2003
Location: PA
Distribution: Slackware 13.37 Linux Reg # 341245
Posts: 285

Original Poster
Rep: Reputation: 40
I hate answering my own posts. Problem was I added the sed code between the 'for' and the 'do'. Moving it to after the 'do' worked.

Sorry
 
Old 11-06-2004, 10:48 PM   #3
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 46
This _should_ work...
Code:
do
#replace slashes with underscores
  tpath=$(echo $ipath | sed -e 's/\//_/g')
  echo $tpath
done
 
Old 11-06-2004, 10:55 PM   #4
gmartin
Member
 
Registered: Mar 2003
Location: PA
Distribution: Slackware 13.37 Linux Reg # 341245
Posts: 285

Original Poster
Rep: Reputation: 40
Thanks ranger_nemo
 
Old 11-08-2004, 11:21 AM   #5
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
tpath=`echo -e ${ipath//:/\_}`

HTH

<EDIT> Oops...

tpath=`echo ${ipath//\//_}`

Last edited by /bin/bash; 11-08-2004 at 12:09 PM.
 
Old 11-08-2004, 01:46 PM   #6
gmartin
Member
 
Registered: Mar 2003
Location: PA
Distribution: Slackware 13.37 Linux Reg # 341245
Posts: 285

Original Poster
Rep: Reputation: 40
Thanks, folks - problem solved AND I learned something
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 12:13 AM
Bash script; command and args in variable. magjo813 Programming 2 02-16-2004 09:22 AM
bash script $n variable expansion cortez Programming 6 12-08-2003 04:03 PM
Bash script renaming a variable zael Programming 3 09-30-2003 04:37 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration