how to replace with variable using sed?
i have the following:
Code:
variable=$(sed -n '3p' truefalse.txt) 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 |
Here is how you do that.
Quote:
|
Moin,
here's a simple example: Code:
jan@jack:~/tmp/tf> cat data Code:
sed 's/'"$VAR"'/something/' file |
thanks to you both! exactly what i was looking for.
BabaG |
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. |
All times are GMT -5. The time now is 11:00 PM. |