Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 02-10-2006, 08:46 AM   #1
Registered: Jan 2006
Location: india
Distribution: FEDORA CORE 3
Posts: 103

Rep: Reputation: 15
problem with sed n variable in shell script

hii ..
i have a problem in using sed .. awk is very complicated for me .. my task is a simple stuff .. i have to create a script in which i have to take three input
2>first word
3> second word

in this script i have to just replace every instance of first word by second word in file supplied

here is my silly script
ls $1 > /dev/null
if [ $? -eq 1 ]
echo file not found
cat $1 | sed 's/$2/$3/g'

when i supply constant string instead of variable .. its doing my work .. like if i give
instead of $2 and $3 like

sed 's/dipen/ass/g'

its replacing dipen by ass .. in the file ..
so again i m stuck by not able to expand my variable ..

i tried everything .. somethings i tried were

sed 's/`echo $2`/`echo $2`/g'
sed 's/${!1}/${!2}/g'

i have tried using escape sequence almost everywhere ..

thnks in advance
Old 02-10-2006, 09:04 AM   #2
Senior Member
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
try sed "s/$2/$3/g" instead of sed 's/$2/$3/g'
Old 02-10-2006, 03:54 PM   #3
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
yea, to see what is actually happening, try this:

echo "$PATH"
echo "$PATH"

single quotes escape special characters in the shell.

echo '$PATH' is the same as echo "\$PATH"
Old 02-11-2006, 01:31 AM   #4
Registered: Jan 2006
Location: india
Distribution: FEDORA CORE 3
Posts: 103

Original Poster
Rep: Reputation: 15
thanks agrouf and sirclif ... i really need to read some tutorial on expanding variable .. its the second time i have landed into this puddle ..

... but expanding variable is one concept ... escaping special character n so on ...
got anything in ur bookmarks related to scripting ..? if u got any link then plz lemme know the link
thanks a ton guys ..
Old 02-12-2006, 11:54 PM   #5
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,340

Rep: Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745

try these :-)


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
shell script problem on sed. chooi Programming 3 02-11-2006 12:35 AM
Odd problem with making a variable the output of a command in a shell script linux=future Programming 3 12-13-2005 10:45 PM
sed doesn't accept $variable in bash script chii-chan Programming 6 05-28-2005 08:07 AM
Expanding shell variable in sed goofyheadedpunk Programming 4 09-11-2004 06:17 PM
delete line specified by variable wihin shel script with sed lnchatterbox Linux - Newbie 2 02-23-2004 02:24 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:44 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration