Regular expression in sed - regex

I try to use regex in a sed command. I try to replace a pattern. My pattern is a line in /etc/security/policy.conf file. The value is unknown it should be either:
#LOCK_AFTER_RETRIES=NO or LOCK_AFTER_RETRIES=4 (or any number here). I want to replace found pattern with the value LOCK_AFTER_RETRIES=6. So I tried the command:
sed 's/#*LOCK_AFTER_RETRIES=[a-zA-Z0-9][a-zA-Z0-9]/LOCK_AFTER_RETRIES=6/g' ./policy.conf
But it didn't look to be working.
Thanks.

give this a try:
sed 's/^#\?\s*LOCK_AFTER_RETRIES=[^=]*/LOCK_AFTER_RETRIES=6/'
test with some example:
# LOCK_AFTER_RETRIES=NO
#LOCK_AFTER_RETRIES=NO
LOCK_AFTER_RETRIES=4
LOCK_AFTER_RETRIES=4
kent$ sed 's/^#\?\s*LOCK_AFTER_RETRIES=[^=]*/LOCK_AFTER_RETRIES=6/' f
LOCK_AFTER_RETRIES=6
LOCK_AFTER_RETRIES=6
LOCK_AFTER_RETRIES=6
LOCK_AFTER_RETRIES=6

Is this what you want?
sed -i 's/LOCK_AFTER_RETRIES=.*/LOCK_AFTER_RETRIES=6/g' filename
I test like this and it works:
echo "LOCK_AFTER_RETRIES=4\n#LOCK_AFTER_RETRIES=NO" | sed 's/LOCK_AFTER_RETRIES=.*/LOCK_AFTER_RETRIES=6/g'
[EDIT]
After checking other's answer, I am not sure whether you want to replace #LOCK_AFTER_RETRIES=NO to LOCK_AFTER_RETRIES=6 or #LOCK_AFTER_RETRIES=6.
My above answer will change #LOCK_AFTER_RETRIES=NO to #LOCK_AFTER_RETRIES=6
If you want to change change #LOCK_AFTER_RETRIES=NO to LOCK_AFTER_RETRIES=6, try:
sed -i 's/^#*LOCK_AFTER_RETRIES=.*/LOCK_AFTER_RETRIES=6/g' filename
In summary:
echo "LOCK_AFTER_RETRIES=4\n#LOCK_AFTER_RETRIES=NO" | sed 's/LOCK_AFTER_RETRIES=.*/LOCK_AFTER_RETRIES=6/g'
LOCK_AFTER_RETRIES=6
#LOCK_AFTER_RETRIES=6
echo "LOCK_AFTER_RETRIES=4\n#LOCK_AFTER_RETRIES=NO" | sed 's/^#*LOCK_AFTER_RETRIES=.*/LOCK_AFTER_RETRIES=6/g'
LOCK_AFTER_RETRIES=6
LOCK_AFTER_RETRIES=6

Try this:
sed '/LOCK_AFTER_RETRIES/s/=.*/=6/g' ./policy.conf

Related

How to cut a string from a string

