How to use variables in bash sed command, specific example
You have "$" in front of the variables when you assign them which is incorrect.
To use shell variables that are concatenated with text or variables surround them with curly-braces so the shell knows where the variables end. In your example the shell is looking for the variable $endp
which doesn't exist.
start=1641
end=1804
sed -n "${start},${end}p" oldfile.txt > newfile.txt
How to use variables in SED command (bash)
Does this give you your desired output?
sed -i "10,\$s/-.*/-$LOWX:HIGHX+$HIGHX]/" plot.p
sed -i "11,\$s/+.*/+$HIGHY]/" plot.p
sed -i "14,\$s/+.*/+$HIGHY/" plot.p
How to use variables in a command in sed?
Say:
sed "s|\$ROOT|${HOME}|" abc.sh
Note:
- Use double quotes so that the shell would expand variables.
- Use a separator different than
/
since the replacement contains/
- Escape the
$
in the pattern since you don't want to expand it.
EDIT: In order to replace all occurrences of $ROOT
, say
sed "s|\$ROOT|${HOME}|g" abc.sh
Use a variable's value in a sed command
you need to use double quotes ("
) instead of single quotes ('
).
single quotes pass their content literally, without translating variables (expansion).
try
sed "24s/.*/\"$ct_tname\"/" file1.sas > file2.sas
btw, if you're going to be editing a file (that is if file2.sas is a temporary file), you should be using ed
instead.
Using variables in sed
You got the double-quotes wrong:
sed -i '' “s/mycomputer/$localip/” config.txt
This should work (notice the difference):
sed -i '' "s/mycomputer/$localip/" config.txt
Actually you have similar problems on other lines too. So the full script, corrected:
#!/bin/bash
cd $(dirname "$0")
localip=$(ipconfig getifaddr en0)
sed -i '' "s/mycomputer/$localip/" config.txt
Note that -i ''
is for the BSD version of sed
(in BSD systems and MAC). In Linux, you'd write the same thing this way:
sed -i "s/mycomputer/$localip/" config.txt
How to use variables in sed command
Does this help as a start?
echo ''
OU1="QA"
echo "subject= /C=US/O=AAA/OU=${OU1}/OU=12345/OU=TESTAPP/" \
| sed -e "s|/OU=${OU1}/|/OU=\$\$\$${OU1}/|g"
The result is:
subject= /C=US/O=AAA/OU=$$$QA/OU=12345/OU=TESTAPP/
(You are mixing up the use of $ signs .)
How to use bash variables in sed command?
sed
is overkill for this. Use parameter expansion:
NEWBASE=${NAME//$DIR//}
NEWBASE=${NEWBASE//.\//}
Use a variable as replacement in bash sed command
TL;DR:
Try:
sed -i '$ s@$@ '"$1"'@' "$DIR./result/doc.md"
Long version:
Let's start with your original code:
sed -i '$ s/$/ /replacement/' "$DIR./result/doc.md"
And let's compare it to the code you referenced:
sed -i '$ s/$/abc/' file.txt
We can see that they don't exactly match up. I see that you've correctly made this substitution:
file.txt --> "$DIR./result/doc.md"
That looks fine (although I do have my doubts about the . after $DIR ). However, the other substitution doesn't look great:
abc --> /replacement
You actually introduced another delimeter /. However, if we replace the delimiters with '@' we get this:
sed -i '$ s@$@ /replacement@' "$DIR./result/doc.md"
I think that the above is perfectly valid in sed/bash. The $@
will not be replaced by the shell because it is single quoted. The $DIR
variable will be interpolated by the shell because it is double quoted.
Looking at one of your attempts:
sed -i "$ s@$@ $1@" "$DIR./result/doc.md"
You will have problems due to the shell interpolation of $@ in the double quotes. Let's correct that by replacing with single quotes (but leaving $1 unquoted):
sed -i '$ s@$@ '"$1"'@' "$DIR./result/doc.md"
Notice the '"$1"'. I had to surround $1 with '' to basically unescape the surrounding single quotes. But then I surrounded the $1 with double quotes so we could protect the string from white spaces.
How to use variable in sed search pattern
It looks like you're trying to use the sed
range notation, i.e. /start/,/end/?. Is that correct?
If so all you need to do is add the additional '/' chars that are missing, i.e.
sed -n "/$VARIABLE/,/test/p" aFile
A range can be composed of line numbers, strings/regexs, and/or relative line numbers (with no negative look back).
sed -n "1,20p" aFile
# prints lines 1-20
sed -n '/start/,/end/p' aFile
# prints any text (include current line)
# with start until it finds the word end
sed -n '/start/,+2p' aFile
# prints line matching start and 2 more lines after it
I am using strings in a /regex/
pattern to simplfy the explanation.
You can do real rex-ex's like /^[A-Za-z_][A-Za-z0-9_]*/
etc. as you learn about them.
Also per your comment about '$', sed will also use '$' as an indicator of 'end-of-line' IF the character is visible in the reg-ex that is being evaluated. Also note how some of my examples use dbl-quotes OR single-quotes. Single quotes mean that your expression sed -n '/$VARIABLE/,/test/p' aFile
would match the literal chars AND because the $ is not at the end of a reg-ex, it will be used as a regular character. The '$' only applies as end-of-line, when it is at the end of a reg-ex (part); for example you could do /start$|start2$/
, and both of those would signify end-of-line.
As others have pointed out, your use of
sed -n "/$VARIABLE/,/test/p" aFile
is being converted via shell variable explasion to
sed -n "/some text/,/test/p" aFile
SO if you wanted to ensure your text was anchored at the end-of-line, you could write
sed -n "/$VARIABLE$/,/test/p" aFile
which expands to
sed -n "/some text$/,/test/p" aFile
I hope this helps
How to use variable in the sed command using PHP to replace line
Maybe you should extract variables out of your string so as to make sure the '$' is not treated as a character ?
$var = exec("sed -i '/".$var1."/c ".$var1." ".$var2".' t1.conf");
Related Topics
How to Get the Bssid of Currently Connected Network Through Bash
How to Prompt For Yes/No/Cancel Input in a Linux Shell Script
Confused About Stdin, Stdout and Stderr
How to Debug the Linux Kernel With Gdb and Qemu
Syntax Error in Shell Script With Process Substitution
How to Get Terminal'S Character Encoding
How to Loop Over Directories in Linux
Ipc Performance: Named Pipe VS Socket
Linux/Unix Command to Determine If Process Is Running
Error:13 - Permission Denied Android Studio
How to Compare Two Strings in Dot Separated Version Format in Bash
How to Change the Root Directory of an Apache Server
How to Print a Number in Assembly Nasm
Pipe To/From the Clipboard in a Bash Script
Run an Untrusted C Program in a Sandbox in Linux That Prevents It from Opening Files, Forking, etc.