This question already has answers here:
Sed replace variable in double quotes
(3 answers)
Closed 3 years ago.
I've already looked at the following:
Escape a string for a sed replace pattern
Is it possible to escape regex metacharacters reliably with sed
They give absolutely no easy to understand answer.
version=1.2.3
sed -i -z -e 's/"version": "[A-Za-z0-9_.-]*"/"version": "$(version)"/' package.json
I'm trying to use a variable in a replace regular expression in a file. I don't have to use sed per say, as long as it works on macs and linux dists I'm ok with it.
So I just had to use double quote for sed to recognise the 4version variable, then just escape the double quotes which are included otherwise.
sed -i -z -e "s/\"version\": \"[A-Za-z0-9_.-]*\"/\"version\": \"$version\"/" package.json
Related
This question already has answers here:
What delimiters can you use in sed? [duplicate]
Using different delimiters in sed commands and range addresses
(3 answers)
How to insert strings containing slashes with sed? [duplicate]
(11 answers)
Closed 2 years ago.
I'm studying Regex and I faced a example that convertes dates formats:
sed -r 's|([0-9]{2})/([0-9]{2})/([0-9]{4})|\3/\2/\1|' <<< 05/04/1947
Output:
1947/04/05
I understand why this works but I'm not sure why the author used | 'pipe' instead of '/' to separate the regexp, replacement and flags sections.
I think changing | to / would make the same output:
sed -r 's/([0-9]{2})/([0-9]{2})/([0-9]{4})/\3/\2/\1/' <<< 05/04/1947
Why the last example does not work like the first one?
Thanks!
This question already has answers here:
How to escape single quotes within single quoted strings
(25 answers)
Closed 4 years ago.
How can I make this regular expression work correctly. Is adding the line
LOGIN_SERVICE: '"https://dev-login-o365.grey.com/gp_loginservice/"',
in the file env.js I'm using the regular expresion:
sed -i '5i LOGIN_SERVICE: '"https://login.xxxx.com/server/"',' ./env.js
But is adding the value without the quotes and 3 spaces more to the left like this:
Use different quoting:
sed -i "5i LOGIN_SERVICE: '\"https://login.xxxx.com/server/\"'," env.js
Or as the helpful comments below suggest, wrap sed command in single quote:
sed '5i LOGIN_SERVICE: '\''"https://login.xxxx.com/server/"'\'',' env.js
This question already has answers here:
Escaping the exclamation point in grep?
(2 answers)
Closed 5 years ago.
I want to find files that are scripts and I need to get from these files the list of all interpreters like Bash, sh, etc.
To find that, I use:
grep "#!/bin/*" ./*
But it displays that:
-bash: !/bin/*": event not found
I assume I need to escape # symbol somehow, but I didn't find that symbol to be escaped in documentation of BRE.
And how I can find files that contain this pattern in regex only on the first line of the file?
the # is no problem, you should escape the !, in Bash it refers to a previous command and must be followed by something, $ for the previous command or a number representing the index of the command in the history. (thx Aaron's correction)
also you may want to change * into .*
like grep "#\!/bin/.*"
If you don't want to escape !, use single quote like:
grep '#!/bin' ....
Also you can disable the regex match by using -F
Could you please try following find command and let me know if this helps.
find -type f -exec grep -l '#!/bin*' {} \+
Escape the ! with \:
grep "#\!/bin/*" ./*
I have this regex for now
It should catch something like this
org.package;version="[1.0.41, 1.0.51)" and "," optionally if it is not last element.
Also if after package i added .* because the package could be "org.package.util.something" until ";version"
I tried it online in Regex tool and it is working like this
org.package(.*.*)?;version="[[0-9].[0-9].[0-9][0-9],\s[0-9].[0-9].[0-9][0-9])",?
but i dont know what should i change so it can work in bash
package="org.package"
sed -i "s/"$$package.*;version="\[[0-9].[0-9].[0-9][0-9],[[:space:]][0-9].[0-9].[0-9][0-9]\)",?"//g" "$file"
Change the double quotes arround sed command by single quotes, because variable expansion of $package single quotes are closed and double quotes are use arround variable
package="org.package"
sed -i 's/'"$package"'.*;version="\[[0-9].[0-9].[0-9][0-9],[[:space:]][0-9].[0-9].[0-9][0-9]\)",?//g' "$file"
before using command with -i option check the output is correct
There is more than one problem
$$ will be replaced by bash with its PID, that's probably not what you want
online regex evaluators usually use extended regex or perl regex syntax
sed -r will enable extended regex mode. (for grep there's -E and -P)
You use . when you want to match literal dots. However you should be using \., because . actually means "any character" in regular expressions.
This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 7 years ago.
There's something I'm having trouble understanding concerning sed behavior.
sed -n "/pattern/,$p" < input.inp > output.out
gives the following error
sed: -e expression n°1, caractère 10: `,' inattendue
(my system is in french).
sed -n '/pattern/,$p' < input.inp > output.out
Works fine
I've personnally used commands like
sed -n "/begin/,/end/p" < input.inp > output.out
with both single or double quotes, and they work just fine.
In case it's useful, I have sed version: sed (GNU sed) 4.2.2
In double quotes, the shell, not sed, will evaluate $p. Since you probably haven't set a variable named p, sed will only see /pattern/,. To prevent this from happening, you'd need to escape the $ to the shell, by writing \$ instead:
sed -n "/pattern/,\$p" < input.inp > output.out
(You can imagine that using single quotes is a lot easier on the eyes and brain, unless you need shell variables in your expression.)