How to get the string after the first numerical?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a string structure like this bob-type-8.2-mp2-2017-93-43-11-65-48.spr
form where I just need to extract 8.2-mp2-2017-93-43-11-65-48
.
It means the output string should contain all the characters after encountering the first numerical value and then removing the .spr
from that.
How can I do that?
shell regular-expression
add a comment |Â
up vote
0
down vote
favorite
I have a string structure like this bob-type-8.2-mp2-2017-93-43-11-65-48.spr
form where I just need to extract 8.2-mp2-2017-93-43-11-65-48
.
It means the output string should contain all the characters after encountering the first numerical value and then removing the .spr
from that.
How can I do that?
shell regular-expression
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a string structure like this bob-type-8.2-mp2-2017-93-43-11-65-48.spr
form where I just need to extract 8.2-mp2-2017-93-43-11-65-48
.
It means the output string should contain all the characters after encountering the first numerical value and then removing the .spr
from that.
How can I do that?
shell regular-expression
I have a string structure like this bob-type-8.2-mp2-2017-93-43-11-65-48.spr
form where I just need to extract 8.2-mp2-2017-93-43-11-65-48
.
It means the output string should contain all the characters after encountering the first numerical value and then removing the .spr
from that.
How can I do that?
shell regular-expression
shell regular-expression
edited Oct 3 '17 at 13:02
Archemar
19.1k93366
19.1k93366
asked Oct 3 '17 at 12:58
subrat prusty
31
31
add a comment |Â
add a comment |Â
7 Answers
7
active
oldest
votes
up vote
1
down vote
accepted
Try this:
echo "bob-type-8.2-mp2-2017-93-43-11-65-48.spr" | sed 's/^[^0-9]*//;s/.[^.]*$//'
Output will:
8.2-mp2-2017-93-43-11-65-48
Explanation:
Sed uses pattern 's/pattern/replace_pattern/'
to find pattern
and replace it to replace_pattern
So, pattern 's/^[^0-9]*//'
get all symbols from start of line and before first digits occurance and replace it to nothing (replace_pattern
are empty).
The next step - delete extention. We can do this with the same sed's pattern 's///'
.
s/.[^.]*$//
- find all symbols that not a .
at the end $
of line and replace it to nothing.
;
- devide patterns.
For best understanding you could use this command instead:
echo "bob-type-8.2-mp2-2017-93-43-11-65-48.spr" | sed -e 's/^[^0-9]*//' -e 's/.[^.]*$//'
Note that it assumes the file name doesn't contain newline characters (or bytes not forming valid characters, or backslash characters depending on the echo implementation)
â Stéphane Chazelas
Oct 3 '17 at 16:33
add a comment |Â
up vote
1
down vote
In Bash, with extglob
:
$ shopt -s extglob
$ var=bob-type-8.2-mp2-2017-93-43-11-65-48.spr
$ res=$var##*([^0-9])
$ÃÂ res=$res%.spr
$ echo "$res"
8.2-mp2-2017-93-43-11-65-48
*([^0-9])
matches any string of non-digits, $var##pattern
removes the longest matching pattern from the beginning of the string, $var%pattern
removes the (shortest) matching pattern from the end.
add a comment |Â
up vote
1
down vote
another approach with sed
.
sed 's/^[^[[:digit:]]*(.*).spr$/1/' <<<"bob-type-8.2-mp2-2017-93-43-11-65-48.spr"
^[^[[:digit:]]*
match everything start from beginning of the string until first digit seen; Same as^[^0-9]*
(.*)
matches everything after above matches and parentheses(...)
are used to capture a group match with1
as its back-reference..spr$
matches a literal point followed byspr
at the end of input string.1
prints only captured group match.
add a comment |Â
up vote
1
down vote
So many sed
answers! How about a pure bash solution using =~
pattern matching and the not-so-common back-reference array BASH_REMATCH
?
# put your string in variable named 'input'
patt='^[^0-9]*(.*)..*' && [[ $input =~ $patt ]] && echo "$BASH_REMATCH[1]"
Just to stand apart from the crowd. ;)
add a comment |Â
up vote
1
down vote
POSIXly:
string=bob-type-8.2-mp2-2017-93-43-11-65-48.spr
newstring=$string#"$string%%[0-9]*"
newstring=$newstring%.*
$string#pattern
: remove from the start of$string
what is matched bypattern
which in this case is:$string%%[0-9]*
$string
stripped of its longest trailing part that matches the[0-9]*
pattern. So that's the part up to the first digit.$newstring%.*
:$string
stripped of its shortest trailing part that matches the.*
pattern. So removes the extension.
With zsh
:
newstring=$(M)$string:r%%[0-9]*
$string:r
: expands to the root name (removed the extension) like incsh
$(M)var%%pattern
, return what isM
atched by the%%
operator (which without(M)
would remove the longest part at the end that would match the pattern like in POSIX shells).
add a comment |Â
up vote
1
down vote
How about this?
echo bob-type-8.2-mp2-2017-93-43-11-65-48.spr | sed -r 's/^[^0-9]*(.*)..*$/1/'
This takes everything from the first numerical value encountered after the beginning of the string to the end of the string minus the last dot and what follows it.
^[^0-9]*
skips all non-numerical: NOT zero to nine ([^0-9]
), 0 to N times (*
), from the beginning of the line (^
)...*$
matches any character (.
), 0 to N times (*
), before the end of the line ($
), and preceeded by a dot (.
). In our case this is.spr
but it could apply to any other "dot + N-characters" file extension:.s
,.yaythatsacoolextension
, etc, work as well.(.*)
, referred as1
later, keeps (this is the purpose of the parenthesis, we couldn't refer to it later otherwise) what's in the middle: any character (.
), 0 to N times (*
). This gives us8.2-mp2-2017-93-43-11-65-48
.
So if you need to list into a file the normalized names of all .spr
contained in the current directory, for example, you can do something like this:
for i in *.spr
do
echo $i | sed -r 's/^[^0-9]*(.*)..*$/1/' >> mylist.lst
done
And tomorrow, should you need to do that with .blop
files instead, just turn the *.spr
above into *.blop
.
add a comment |Â
up vote
0
down vote
Sed is one of the possible approaches
echo bob-type-8.2-mp2-2017-93-43-11-65-48.spr | sed 's/^[^0-9]*//;s/....$//'
8.2-mp2-2017-93-43-11-65-48
regexps mean
- replace all non-numerical symbols from beginning with nothing till
the first numerical - replace last 4 chars with nothinig
yep, overseen that, updated now
â Tagwint
Oct 3 '17 at 13:15
add a comment |Â
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Try this:
echo "bob-type-8.2-mp2-2017-93-43-11-65-48.spr" | sed 's/^[^0-9]*//;s/.[^.]*$//'
Output will:
8.2-mp2-2017-93-43-11-65-48
Explanation:
Sed uses pattern 's/pattern/replace_pattern/'
to find pattern
and replace it to replace_pattern
So, pattern 's/^[^0-9]*//'
get all symbols from start of line and before first digits occurance and replace it to nothing (replace_pattern
are empty).
The next step - delete extention. We can do this with the same sed's pattern 's///'
.
s/.[^.]*$//
- find all symbols that not a .
at the end $
of line and replace it to nothing.
;
- devide patterns.
For best understanding you could use this command instead:
echo "bob-type-8.2-mp2-2017-93-43-11-65-48.spr" | sed -e 's/^[^0-9]*//' -e 's/.[^.]*$//'
Note that it assumes the file name doesn't contain newline characters (or bytes not forming valid characters, or backslash characters depending on the echo implementation)
â Stéphane Chazelas
Oct 3 '17 at 16:33
add a comment |Â
up vote
1
down vote
accepted
Try this:
echo "bob-type-8.2-mp2-2017-93-43-11-65-48.spr" | sed 's/^[^0-9]*//;s/.[^.]*$//'
Output will:
8.2-mp2-2017-93-43-11-65-48
Explanation:
Sed uses pattern 's/pattern/replace_pattern/'
to find pattern
and replace it to replace_pattern
So, pattern 's/^[^0-9]*//'
get all symbols from start of line and before first digits occurance and replace it to nothing (replace_pattern
are empty).
The next step - delete extention. We can do this with the same sed's pattern 's///'
.
s/.[^.]*$//
- find all symbols that not a .
at the end $
of line and replace it to nothing.
;
- devide patterns.
For best understanding you could use this command instead:
echo "bob-type-8.2-mp2-2017-93-43-11-65-48.spr" | sed -e 's/^[^0-9]*//' -e 's/.[^.]*$//'
Note that it assumes the file name doesn't contain newline characters (or bytes not forming valid characters, or backslash characters depending on the echo implementation)
â Stéphane Chazelas
Oct 3 '17 at 16:33
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Try this:
echo "bob-type-8.2-mp2-2017-93-43-11-65-48.spr" | sed 's/^[^0-9]*//;s/.[^.]*$//'
Output will:
8.2-mp2-2017-93-43-11-65-48
Explanation:
Sed uses pattern 's/pattern/replace_pattern/'
to find pattern
and replace it to replace_pattern
So, pattern 's/^[^0-9]*//'
get all symbols from start of line and before first digits occurance and replace it to nothing (replace_pattern
are empty).
The next step - delete extention. We can do this with the same sed's pattern 's///'
.
s/.[^.]*$//
- find all symbols that not a .
at the end $
of line and replace it to nothing.
;
- devide patterns.
For best understanding you could use this command instead:
echo "bob-type-8.2-mp2-2017-93-43-11-65-48.spr" | sed -e 's/^[^0-9]*//' -e 's/.[^.]*$//'
Try this:
echo "bob-type-8.2-mp2-2017-93-43-11-65-48.spr" | sed 's/^[^0-9]*//;s/.[^.]*$//'
Output will:
8.2-mp2-2017-93-43-11-65-48
Explanation:
Sed uses pattern 's/pattern/replace_pattern/'
to find pattern
and replace it to replace_pattern
So, pattern 's/^[^0-9]*//'
get all symbols from start of line and before first digits occurance and replace it to nothing (replace_pattern
are empty).
The next step - delete extention. We can do this with the same sed's pattern 's///'
.
s/.[^.]*$//
- find all symbols that not a .
at the end $
of line and replace it to nothing.
;
- devide patterns.
For best understanding you could use this command instead:
echo "bob-type-8.2-mp2-2017-93-43-11-65-48.spr" | sed -e 's/^[^0-9]*//' -e 's/.[^.]*$//'
edited Oct 3 '17 at 13:17
answered Oct 3 '17 at 13:08
Egor Vasilyev
1,792129
1,792129
Note that it assumes the file name doesn't contain newline characters (or bytes not forming valid characters, or backslash characters depending on the echo implementation)
â Stéphane Chazelas
Oct 3 '17 at 16:33
add a comment |Â
Note that it assumes the file name doesn't contain newline characters (or bytes not forming valid characters, or backslash characters depending on the echo implementation)
â Stéphane Chazelas
Oct 3 '17 at 16:33
Note that it assumes the file name doesn't contain newline characters (or bytes not forming valid characters, or backslash characters depending on the echo implementation)
â Stéphane Chazelas
Oct 3 '17 at 16:33
Note that it assumes the file name doesn't contain newline characters (or bytes not forming valid characters, or backslash characters depending on the echo implementation)
â Stéphane Chazelas
Oct 3 '17 at 16:33
add a comment |Â
up vote
1
down vote
In Bash, with extglob
:
$ shopt -s extglob
$ var=bob-type-8.2-mp2-2017-93-43-11-65-48.spr
$ res=$var##*([^0-9])
$ÃÂ res=$res%.spr
$ echo "$res"
8.2-mp2-2017-93-43-11-65-48
*([^0-9])
matches any string of non-digits, $var##pattern
removes the longest matching pattern from the beginning of the string, $var%pattern
removes the (shortest) matching pattern from the end.
add a comment |Â
up vote
1
down vote
In Bash, with extglob
:
$ shopt -s extglob
$ var=bob-type-8.2-mp2-2017-93-43-11-65-48.spr
$ res=$var##*([^0-9])
$ÃÂ res=$res%.spr
$ echo "$res"
8.2-mp2-2017-93-43-11-65-48
*([^0-9])
matches any string of non-digits, $var##pattern
removes the longest matching pattern from the beginning of the string, $var%pattern
removes the (shortest) matching pattern from the end.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
In Bash, with extglob
:
$ shopt -s extglob
$ var=bob-type-8.2-mp2-2017-93-43-11-65-48.spr
$ res=$var##*([^0-9])
$ÃÂ res=$res%.spr
$ echo "$res"
8.2-mp2-2017-93-43-11-65-48
*([^0-9])
matches any string of non-digits, $var##pattern
removes the longest matching pattern from the beginning of the string, $var%pattern
removes the (shortest) matching pattern from the end.
In Bash, with extglob
:
$ shopt -s extglob
$ var=bob-type-8.2-mp2-2017-93-43-11-65-48.spr
$ res=$var##*([^0-9])
$ÃÂ res=$res%.spr
$ echo "$res"
8.2-mp2-2017-93-43-11-65-48
*([^0-9])
matches any string of non-digits, $var##pattern
removes the longest matching pattern from the beginning of the string, $var%pattern
removes the (shortest) matching pattern from the end.
answered Oct 3 '17 at 13:25
ilkkachu
50.9k678140
50.9k678140
add a comment |Â
add a comment |Â
up vote
1
down vote
another approach with sed
.
sed 's/^[^[[:digit:]]*(.*).spr$/1/' <<<"bob-type-8.2-mp2-2017-93-43-11-65-48.spr"
^[^[[:digit:]]*
match everything start from beginning of the string until first digit seen; Same as^[^0-9]*
(.*)
matches everything after above matches and parentheses(...)
are used to capture a group match with1
as its back-reference..spr$
matches a literal point followed byspr
at the end of input string.1
prints only captured group match.
add a comment |Â
up vote
1
down vote
another approach with sed
.
sed 's/^[^[[:digit:]]*(.*).spr$/1/' <<<"bob-type-8.2-mp2-2017-93-43-11-65-48.spr"
^[^[[:digit:]]*
match everything start from beginning of the string until first digit seen; Same as^[^0-9]*
(.*)
matches everything after above matches and parentheses(...)
are used to capture a group match with1
as its back-reference..spr$
matches a literal point followed byspr
at the end of input string.1
prints only captured group match.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
another approach with sed
.
sed 's/^[^[[:digit:]]*(.*).spr$/1/' <<<"bob-type-8.2-mp2-2017-93-43-11-65-48.spr"
^[^[[:digit:]]*
match everything start from beginning of the string until first digit seen; Same as^[^0-9]*
(.*)
matches everything after above matches and parentheses(...)
are used to capture a group match with1
as its back-reference..spr$
matches a literal point followed byspr
at the end of input string.1
prints only captured group match.
another approach with sed
.
sed 's/^[^[[:digit:]]*(.*).spr$/1/' <<<"bob-type-8.2-mp2-2017-93-43-11-65-48.spr"
^[^[[:digit:]]*
match everything start from beginning of the string until first digit seen; Same as^[^0-9]*
(.*)
matches everything after above matches and parentheses(...)
are used to capture a group match with1
as its back-reference..spr$
matches a literal point followed byspr
at the end of input string.1
prints only captured group match.
edited Oct 3 '17 at 13:26
answered Oct 3 '17 at 13:21
ñÃÂsýù÷
15.7k92563
15.7k92563
add a comment |Â
add a comment |Â
up vote
1
down vote
So many sed
answers! How about a pure bash solution using =~
pattern matching and the not-so-common back-reference array BASH_REMATCH
?
# put your string in variable named 'input'
patt='^[^0-9]*(.*)..*' && [[ $input =~ $patt ]] && echo "$BASH_REMATCH[1]"
Just to stand apart from the crowd. ;)
add a comment |Â
up vote
1
down vote
So many sed
answers! How about a pure bash solution using =~
pattern matching and the not-so-common back-reference array BASH_REMATCH
?
# put your string in variable named 'input'
patt='^[^0-9]*(.*)..*' && [[ $input =~ $patt ]] && echo "$BASH_REMATCH[1]"
Just to stand apart from the crowd. ;)
add a comment |Â
up vote
1
down vote
up vote
1
down vote
So many sed
answers! How about a pure bash solution using =~
pattern matching and the not-so-common back-reference array BASH_REMATCH
?
# put your string in variable named 'input'
patt='^[^0-9]*(.*)..*' && [[ $input =~ $patt ]] && echo "$BASH_REMATCH[1]"
Just to stand apart from the crowd. ;)
So many sed
answers! How about a pure bash solution using =~
pattern matching and the not-so-common back-reference array BASH_REMATCH
?
# put your string in variable named 'input'
patt='^[^0-9]*(.*)..*' && [[ $input =~ $patt ]] && echo "$BASH_REMATCH[1]"
Just to stand apart from the crowd. ;)
edited Oct 3 '17 at 13:47
answered Oct 3 '17 at 13:36
B Layer
3,9241525
3,9241525
add a comment |Â
add a comment |Â
up vote
1
down vote
POSIXly:
string=bob-type-8.2-mp2-2017-93-43-11-65-48.spr
newstring=$string#"$string%%[0-9]*"
newstring=$newstring%.*
$string#pattern
: remove from the start of$string
what is matched bypattern
which in this case is:$string%%[0-9]*
$string
stripped of its longest trailing part that matches the[0-9]*
pattern. So that's the part up to the first digit.$newstring%.*
:$string
stripped of its shortest trailing part that matches the.*
pattern. So removes the extension.
With zsh
:
newstring=$(M)$string:r%%[0-9]*
$string:r
: expands to the root name (removed the extension) like incsh
$(M)var%%pattern
, return what isM
atched by the%%
operator (which without(M)
would remove the longest part at the end that would match the pattern like in POSIX shells).
add a comment |Â
up vote
1
down vote
POSIXly:
string=bob-type-8.2-mp2-2017-93-43-11-65-48.spr
newstring=$string#"$string%%[0-9]*"
newstring=$newstring%.*
$string#pattern
: remove from the start of$string
what is matched bypattern
which in this case is:$string%%[0-9]*
$string
stripped of its longest trailing part that matches the[0-9]*
pattern. So that's the part up to the first digit.$newstring%.*
:$string
stripped of its shortest trailing part that matches the.*
pattern. So removes the extension.
With zsh
:
newstring=$(M)$string:r%%[0-9]*
$string:r
: expands to the root name (removed the extension) like incsh
$(M)var%%pattern
, return what isM
atched by the%%
operator (which without(M)
would remove the longest part at the end that would match the pattern like in POSIX shells).
add a comment |Â
up vote
1
down vote
up vote
1
down vote
POSIXly:
string=bob-type-8.2-mp2-2017-93-43-11-65-48.spr
newstring=$string#"$string%%[0-9]*"
newstring=$newstring%.*
$string#pattern
: remove from the start of$string
what is matched bypattern
which in this case is:$string%%[0-9]*
$string
stripped of its longest trailing part that matches the[0-9]*
pattern. So that's the part up to the first digit.$newstring%.*
:$string
stripped of its shortest trailing part that matches the.*
pattern. So removes the extension.
With zsh
:
newstring=$(M)$string:r%%[0-9]*
$string:r
: expands to the root name (removed the extension) like incsh
$(M)var%%pattern
, return what isM
atched by the%%
operator (which without(M)
would remove the longest part at the end that would match the pattern like in POSIX shells).
POSIXly:
string=bob-type-8.2-mp2-2017-93-43-11-65-48.spr
newstring=$string#"$string%%[0-9]*"
newstring=$newstring%.*
$string#pattern
: remove from the start of$string
what is matched bypattern
which in this case is:$string%%[0-9]*
$string
stripped of its longest trailing part that matches the[0-9]*
pattern. So that's the part up to the first digit.$newstring%.*
:$string
stripped of its shortest trailing part that matches the.*
pattern. So removes the extension.
With zsh
:
newstring=$(M)$string:r%%[0-9]*
$string:r
: expands to the root name (removed the extension) like incsh
$(M)var%%pattern
, return what isM
atched by the%%
operator (which without(M)
would remove the longest part at the end that would match the pattern like in POSIX shells).
edited Oct 3 '17 at 13:51
answered Oct 3 '17 at 13:43
Stéphane Chazelas
283k53522859
283k53522859
add a comment |Â
add a comment |Â
up vote
1
down vote
How about this?
echo bob-type-8.2-mp2-2017-93-43-11-65-48.spr | sed -r 's/^[^0-9]*(.*)..*$/1/'
This takes everything from the first numerical value encountered after the beginning of the string to the end of the string minus the last dot and what follows it.
^[^0-9]*
skips all non-numerical: NOT zero to nine ([^0-9]
), 0 to N times (*
), from the beginning of the line (^
)...*$
matches any character (.
), 0 to N times (*
), before the end of the line ($
), and preceeded by a dot (.
). In our case this is.spr
but it could apply to any other "dot + N-characters" file extension:.s
,.yaythatsacoolextension
, etc, work as well.(.*)
, referred as1
later, keeps (this is the purpose of the parenthesis, we couldn't refer to it later otherwise) what's in the middle: any character (.
), 0 to N times (*
). This gives us8.2-mp2-2017-93-43-11-65-48
.
So if you need to list into a file the normalized names of all .spr
contained in the current directory, for example, you can do something like this:
for i in *.spr
do
echo $i | sed -r 's/^[^0-9]*(.*)..*$/1/' >> mylist.lst
done
And tomorrow, should you need to do that with .blop
files instead, just turn the *.spr
above into *.blop
.
add a comment |Â
up vote
1
down vote
How about this?
echo bob-type-8.2-mp2-2017-93-43-11-65-48.spr | sed -r 's/^[^0-9]*(.*)..*$/1/'
This takes everything from the first numerical value encountered after the beginning of the string to the end of the string minus the last dot and what follows it.
^[^0-9]*
skips all non-numerical: NOT zero to nine ([^0-9]
), 0 to N times (*
), from the beginning of the line (^
)...*$
matches any character (.
), 0 to N times (*
), before the end of the line ($
), and preceeded by a dot (.
). In our case this is.spr
but it could apply to any other "dot + N-characters" file extension:.s
,.yaythatsacoolextension
, etc, work as well.(.*)
, referred as1
later, keeps (this is the purpose of the parenthesis, we couldn't refer to it later otherwise) what's in the middle: any character (.
), 0 to N times (*
). This gives us8.2-mp2-2017-93-43-11-65-48
.
So if you need to list into a file the normalized names of all .spr
contained in the current directory, for example, you can do something like this:
for i in *.spr
do
echo $i | sed -r 's/^[^0-9]*(.*)..*$/1/' >> mylist.lst
done
And tomorrow, should you need to do that with .blop
files instead, just turn the *.spr
above into *.blop
.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
How about this?
echo bob-type-8.2-mp2-2017-93-43-11-65-48.spr | sed -r 's/^[^0-9]*(.*)..*$/1/'
This takes everything from the first numerical value encountered after the beginning of the string to the end of the string minus the last dot and what follows it.
^[^0-9]*
skips all non-numerical: NOT zero to nine ([^0-9]
), 0 to N times (*
), from the beginning of the line (^
)...*$
matches any character (.
), 0 to N times (*
), before the end of the line ($
), and preceeded by a dot (.
). In our case this is.spr
but it could apply to any other "dot + N-characters" file extension:.s
,.yaythatsacoolextension
, etc, work as well.(.*)
, referred as1
later, keeps (this is the purpose of the parenthesis, we couldn't refer to it later otherwise) what's in the middle: any character (.
), 0 to N times (*
). This gives us8.2-mp2-2017-93-43-11-65-48
.
So if you need to list into a file the normalized names of all .spr
contained in the current directory, for example, you can do something like this:
for i in *.spr
do
echo $i | sed -r 's/^[^0-9]*(.*)..*$/1/' >> mylist.lst
done
And tomorrow, should you need to do that with .blop
files instead, just turn the *.spr
above into *.blop
.
How about this?
echo bob-type-8.2-mp2-2017-93-43-11-65-48.spr | sed -r 's/^[^0-9]*(.*)..*$/1/'
This takes everything from the first numerical value encountered after the beginning of the string to the end of the string minus the last dot and what follows it.
^[^0-9]*
skips all non-numerical: NOT zero to nine ([^0-9]
), 0 to N times (*
), from the beginning of the line (^
)...*$
matches any character (.
), 0 to N times (*
), before the end of the line ($
), and preceeded by a dot (.
). In our case this is.spr
but it could apply to any other "dot + N-characters" file extension:.s
,.yaythatsacoolextension
, etc, work as well.(.*)
, referred as1
later, keeps (this is the purpose of the parenthesis, we couldn't refer to it later otherwise) what's in the middle: any character (.
), 0 to N times (*
). This gives us8.2-mp2-2017-93-43-11-65-48
.
So if you need to list into a file the normalized names of all .spr
contained in the current directory, for example, you can do something like this:
for i in *.spr
do
echo $i | sed -r 's/^[^0-9]*(.*)..*$/1/' >> mylist.lst
done
And tomorrow, should you need to do that with .blop
files instead, just turn the *.spr
above into *.blop
.
edited Oct 3 '17 at 13:54
answered Oct 3 '17 at 13:11
Shlublu
1737
1737
add a comment |Â
add a comment |Â
up vote
0
down vote
Sed is one of the possible approaches
echo bob-type-8.2-mp2-2017-93-43-11-65-48.spr | sed 's/^[^0-9]*//;s/....$//'
8.2-mp2-2017-93-43-11-65-48
regexps mean
- replace all non-numerical symbols from beginning with nothing till
the first numerical - replace last 4 chars with nothinig
yep, overseen that, updated now
â Tagwint
Oct 3 '17 at 13:15
add a comment |Â
up vote
0
down vote
Sed is one of the possible approaches
echo bob-type-8.2-mp2-2017-93-43-11-65-48.spr | sed 's/^[^0-9]*//;s/....$//'
8.2-mp2-2017-93-43-11-65-48
regexps mean
- replace all non-numerical symbols from beginning with nothing till
the first numerical - replace last 4 chars with nothinig
yep, overseen that, updated now
â Tagwint
Oct 3 '17 at 13:15
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Sed is one of the possible approaches
echo bob-type-8.2-mp2-2017-93-43-11-65-48.spr | sed 's/^[^0-9]*//;s/....$//'
8.2-mp2-2017-93-43-11-65-48
regexps mean
- replace all non-numerical symbols from beginning with nothing till
the first numerical - replace last 4 chars with nothinig
Sed is one of the possible approaches
echo bob-type-8.2-mp2-2017-93-43-11-65-48.spr | sed 's/^[^0-9]*//;s/....$//'
8.2-mp2-2017-93-43-11-65-48
regexps mean
- replace all non-numerical symbols from beginning with nothing till
the first numerical - replace last 4 chars with nothinig
edited Oct 3 '17 at 13:14
answered Oct 3 '17 at 13:08
Tagwint
1,3181612
1,3181612
yep, overseen that, updated now
â Tagwint
Oct 3 '17 at 13:15
add a comment |Â
yep, overseen that, updated now
â Tagwint
Oct 3 '17 at 13:15
yep, overseen that, updated now
â Tagwint
Oct 3 '17 at 13:15
yep, overseen that, updated now
â Tagwint
Oct 3 '17 at 13:15
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%2f395826%2fhow-to-get-the-string-after-the-first-numerical%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