<proxybypass var="proxybypass">xxx.yyy.com|\DDD.yyy.com</proxybypass>
Where DDD is server name as defined ${sname}
unable to pass the variable \ after xxx.yyy.com| when using below code:
in short I have to by pass |\ character in updateflag
updateflag="<proxybypass var=\"proxybypass\">xxx.yyy.com|\"${sname}".yyy.com</proxybypass>"
sed -i ''"$line_number"'a '"$updateflag"'' $xmlval
If it's possible change the updateflag, so that it contains an additional backslash (to escape the one you want to keep):
updateflag="...|\\\\${name}..."
Then:
sed "1a $updateflag" <<< "test"
Gives:
test
|\DDD
Related
I'm trying to add a line in a file afile.xyz using my script. This is what I've done so far using sed:
n="$(grep ".method" "$m" | grep "onCreate(Landroid/os/Bundle;)V")"
sed -i '' -e '/$n/ a\
"test", /Users/username/Documents/afile.xyz
I'm getting the error:
"onCreate\(\Landroid\/ ...": bad flag in substitute command: 'g'
How do I solve this? Please do help. Thanks.
Edit: Content of n
method protected onCreate(Landroid/os/Bundle;)V
2 problems:
because the sed body is in single quotes, the variable $n will not be expanded,
the regular expression in $n contains the / dilimiters.
Try this:
n=$(...)
nn=${n//\//\\/} # escape all slashes
sed -i '' '/'"${nn}"'/ a ...
The single-quoted sed body is interrupted to append the double quoted shell variable.
You can also use a different delimiter for the RE:
sed -i '' -e "\#$n# a\\
\"test\"" /Users/username/Documents/afile.xyz
I would like to edit a .mk file using Bash.
Inside the file, it looks like this:
SRC_PATHS = src/lib \
src/Application \
src/win \
src/prj
I would like to add a new source, which should look like this:
SRC_PATHS = src/lib \
src/Application \
src/win \
src/prj \
src/New
I am trying a sed command, but cannot add a new line.
Note: the last src path (src/prj) is not always the same.
If ed is available/acceptable.
#!/usr/bin/env bash
ed -s file.mk <<-'EOF'
$t.
-1s/$/ \\/
+s|\(^[[:blank:]]\{1,\}\) \(.\{1,\}\)$|\1 scr/new|
,p
Q
EOF
In-one-line
printf '%s\n' '$t.' '-1s/$/ \\/' '+s|\(^[[:blank:]]*\) \(.*\)$|\1 scr/new|' ,p Q | ed -s file.mk
with a shell variable to store the replacement.
#!/usr/bin/env bash
var='scr/new'
ed -s file.mk <<-EOF
\$t.
-1s/\$/ \\\/
+s|\(^[[:blank:]]\{1,\}\) \(.\{1,\}\)\$|\1 $var|
,p
Q
EOF
Remove the ,p to silence the output to stdout , it is there just to see what is the new outcome of the edited buffer.
Change Q to w if in-place editing is needed
JFYI, both the script and the one-liner are not limited to just bash it should work on any POSIX compliant shell.
With sed how about:
sed -i '$s#.\+#& \\'\\$'\n'' src/New#' file.mk
Result:
SRC_PATHS = src/lib \
src/Application \
src/win \
src/prj \
src/New
Considering the indentation of the input and of the desired result, which is not uniform between the first line and the others, I suspect that it is not important at all. If this is the case, then this sed command might work:
sed -z 's#\n$# \\\nsrc/New\n#' file.mk
where
-z is to treat the file as a single line/stream with embedded \ns
\n$ targets the EOF together with the last \n
the replacement string is \\\nsrc/New\n.
Thanks to all who answered, I tried all your suggestions, and here are the code snippets working and applicable to my needs:
sed -i '/^SRC_PATHS[\t ]*=/{:a;/\\$/{N;ba;};s,$, \\\n\tsrc/New,}' file.mk
there are some instances where there is already a "\" in the file, so I added new code to clean up those lines
sed -i '/^$*.\\/d' file.mk
then to add another path in the EOF:
sed -i '$s#.\+#& \\'\\$'\n'' src/New#' file.mk
output.txt:
select * from table startTime.... (very big query)
abc.rb:
it "example" do
time = 6537290102
replacestarttime = `sed -i -e 's/startTime/$time/g' ./db/output.txt`
end
expected output----
output.txt:
select * from table 6537290102....
In output.txt, the value should be 6537290102 please someone help. thanks in advance
If I hardcode the time value as shown below in sed command then it is working fine whereas the passing the dynamic value "time" is not working
replacestarttime = sed -i -e 's/startTime/6537290102/g' ./db/output.txt
Replace single quotes with double quotes
`sed -i -e "s/startTime/$time/g" ./db/output.txt`
Interpolate Your String in Ruby
Right now, you're expecting sed to access a shell variable named $time. This variable doesn't exist, and wouldn't work as-is even if it did.
To fix this, you should to interpolate your Ruby variable into the string passed to sed. You can do this as follows:
it "example" do
time = 6537290102
replacestarttime = `sed -i -e 's/startTime/#{time}/g' ./db/output.txt`
end
I've been throught all possible answers in SO but still I cannot make my script work.
I have a query script in SPARQL with a line that needs to substitute the mark %contributor% by a variable between "<>"
?link dcterms:contributor %contributor%
Into
?link dcterms:contributor <http://newurl>
If I execute the code in the shell, the substitution is done properly and I see my query with the %contributor% tag successfully substituted. But when executed inside a Makefile, even with the double $$ dollar to allow the expansion of variables I don't manage to get it done.
for l in `cat sref.csv`; do \
QUERY=$$(cat queries/table_knowledge.rq | sed "s#%contributor%#<$$l>#g") ; \
echo $$QUERY ; \
done
Also like this it does not work:
QUERY=$$(cat queries/table_knowledge.rq | sed 's/%contributor%/<$(l)>/g') ; \
or
QUERY=$$(cat queries/table_knowledge.rq | sed 's/%contributor%/<${l}>/g') ; \
Use double quotes so that sed would expand variables.
Use a separator different than / since the replacement contains /
QUERY=$(cat queries/table_knowledge.rq | sed "s|%contributor%|<${l}>|g") ;
Test:
$ test="http://test.com"
$ echo "?link dcterms:contributor %contributor%" > source.file
$ x=$(cat source.file|sed "s|%contributor%|<${test}>|g")
$ echo $x
?link dcterms:contributor <http://test.com>
I'm trying to use sed to change a variable in the site.js file on my server.
Here is the line: var url = "page.php"; I'm looking to just substitute page.php for whatever.php.
I thought this would be pretty simple and I figured this would work with no issues:
sed -i "s/\url = \".*\"/\url = \"page2.php\"/" /home/site.js
It works okay except instead of getting: var url = "page2.php"; I get: var R1 = "page2.php";
Why is the url value being changed to R1 when I use sed here?
You don't need \ before url.
sed -i -r 's#url\s*=\s*"[^"]+"#url = "page2.php"#' /home/site.js
Extra escaping of " can be eliminated by enclosing sed expression with ' instead of "
It's better to use different separator than / (here #) when the strings themselves may contain /
Try doing this :
sed -i -r 's#(var\s+url\s*=\s*")[^"]+"#\1whatever.php"#' file.js
/ is not mandatory as delimiter, I've picked up # there.
Here's another example: Took me while to figure that you change the / for delimiter and not the / in the directory path.
Use # instead of / for sed delimiter if you have dir path names.
First I tried this:
[root#ip-172-35-24-37 ec2-user]# egrep -q "^(\s*\S+\s+)/dev/shm(\s+\S+\s+\S+)(\s+\S+\s+\S+)(\s*#.*)?\s*$" /etc/fstab && sed -ri "s/^(\s*\S+\s+)/dev/shm(\s+\S+\s+\S+)(\s+\S+\s+\S+)(\s*#.*)?\s*$/\1/dev/shm\2nodev\3\4/" /etc/fstab
And got this error:
sed: -e expression #1, char 20: unknown option to `s'
So then I used # for the sed delimiter instead of /:
[root#ip-172-35-24-37 ec2-user]# egrep -q "^(\s*\S+\s+)/dev/shm(\s+\S+\s+\S+)(\s+\S+\s+\S+)(\s*#.*)?\s*$" /etc/fstab && sed -ri "s#^(\s*\S+\s+)/dev/shm(\s+\S+\s+\S+)(\s+\S+\s+\S+)(\s*#.*)?\s*$#\1/dev/shm\2nodev\3\4##" /etc/fstab
[root#ip-172-35-24-37 ec2-user]#
And it worked.
You can use something else besides # for a delimiter like ! or ? or %. Just don't use / if you have dir paths.