LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 09-17-2008, 01:19 PM   #1
babag
Member
 
Registered: Aug 2003
Posts: 365

Rep: Reputation: 30
how to replace with variable using sed?


i have the following:
Code:
variable=$(sed -n '3p' truefalse.txt)

echo $variable

if [ "$variable" = "True" ] ; then
     
     sed -i '3 cFalse' truefalse.txt
else
     
     sed -i '3 cTrue' truefalse.txt
fi

variable=$(sed -n '3p' truefalse.txt)

echo $variable
this reads line three of a simple text file and
changes True to False or False to True depending
on what the line says.

how would i replace the '3' with a variable?
what is the syntax for that?

i tried a couple of things but was just guessing
and it didn't work.

thanks,
BabaG
 
Old 09-17-2008, 01:27 PM   #2
w3bd3vil
Senior Member
 
Registered: Jun 2006
Location: Hyderabad, India
Distribution: Fedora
Posts: 1,189

Rep: Reputation: 49
Here is how you do that.
Quote:
bash-3.1# TEST=3
bash-3.1# export TEST
bash-3.1# cat test.txt
1
2
3
4
5
bash-3.1# sed -n $TEST'p' test.txt
3
 
Old 09-17-2008, 02:41 PM   #3
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

here's a simple example:
Code:
jan@jack:~/tmp/tf> cat data
True
False
False
True
True
True
False
jan@jack:~/tmp/tf> LINE=3
jan@jack:~/tmp/tf> sed "$LINE{s/True/_/;s/False/True/;s/_/False/}" data
True
False
True
True
True
True
False
jan@jack:~/tmp/tf> LINE=5
jan@jack:~/tmp/tf> sed "$LINE{s/True/_/;s/False/True/;s/_/False/}" data
True
False
False
True
False
True
False
If you want to use variables within a sed, you may not use '' around the sed script - no shell substitutions are done (this is a shell feature, not limited to sed). You must use "" instead or you place the variable outside the '' - for example:
Code:
sed 's/'"$VAR"'/something/' file
Jan
 
Old 09-17-2008, 04:16 PM   #4
babag
Member
 
Registered: Aug 2003
Posts: 365

Original Poster
Rep: Reputation: 30
thanks to you both! exactly what i was looking for.

BabaG
 
Old 09-17-2008, 04:28 PM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
If a part of the sed command is provided by a variable, you need to be wary of whether the variable may contain a character such as a forward slash, which you would normally escape. If the variable contains a pathname for example, consider using another character such as "#" instead of a slash.

There is a trick you can try to test if the arguments are being passed as you believe. This may help with debugging.

set sed "$VAR"'s#'$var1'#old-'"$var1"'#p' file
echo $2

The arguments for set will be in $0, $1, etc. So you can echo them and see how the variable expansion went. The "sed" command will be in $1. The arguments to sed start with $2.
 
  


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
sed search replace tomerbd1 Linux - General 9 04-10-2008 05:31 AM
How do I replace ' with sed.... @ngelot Linux - Newbie 2 11-02-2007 08:04 PM
Replace variable with user defined variable ce124 Programming 10 04-13-2007 10:29 AM
Replace substring with SED marri Programming 2 07-09-2005 06:18 PM
[sed] replace string? chuanyung Programming 3 03-11-2004 09:42 PM


All times are GMT -5. The time now is 06:44 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration