Issue
Want to rename the (known) 3th folder within a (unknown) file path from a string, when positioned on 3th level while separator is /
Need a one-liner explicitly for sed
. Because I later want use it for tar --transform=EXPRESSION
string="/db/foo/db/bar/db/folder"
echo "$string" | sed 's,db,databases,'
sed replace "db" only on 3th level
expected result
/db/foo/databases/bar/db/folder
Solution
You could use a capturing group to capture /db/foo/
and then match db
. Then use use the first caputring group in the replacement using \1
:
string="/db/foo/db/bar/db/folder"
echo -e "$string" | sed 's,^\(/[^/]*/[^/]*/\)db,\1databases,'
About the pattern
^
Start of string\(
Start capture group/[^/]*/[^/]*/
Match the first 2 parts using a negated character class
\)
Close capture groupdb
Match literally
That will give you
/db/foo/databases/bar/db/folder
Answered By - The fourth bird