Reputation: 452
I have a file containing a lot paths. I need to replace all those path (containing build numbers) by the current path. In other word in my file I have like :
/build/8b7yrg_k/12/src/main/myfile
and I want (if I'm in /build/home/0/):
/build/home/0/src/main/myfile
What I'm trying is to do :
sed 's,/build/'.+'/'[0-9]+'/','$PWD',g
But the the command doesn't find the pattern. I also tried to add \ or to remove the ' but I don't manage to match the desired patterns
Upvotes: 2
Views: 1768
Reputation: 627607
There are several issues:
+
is treated as a literal char, not a quanitifierYou can use
sed -E 's,/build/[^/]+/[0-9]+/,'"$PWD"/',g'
sed 's,/build/[^/]*/[0-9]*/,'"$PWD"/',g'
Details:
/build/[^/]+/[0-9]+/
is a POSIX ERE regex (enabled with -E
) that matches /build/
, one or more chars other than /
, then a /
, one or more digits and then a /
chr/build/[^/]*/[0-9]*/
is a POSIX BRE pattern that does the same as above"$PWD"/
- the current directory variable is inside double quotes, and the /
is added to make this replacement pattern work.See the online demo:
#!/bin/bash
s='/build/8b7yrg_k/12/src/main/myfile'
sed -E 's,/build/[^/]+/[0-9]+/,'"$PWD"/',g' <<< "$s"
sed 's,/build/[^/]*/[0-9]*/,'"$PWD"/',g' <<< "$s"
Output:
/home/3octv4/src/main/myfile
/home/3octv4/src/main/myfile
Upvotes: 2