LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 02-10-2006, 07:46 AM   #1
dipenchaudhary
Member
 
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
1>filename
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
#!/bin/bash
ls $1 > /dev/null
if [ $? -eq 1 ]
then
echo file not found
fi
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, 08:04 AM   #2
Agrouf
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, 02:54 PM   #3
sirclif
Member
 
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"
and
echo "$PATH"

single quotes escape special characters in the shell.

echo '$PATH' is the same as echo "\$PATH"
 
Old 02-11-2006, 12:31 AM   #4
dipenchaudhary
Member
 
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, 10:54 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
http://rute.2038bug.com/index.html.gz
http://www.tldp.org/LDP/abs/html/index.html

try these :-)
 
  


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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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