sed - * works, + doesnt?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I'm renaming a bunch of music files, stripping of the track numbers using sed. However I'm puzzled by sed's behavior towards the '+' character for regex expressions.
What I wanted to do:
for f in *-[0-9]**; do echo "$f" | sed 's/-[0-9]+/-/g'; done
But that didn't work as intended. This workaround did the job:
for f in *-[0-9]**; do echo "$f" | sed 's/-[0-9][0-9]*/-/g'; done
However I would like to know what the issue is with using '+' in sed... Any ideas?
sed
add a comment |Â
up vote
0
down vote
favorite
I'm renaming a bunch of music files, stripping of the track numbers using sed. However I'm puzzled by sed's behavior towards the '+' character for regex expressions.
What I wanted to do:
for f in *-[0-9]**; do echo "$f" | sed 's/-[0-9]+/-/g'; done
But that didn't work as intended. This workaround did the job:
for f in *-[0-9]**; do echo "$f" | sed 's/-[0-9][0-9]*/-/g'; done
However I would like to know what the issue is with using '+' in sed... Any ideas?
sed
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm renaming a bunch of music files, stripping of the track numbers using sed. However I'm puzzled by sed's behavior towards the '+' character for regex expressions.
What I wanted to do:
for f in *-[0-9]**; do echo "$f" | sed 's/-[0-9]+/-/g'; done
But that didn't work as intended. This workaround did the job:
for f in *-[0-9]**; do echo "$f" | sed 's/-[0-9][0-9]*/-/g'; done
However I would like to know what the issue is with using '+' in sed... Any ideas?
sed
I'm renaming a bunch of music files, stripping of the track numbers using sed. However I'm puzzled by sed's behavior towards the '+' character for regex expressions.
What I wanted to do:
for f in *-[0-9]**; do echo "$f" | sed 's/-[0-9]+/-/g'; done
But that didn't work as intended. This workaround did the job:
for f in *-[0-9]**; do echo "$f" | sed 's/-[0-9][0-9]*/-/g'; done
However I would like to know what the issue is with using '+' in sed... Any ideas?
sed
asked Jun 27 at 11:45
koen
1
1
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
+
is an extended regular expression modifier. sed
does basic regular expressions by default.
To make sed
parse your expressions as extended regular expressions, use sed -E
. GNU sed
also understands +
in basic regular expressions if you use it as +
.
Also, your pattern *-[0-9]**
looks strange. The -
does not need escaping (not in the shell pattern and not in the regular expression), and unless you use globstar
in bash
, the **
pattern will expand to itself. With globstar
enabled in bash
*-[0-9]**
would expand to all pathnames that contain -N
in the first filename component (where N
is a digit).
What I think you'd like to do is something like this (probably not quite, but I don't know what your filenames look like):
for name in *-[0-9][0-9]*; do
newname=$name%-*
printf 'Would rename "%s" into "%s"n' "$name" "$newname"
# mv "$name" "$newname"
done
This matches all names in the current directory that contains -NN
(N
is a digit). It strips off everything from the last -
in the filename and renames the file (the actual renaming is commented out).
1
Ah yes, escaping is a GNU-ism...
â Stephen Kitt
Jun 27 at 11:53
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
+
is an extended regular expression modifier. sed
does basic regular expressions by default.
To make sed
parse your expressions as extended regular expressions, use sed -E
. GNU sed
also understands +
in basic regular expressions if you use it as +
.
Also, your pattern *-[0-9]**
looks strange. The -
does not need escaping (not in the shell pattern and not in the regular expression), and unless you use globstar
in bash
, the **
pattern will expand to itself. With globstar
enabled in bash
*-[0-9]**
would expand to all pathnames that contain -N
in the first filename component (where N
is a digit).
What I think you'd like to do is something like this (probably not quite, but I don't know what your filenames look like):
for name in *-[0-9][0-9]*; do
newname=$name%-*
printf 'Would rename "%s" into "%s"n' "$name" "$newname"
# mv "$name" "$newname"
done
This matches all names in the current directory that contains -NN
(N
is a digit). It strips off everything from the last -
in the filename and renames the file (the actual renaming is commented out).
1
Ah yes, escaping is a GNU-ism...
â Stephen Kitt
Jun 27 at 11:53
add a comment |Â
up vote
3
down vote
+
is an extended regular expression modifier. sed
does basic regular expressions by default.
To make sed
parse your expressions as extended regular expressions, use sed -E
. GNU sed
also understands +
in basic regular expressions if you use it as +
.
Also, your pattern *-[0-9]**
looks strange. The -
does not need escaping (not in the shell pattern and not in the regular expression), and unless you use globstar
in bash
, the **
pattern will expand to itself. With globstar
enabled in bash
*-[0-9]**
would expand to all pathnames that contain -N
in the first filename component (where N
is a digit).
What I think you'd like to do is something like this (probably not quite, but I don't know what your filenames look like):
for name in *-[0-9][0-9]*; do
newname=$name%-*
printf 'Would rename "%s" into "%s"n' "$name" "$newname"
# mv "$name" "$newname"
done
This matches all names in the current directory that contains -NN
(N
is a digit). It strips off everything from the last -
in the filename and renames the file (the actual renaming is commented out).
1
Ah yes, escaping is a GNU-ism...
â Stephen Kitt
Jun 27 at 11:53
add a comment |Â
up vote
3
down vote
up vote
3
down vote
+
is an extended regular expression modifier. sed
does basic regular expressions by default.
To make sed
parse your expressions as extended regular expressions, use sed -E
. GNU sed
also understands +
in basic regular expressions if you use it as +
.
Also, your pattern *-[0-9]**
looks strange. The -
does not need escaping (not in the shell pattern and not in the regular expression), and unless you use globstar
in bash
, the **
pattern will expand to itself. With globstar
enabled in bash
*-[0-9]**
would expand to all pathnames that contain -N
in the first filename component (where N
is a digit).
What I think you'd like to do is something like this (probably not quite, but I don't know what your filenames look like):
for name in *-[0-9][0-9]*; do
newname=$name%-*
printf 'Would rename "%s" into "%s"n' "$name" "$newname"
# mv "$name" "$newname"
done
This matches all names in the current directory that contains -NN
(N
is a digit). It strips off everything from the last -
in the filename and renames the file (the actual renaming is commented out).
+
is an extended regular expression modifier. sed
does basic regular expressions by default.
To make sed
parse your expressions as extended regular expressions, use sed -E
. GNU sed
also understands +
in basic regular expressions if you use it as +
.
Also, your pattern *-[0-9]**
looks strange. The -
does not need escaping (not in the shell pattern and not in the regular expression), and unless you use globstar
in bash
, the **
pattern will expand to itself. With globstar
enabled in bash
*-[0-9]**
would expand to all pathnames that contain -N
in the first filename component (where N
is a digit).
What I think you'd like to do is something like this (probably not quite, but I don't know what your filenames look like):
for name in *-[0-9][0-9]*; do
newname=$name%-*
printf 'Would rename "%s" into "%s"n' "$name" "$newname"
# mv "$name" "$newname"
done
This matches all names in the current directory that contains -NN
(N
is a digit). It strips off everything from the last -
in the filename and renames the file (the actual renaming is commented out).
edited Jun 27 at 12:00
answered Jun 27 at 11:48
Kusalananda
101k13199312
101k13199312
1
Ah yes, escaping is a GNU-ism...
â Stephen Kitt
Jun 27 at 11:53
add a comment |Â
1
Ah yes, escaping is a GNU-ism...
â Stephen Kitt
Jun 27 at 11:53
1
1
Ah yes, escaping is a GNU-ism...
â Stephen Kitt
Jun 27 at 11:53
Ah yes, escaping is a GNU-ism...
â Stephen Kitt
Jun 27 at 11:53
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%2f452202%2fsed-works-doesnt%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