My script gets this string for example:
/dir1/dir2/dir3.../importance/lib1/lib2/lib3/file
let's say I don't know how long the string until the /importance.
I want a new variable that will keep only the /importance/lib1/lib2/lib3/file from the full string.
I tried to use sed 's/.*importance//' but it's giving me the path without the importance....
Here is the command in my code:
find <main_path> -name file | sed 's/.*importance//
I am not familiar with the regex, so I need your help please :)
Sorry my friends I have just wrong about my question,
I don't need the output /importance/lib1/lib2/lib3/file but /importance/lib1/lib2/lib3 with no /file in the output.
Can you help me?
I would use awk:
$ echo "/dir1/dir2/dir3.../importance/lib1/lib2/lib3/file" | awk -F"/importance/" '{print FS$2}'
importance/lib1/lib2/lib3/file
Which is the same as:
$ awk -F"/importance/" '{print FS$2}' <<< "/dir1/dir2/dir3.../importance/lib1/lib2/lib3/file"
importance/lib1/lib2/lib3/file
That is, we set the field separator to /importance/, so that the first field is what comes before it and the 2nd one is what comes after. To print /importance/ itself, we use FS!
All together, and to save it into a variable, use:
var=$(find <main_path> -name file | awk -F"/importance/" '{print FS$2}')
Update
I don't need the output /importance/lib1/lib2/lib3/file but
/importance/lib1/lib2/lib3 with no /file in the output.
Then you can use something like dirname to get the path without the name itself:
$ dirname $(awk -F"/importance/" '{print FS$2}' <<< "/dir1/dir2/dir3.../importance/lib1/lib2/lib3/file")
/importance/lib1/lib2/lib3
Instead of substituting all until importance with nothing, replace with /importance:
~$ echo $var
/dir1/dir2/dir3.../importance/lib1/lib2/lib3/file
~$ sed 's:.*importance:/importance:' <<< $var
/importance/lib1/lib2/lib3/file
As noted by #lurker, if importance can be in some dir, you could add /s to be safe:
~$ sed 's:.*/importance/:/importance/:' <<< "/dir1/dirimportance/importancedir/..../importance/lib1/lib2/lib3/file"
/importance/lib1/lib2/lib3/file
With GNU sed:
echo '/dir1/dir2/dir3.../importance/lib1/lib2/lib3/file' | sed -E 's#.*(/importance.*)#\1#'
Output:
/importance/lib1/lib2/lib3/file
pure bash
kent$ a="/dir1/dir2/dir3.../importance/lib1/lib2/lib3/file"
kent$ echo ${a/*\/importance/\/importance}
/importance/lib1/lib2/lib3/file
external tool: grep
kent$ grep -o '/importance/.*' <<<$a
/importance/lib1/lib2/lib3/file
I tried to use sed 's/.*importance//' but it's giving me the path without the importance....
You were very close. All you had to do was substitute back in importance:
sed 's/.*importance/importance/'
However, I would use Bash's built in pattern expansion. It's much more efficient and faster.
The pattern expansion ${foo##pattern} says to take the shell variable ${foo} and remove the largest matching glob pattern from the left side of the shell variable:
file_name="/dir1/dir2/dir3.../importance/lib1/lib2/lib3/file"
file_name=${file_name##*importance}
Removeing the /file at the end as you ask:
echo '<path>' | sed -r 's#.*(/importance.*)/[^/]*#\1#'
Input /dir1/dir2/dir3.../importance/lib1/lib2/lib3/file
Returns: /importance/lib1/lib2/lib3
See this "Match groups" tutorial.

Sed replace domain in URL

I have these strings http://sub.domain.com/myuri/default.aspx, https://sub.domain.com/myuri/default.aspx and https://domain.com
Is it possible to use sed to replace only the domain part?
For example, this URL:
http://sub.domain.com/myuri/default.aspx
Would become:
http://anotherdomain.com/myuri/default.aspx
Please note that the protocol may differ between https and http.
I did search but could not find something similar.
You will need non-greedy pattern that sed can't offer, use perl instead:
perl -pe '/(http|https):\/\/(.*?)(\/|$)/ && s/$2/anotherdomain/g'
Edit:
awk also does the job well and it's even simpler actually:
awk -F/ 'gsub($3,"anotherdomain",$0)' <<< "$urls"
Example:
#!/bin/bash
urls=$(cat << 'EOF'
https://sub.domain.com/myuri/default.aspx
http://sub.domain.com/myuri/default.aspx
http://blabla
EOF
)
perl -pe '/(http|https):\/\/(.*?)(\/|$)/ && s/$2/anotherdomain/g' <<< "$urls"
Output:
bash test.sh
https://anotherdomain/myuri/default.aspx
http://anotherdomain/myuri/default.aspx
http://anotherdomain
If I follow your question, then yes sed 's/sub\.domain\.com/anotherdomain\.com/1' -
echo "http://sub.domain.com/myuri/default.aspx" | \
sed 's/sub\.domain\.com/anotherdomain\.com/1'
Output is
http://anotherdomain.com/myuri/default.aspx
And with,
echo "https://sub.domain.com/myuri/default.aspx" | \
sed 's/sub\.domain\.com/anotherdomain\.com/1'
Output is
https://anotherdomain.com/myuri/default.aspx
You can use sed like this:
sed -r 's|(https?://)[^/]+([[^:blank:]]*)|\1anotherdomain.com\2|g' file
http://anotherdomain.comn.com/myuri/default.aspx
https://anotherdomain.comn.com/myuri/default.aspx
https://anotherdomain.comn.com
PS: Use sed -E on OSX.
Based on #hek2mgl's solution:
SERVER=www.example.com
sed "s=\(https\?://\)[^/]\+=\1$SERVER=" \
<<< 'https://anotherdomain.com/myuri/default.aspx'
It will output:
https://www.example.com/myuri/default.aspx
Modifications from hek2mgl's sed line:
a little shorter (no need to catch the part after domain name to paste it as is in replacement)
deals with both http:// and https:// syntax
You can use sed:
SERVER=www.example.com
sed "s~https\?://\([^/]\+\)\(.*\)~http://$SERVER\2~" <<< "http://newsub.domain.com/myuri/default

Sed substitute input by first matching argument

I'm trying to get some sed command to work without success...
echo -e "This.Is.a.Test.V03.r501.dump" | sed "s/^\(\w+(\.\w+)*\)\.V[0-9]{2}.*$/\1/g"
Basically, I want to match and return This.Is.a.Test while this \.V[0-9]{2} is fixed, but instead it returns the whole input string.
Any help is appreciated, thanks in advance!
\w matches alphanumerics, you are looking to capture only alphabets, so replace \w with [:alpha:]. Additionally {2} needs to be replaced with \{2\}. The following works with GNU sed
echo -e "This.Is.a.Test.V03.r501.dump" |
sed "s/^\([[:alpha:].]\+\)\.V[0-9]\{2\}.*$/\1/g"
This.Is.a.Test
Try this.
echo -e "This.Is.a.Test.V03.r501.dump" | sed -e "s/\(.*\)\.V[0-9]*.*/\1/"
Another way with sed
sed -r 's/^(([^.]+.){3})([^.]+).*/\1\3/'
Are you looking for this?
One way is to use awk
$ echo "This.Is.a.Test.V03.r501.dump" | awk -F'.' 'BEGIN{OFS=FS}{NF=4}1'
This.Is.a.Test

sed: mix explicit and regex phrases

I'm trying to write a sed command to remove a specific string followed by two digits. So far I have:
sed -e 's/bizzbuzz\([0-9][0-9]\)//' file.txt
but I cant seem to get the syntax right. Any suggestions?
sed -re 's/bizzbuzz[0-9]{2}//' file.txt
and
sed -re 's/\bbizzbuzz[0-9]{2}\b//' file.txt
if the searched string have word boundary
sed -e 's/bizzbuzz[0-9]\{2\}//' file.txt
if you don't have GNU sed
Your current approach seems like it should work fine:
$ echo 'FOO bizzbuzz56 BAR' | sed -e 's/bizzbuzz\([0-9][0-9]\)//'
FOO BAR
As said in other answer, the syntax seems to be fine (with unnecesary parenthesis).
But may be you want to replace all the strings found in each line ? In that case, you should add a 'g' at the end of the 's' command:
sed -e 's/bizzbuzz\([0-9][0-9]\)//g' file.txt

Using sed and regex to capture last part of url

I'm trying to make sed match the last part of a url and output just that. For example:
echo "http://randomurl/suburl/file.mp3" | sed (expression)
should give the output:
file.mp3
So far I've tried sed 's|\([^/]+mp3\)$|\1|g' but it just outputs the whole url. Maybe there's something I'm not seeing here but anyways, help would be much appreciated!
this works:
echo "http://randomurl/suburl/file.mp3" | sed 's#.*/##'
basename is your good friend.
> basename "http://randomurl/suburl/file.mp3"
=> file.mp3
This should do the job:
$ echo "http://randomurl/suburl/file.mp3" | sed -r 's|.*/(.*)$|\1|'
file.mp3
where:
| has been used instead of / to separate the arguments of the s command.
Everything is matched and replaced with whatever if found after the last /.
Edit: You could also use bash parameter substitution capabilities:
$ url="http://randomurl/suburl/file.mp3"
$ echo ${url##*/}
file.mp3
echo 'http://randomurl/suburl/file.mp3' | grep -oP '[^/\n]+$'
Here's another solution using grep.