LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-01-2011, 06:15 AM   #1
xombboxer
Member
 
Registered: Apr 2011
Posts: 63

Rep: Reputation: 0
string replace in bash script


i want to find something in a string and replace with other string in bash and ksh how do i do that

ex:
Code:
mystring="asdf-)"
i want to replace ')' with '.'
like this
Code:
replace(mystring, match, newstring)
finally I want mystring as
Code:
mystring="asdf-."
 
Old 05-01-2011, 06:22 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Using bash internals:
Code:
mystring=${mystring/)/.}
Man bash for the details and other neat tricks.

Hope this helps.
 
Old 05-01-2011, 06:24 AM   #3
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
From 'man bash':

Quote:
${parameter/pattern/string}

Pattern substitution. The pattern is expanded to produce a pattern just as in pathname expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. If pattern begins with /, all matches of pattern are replaced with string. Normally only the first match is replaced. If pattern begins with #, it must match at the beginning of the expanded value of parameter. If pattern begins with %, it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / following pattern may be omitted. If parameter is @ or *, the substitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.
For your example:

Code:
mystring="asdf-)"
echo ${mystring/)/.}
 
Old 05-01-2011, 07:54 AM   #4
xombboxer
Member
 
Registered: Apr 2011
Posts: 63

Original Poster
Rep: Reputation: 0
yes this is perfect but in my case i have pass this for a sh file from prompt as
Code:
./sh pass="asdf-)"
this will be treated as asdf-) in bash. Then i am calling another sh file, where i am using expect (password). there i cannot change value fro ')' to '.'

how do i do that...

it says bad parameter
 
Old 05-01-2011, 11:18 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Could you explain your sutuation better? And where exactly do you want to do substitution?
 
Old 05-01-2011, 01:24 PM   #6
xombboxer
Member
 
Registered: Apr 2011
Posts: 63

Original Poster
Rep: Reputation: 0
ok

i had enough trouble with with expect and special character ')'.

i will replace ')' with say 'n' while passing the parameter and in the expect utility i will replace back 'n' with ')'

my requirement now is

how do i replace 'n' with ')' withing expect utility.
Code:
${mystring/)/.} 
Wont work within expect


please help me
 
Old 05-01-2011, 01:26 PM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
What is this "expect" you keep talking abut?
 
Old 05-01-2011, 01:27 PM   #8
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by MTK358 View Post
What is this "expect" you keep talking abut?
http://www.nist.gov/el/msid/expect.cfm
 
Old 05-01-2011, 01:29 PM   #9
xombboxer
Member
 
Registered: Apr 2011
Posts: 63

Original Poster
Rep: Reputation: 0
hi MTK358

its an utility used to automate things like automatic password entry etc . i too dont know much about it. i think its sort of TCL
 
Old 05-01-2011, 01:43 PM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
@xombboxer: You talk about two scripts, input from the prompt and expect. Do you mind posting what you have and telling which line(s) are not working?
 
Old 05-02-2011, 01:51 AM   #11
xombboxer
Member
 
Registered: Apr 2011
Posts: 63

Original Poster
Rep: Reputation: 0
first im calling
Code:
./main.sh pass="p:-\)10"
note that i am escaping ')' as '\)'

from main.sh again im calling one more sh file

Code:
../_exp.sh $PASS
were $PASS contains
Code:
"p:-\)10"
_exp.sh looks like this

Code:
#!/usr/bin/expect -f
.
.
.
set p [lrange $argv 0 0]
.
.
.

send -- "echo correct script \r"

send -- "echo valu  passwrd 0 argument = $p \r"

set send_human {.1 .25 2 .05 1.5}
set breaktheloop 0

while {$breaktheloop == 0} {
        expect "*Password:*"
        send -h "$p\r"
        expect -timeout -1  
	sleep 10
	set breaktheloop 1
the main idea is to pass the password as a parameter.

the problem is because of the special character ')'. it is correctly interpreted in main.sh. But when it goes to second shell file which is to handle expect utility, it does not recognize '\' as escape character. it treats "p:-\)10" as 7 character string but actually my password is 6 character string "p:-)10".

Last edited by xombboxer; 05-02-2011 at 01:53 AM.
 
Old 05-02-2011, 04:23 AM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,
Quote:
Originally Posted by xombboxer View Post
first im calling
Code:
./main.sh pass="p:-\)10"
note that i am escaping ')' as '\)'
Why this construct? It seems you need to split the pass part from the "p:-\)10" part inside your script.

Wouldn't ./main.sh "p:-\)10" (or ./main.sh 'p:-)10') be easier? Inside the script you can do: PASS="$1"

If you put single quotes around the input instead of double quotes, you do not need to escape the ) when starting the main script.

Quote:
Originally Posted by xombboxer
from main.sh again im calling one more sh file
Code:
../_exp.sh $PASS
Do you need to call a second script? Can't you incorporate the expect part into your first script? This way you can use the vars that are used in both the bash and the expect part.
Code:
#/bin/bash
.
bash stuff goes here
.

expect -c "
.
expect stuff goes here
.
"

more bash stuff
This way you won't need the set p [lrange $argv 0 0] part and can use $PASS instead of $p (send -h "$PASS\r")

Hope this helps.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash replace all matches of regex substring in string nickleus Linux - General 3 04-30-2011 12:08 PM
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 07:56 AM
Find and replace a string in a file using perl command from bash script koundinya749 Programming 5 02-15-2011 05:52 PM
How do I replace special characters in a string within a bash script? rhaup0317 Linux - Newbie 2 06-03-2008 12:56 PM
BASH: Replace string in file with another eur0dad Programming 5 07-27-2006 05:29 PM


All times are GMT -5. The time now is 08:10 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration