Issue
I have a file full of urls that looks like this:
https://testing/this/string/for/now
which I need to have them all replaced using sed specifically, to:
https://testing/this/now
and save the file at the end with the updated content. So actually remove whatever content exists in the 'string' and 'for'(no matter their length might be), but keep the latter 'now' part of the url.
Thanks in advance.
Vincent
Solution
You can use this sed command to remove 2 paths after https://testing/this/
:
sed -i.bak 's|\(https://testing/this/\)[^/]*/[^/]*/|\1|'' file
Explanation:
\(https://testing/this/\) # match and group https://testing/this/
[^/]*/ # match 0 or more of any character that is not /
[^/]*/ # match 0 or more of any character that is not /
In replacement we're using \1
which is back-reference to first capturing group.
Example:
s='https://testing/this/string/for/now'
sed 's|\(https://testing/this/\)[^/]*/[^/]*/|\1|' <<< "$s"
https://testing/this/now
Answered By - anubhava Answer Checked By - Pedro (WPSolving Volunteer)