sed or awk to replace string between pattern (convert absolute path to relative)
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I am trying to replace all folders in a path with ".."
This is part of a bigger script that will eventually replace some absolute paths into relative paths.
The expected output should look like this:
/ABCDdasda234sEA/asdas2das/asdasf34234/ => /../../../
/ABCDdasda234sEA/asdas2das/asdasf34234/124551assdfa/ => /../../../../
I tried various regex but couldn't get to match correctly, because last "/" is considered ending of the string instead of matching multiple times:
[root@test]# sed "s/^/.*/$//..//g" <<< "/ABCDdasda234sEA/asdasdas/asdasf34234/"
/../
awk sed regular-expression
add a comment |Â
up vote
0
down vote
favorite
I am trying to replace all folders in a path with ".."
This is part of a bigger script that will eventually replace some absolute paths into relative paths.
The expected output should look like this:
/ABCDdasda234sEA/asdas2das/asdasf34234/ => /../../../
/ABCDdasda234sEA/asdas2das/asdasf34234/124551assdfa/ => /../../../../
I tried various regex but couldn't get to match correctly, because last "/" is considered ending of the string instead of matching multiple times:
[root@test]# sed "s/^/.*/$//..//g" <<< "/ABCDdasda234sEA/asdasdas/asdasf34234/"
/../
awk sed regular-expression
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to replace all folders in a path with ".."
This is part of a bigger script that will eventually replace some absolute paths into relative paths.
The expected output should look like this:
/ABCDdasda234sEA/asdas2das/asdasf34234/ => /../../../
/ABCDdasda234sEA/asdas2das/asdasf34234/124551assdfa/ => /../../../../
I tried various regex but couldn't get to match correctly, because last "/" is considered ending of the string instead of matching multiple times:
[root@test]# sed "s/^/.*/$//..//g" <<< "/ABCDdasda234sEA/asdasdas/asdasf34234/"
/../
awk sed regular-expression
I am trying to replace all folders in a path with ".."
This is part of a bigger script that will eventually replace some absolute paths into relative paths.
The expected output should look like this:
/ABCDdasda234sEA/asdas2das/asdasf34234/ => /../../../
/ABCDdasda234sEA/asdas2das/asdasf34234/124551assdfa/ => /../../../../
I tried various regex but couldn't get to match correctly, because last "/" is considered ending of the string instead of matching multiple times:
[root@test]# sed "s/^/.*/$//..//g" <<< "/ABCDdasda234sEA/asdasdas/asdasf34234/"
/../
awk sed regular-expression
awk sed regular-expression
edited Sep 26 '17 at 17:19
Jeff Schaller
32.3k849110
32.3k849110
asked Sep 26 '17 at 14:11
Futur'Fusionneur
518
518
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
2
down vote
accepted
1) The .*
will eat everything including the following slashes, so use [^/]*
instead (any number of characters except for the slash)
2) To avoid the +
(for "one or more occurences") which is not available in all sed
version simply add a dot requiring at least one character. You don't need another [^/]
here if you trust that there is no //
3) Just for beautification: If the pattern or replacement contain a slash, it's easier to read if you use a different delimiter for the s
command like _
or #
All together:
sed 's_/.[^/]*_/.._g'
Thank you. giphy.com/gifs/the-office-thank-you-michael-scott-1Z02vuppxP1Pa
â Futur'Fusionneur
Sep 26 '17 at 15:27
add a comment |Â
up vote
2
down vote
awk -F/ -v OFS=/ 'for (i=1; i<=NF; i++) if (length($i)) $i = ".."1' <<END
/ABCDdasda234sEA/asdas2das/asdasf34234/
/ABCDdasda234sEA/asdas2das/asdasf34234/124551assdfa/
END
/../../../
/../../../../
Thank you this works. I also posted 2 more methods i found meanwhile.
â Futur'Fusionneur
Sep 26 '17 at 14:32
add a comment |Â
up vote
2
down vote
I found two more methods meanwhile:
sed 's:/[[:alnum:]]*:../:g' <<< "/ABCDdasda234sEA/asdasdas/asdasf34234/42346346346/" | sed 's:^..::g'
awk -F'/' 'for( i = 2; i <= NF-1; i++ ) printf "/../" ' <<< "/ABCDdasda234sEA/asdasdas/asdasf34234/asfasddfg/2345555/" | sed 's//////g'
Also to include other special characters such as "_":
sed 's:/[[:alnum:]_]*:../:g' <<< "/ABCDdasda234sEA/asdasdas/asdasf34234/42346346$346/" | sed 's:^..::g'
1
Simply include everything but the slash:sed 's_/.[^/]*_/.._g'
â Philippos
Sep 26 '17 at 15:07
nice, this is elegant. you should post it as an answer instead.
â Futur'Fusionneur
Sep 26 '17 at 15:14
add a comment |Â
up vote
0
down vote
Some versions of sed do not accept the advanced features of regular expressions (e.g. + or ) by default. On my system, + works ok.
Also, you may replace w here with [^/], since file names may contain blanks.
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
1) The .*
will eat everything including the following slashes, so use [^/]*
instead (any number of characters except for the slash)
2) To avoid the +
(for "one or more occurences") which is not available in all sed
version simply add a dot requiring at least one character. You don't need another [^/]
here if you trust that there is no //
3) Just for beautification: If the pattern or replacement contain a slash, it's easier to read if you use a different delimiter for the s
command like _
or #
All together:
sed 's_/.[^/]*_/.._g'
Thank you. giphy.com/gifs/the-office-thank-you-michael-scott-1Z02vuppxP1Pa
â Futur'Fusionneur
Sep 26 '17 at 15:27
add a comment |Â
up vote
2
down vote
accepted
1) The .*
will eat everything including the following slashes, so use [^/]*
instead (any number of characters except for the slash)
2) To avoid the +
(for "one or more occurences") which is not available in all sed
version simply add a dot requiring at least one character. You don't need another [^/]
here if you trust that there is no //
3) Just for beautification: If the pattern or replacement contain a slash, it's easier to read if you use a different delimiter for the s
command like _
or #
All together:
sed 's_/.[^/]*_/.._g'
Thank you. giphy.com/gifs/the-office-thank-you-michael-scott-1Z02vuppxP1Pa
â Futur'Fusionneur
Sep 26 '17 at 15:27
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
1) The .*
will eat everything including the following slashes, so use [^/]*
instead (any number of characters except for the slash)
2) To avoid the +
(for "one or more occurences") which is not available in all sed
version simply add a dot requiring at least one character. You don't need another [^/]
here if you trust that there is no //
3) Just for beautification: If the pattern or replacement contain a slash, it's easier to read if you use a different delimiter for the s
command like _
or #
All together:
sed 's_/.[^/]*_/.._g'
1) The .*
will eat everything including the following slashes, so use [^/]*
instead (any number of characters except for the slash)
2) To avoid the +
(for "one or more occurences") which is not available in all sed
version simply add a dot requiring at least one character. You don't need another [^/]
here if you trust that there is no //
3) Just for beautification: If the pattern or replacement contain a slash, it's easier to read if you use a different delimiter for the s
command like _
or #
All together:
sed 's_/.[^/]*_/.._g'
answered Sep 26 '17 at 15:21
Philippos
5,95211546
5,95211546
Thank you. giphy.com/gifs/the-office-thank-you-michael-scott-1Z02vuppxP1Pa
â Futur'Fusionneur
Sep 26 '17 at 15:27
add a comment |Â
Thank you. giphy.com/gifs/the-office-thank-you-michael-scott-1Z02vuppxP1Pa
â Futur'Fusionneur
Sep 26 '17 at 15:27
Thank you. giphy.com/gifs/the-office-thank-you-michael-scott-1Z02vuppxP1Pa
â Futur'Fusionneur
Sep 26 '17 at 15:27
Thank you. giphy.com/gifs/the-office-thank-you-michael-scott-1Z02vuppxP1Pa
â Futur'Fusionneur
Sep 26 '17 at 15:27
add a comment |Â
up vote
2
down vote
awk -F/ -v OFS=/ 'for (i=1; i<=NF; i++) if (length($i)) $i = ".."1' <<END
/ABCDdasda234sEA/asdas2das/asdasf34234/
/ABCDdasda234sEA/asdas2das/asdasf34234/124551assdfa/
END
/../../../
/../../../../
Thank you this works. I also posted 2 more methods i found meanwhile.
â Futur'Fusionneur
Sep 26 '17 at 14:32
add a comment |Â
up vote
2
down vote
awk -F/ -v OFS=/ 'for (i=1; i<=NF; i++) if (length($i)) $i = ".."1' <<END
/ABCDdasda234sEA/asdas2das/asdasf34234/
/ABCDdasda234sEA/asdas2das/asdasf34234/124551assdfa/
END
/../../../
/../../../../
Thank you this works. I also posted 2 more methods i found meanwhile.
â Futur'Fusionneur
Sep 26 '17 at 14:32
add a comment |Â
up vote
2
down vote
up vote
2
down vote
awk -F/ -v OFS=/ 'for (i=1; i<=NF; i++) if (length($i)) $i = ".."1' <<END
/ABCDdasda234sEA/asdas2das/asdasf34234/
/ABCDdasda234sEA/asdas2das/asdasf34234/124551assdfa/
END
/../../../
/../../../../
awk -F/ -v OFS=/ 'for (i=1; i<=NF; i++) if (length($i)) $i = ".."1' <<END
/ABCDdasda234sEA/asdas2das/asdasf34234/
/ABCDdasda234sEA/asdas2das/asdasf34234/124551assdfa/
END
/../../../
/../../../../
answered Sep 26 '17 at 14:18
glenn jackman
47.4k265103
47.4k265103
Thank you this works. I also posted 2 more methods i found meanwhile.
â Futur'Fusionneur
Sep 26 '17 at 14:32
add a comment |Â
Thank you this works. I also posted 2 more methods i found meanwhile.
â Futur'Fusionneur
Sep 26 '17 at 14:32
Thank you this works. I also posted 2 more methods i found meanwhile.
â Futur'Fusionneur
Sep 26 '17 at 14:32
Thank you this works. I also posted 2 more methods i found meanwhile.
â Futur'Fusionneur
Sep 26 '17 at 14:32
add a comment |Â
up vote
2
down vote
I found two more methods meanwhile:
sed 's:/[[:alnum:]]*:../:g' <<< "/ABCDdasda234sEA/asdasdas/asdasf34234/42346346346/" | sed 's:^..::g'
awk -F'/' 'for( i = 2; i <= NF-1; i++ ) printf "/../" ' <<< "/ABCDdasda234sEA/asdasdas/asdasf34234/asfasddfg/2345555/" | sed 's//////g'
Also to include other special characters such as "_":
sed 's:/[[:alnum:]_]*:../:g' <<< "/ABCDdasda234sEA/asdasdas/asdasf34234/42346346$346/" | sed 's:^..::g'
1
Simply include everything but the slash:sed 's_/.[^/]*_/.._g'
â Philippos
Sep 26 '17 at 15:07
nice, this is elegant. you should post it as an answer instead.
â Futur'Fusionneur
Sep 26 '17 at 15:14
add a comment |Â
up vote
2
down vote
I found two more methods meanwhile:
sed 's:/[[:alnum:]]*:../:g' <<< "/ABCDdasda234sEA/asdasdas/asdasf34234/42346346346/" | sed 's:^..::g'
awk -F'/' 'for( i = 2; i <= NF-1; i++ ) printf "/../" ' <<< "/ABCDdasda234sEA/asdasdas/asdasf34234/asfasddfg/2345555/" | sed 's//////g'
Also to include other special characters such as "_":
sed 's:/[[:alnum:]_]*:../:g' <<< "/ABCDdasda234sEA/asdasdas/asdasf34234/42346346$346/" | sed 's:^..::g'
1
Simply include everything but the slash:sed 's_/.[^/]*_/.._g'
â Philippos
Sep 26 '17 at 15:07
nice, this is elegant. you should post it as an answer instead.
â Futur'Fusionneur
Sep 26 '17 at 15:14
add a comment |Â
up vote
2
down vote
up vote
2
down vote
I found two more methods meanwhile:
sed 's:/[[:alnum:]]*:../:g' <<< "/ABCDdasda234sEA/asdasdas/asdasf34234/42346346346/" | sed 's:^..::g'
awk -F'/' 'for( i = 2; i <= NF-1; i++ ) printf "/../" ' <<< "/ABCDdasda234sEA/asdasdas/asdasf34234/asfasddfg/2345555/" | sed 's//////g'
Also to include other special characters such as "_":
sed 's:/[[:alnum:]_]*:../:g' <<< "/ABCDdasda234sEA/asdasdas/asdasf34234/42346346$346/" | sed 's:^..::g'
I found two more methods meanwhile:
sed 's:/[[:alnum:]]*:../:g' <<< "/ABCDdasda234sEA/asdasdas/asdasf34234/42346346346/" | sed 's:^..::g'
awk -F'/' 'for( i = 2; i <= NF-1; i++ ) printf "/../" ' <<< "/ABCDdasda234sEA/asdasdas/asdasf34234/asfasddfg/2345555/" | sed 's//////g'
Also to include other special characters such as "_":
sed 's:/[[:alnum:]_]*:../:g' <<< "/ABCDdasda234sEA/asdasdas/asdasf34234/42346346$346/" | sed 's:^..::g'
edited Sep 26 '17 at 14:44
answered Sep 26 '17 at 14:31
Futur'Fusionneur
518
518
1
Simply include everything but the slash:sed 's_/.[^/]*_/.._g'
â Philippos
Sep 26 '17 at 15:07
nice, this is elegant. you should post it as an answer instead.
â Futur'Fusionneur
Sep 26 '17 at 15:14
add a comment |Â
1
Simply include everything but the slash:sed 's_/.[^/]*_/.._g'
â Philippos
Sep 26 '17 at 15:07
nice, this is elegant. you should post it as an answer instead.
â Futur'Fusionneur
Sep 26 '17 at 15:14
1
1
Simply include everything but the slash:
sed 's_/.[^/]*_/.._g'
â Philippos
Sep 26 '17 at 15:07
Simply include everything but the slash:
sed 's_/.[^/]*_/.._g'
â Philippos
Sep 26 '17 at 15:07
nice, this is elegant. you should post it as an answer instead.
â Futur'Fusionneur
Sep 26 '17 at 15:14
nice, this is elegant. you should post it as an answer instead.
â Futur'Fusionneur
Sep 26 '17 at 15:14
add a comment |Â
up vote
0
down vote
Some versions of sed do not accept the advanced features of regular expressions (e.g. + or ) by default. On my system, + works ok.
Also, you may replace w here with [^/], since file names may contain blanks.
add a comment |Â
up vote
0
down vote
Some versions of sed do not accept the advanced features of regular expressions (e.g. + or ) by default. On my system, + works ok.
Also, you may replace w here with [^/], since file names may contain blanks.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Some versions of sed do not accept the advanced features of regular expressions (e.g. + or ) by default. On my system, + works ok.
Also, you may replace w here with [^/], since file names may contain blanks.
Some versions of sed do not accept the advanced features of regular expressions (e.g. + or ) by default. On my system, + works ok.
Also, you may replace w here with [^/], since file names may contain blanks.
answered Sep 26 '17 at 14:39
Amos Shapir
11
11
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f394549%2fsed-or-awk-to-replace-string-between-pattern-convert-absolute-path-to-relative%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password