How extract string from file name in shell script? [duplicate]
This question already has an answer here:
Extract part of a filename shell script
3 answers
I need to get text that includes alphabetic characters and _ from this format of file name: oracle_NAME_OF_DB_USER.log , so PARAM=NAME_OF_DB_USER.
Couldn't find the best regex to use in a for loop:
LIST=oracle_*.log
for file in $LIST; do
.
.
.
USER=${extracted_file_name}
regex bash shell unix
marked as duplicate by Wiktor Stribiżew, Kent, Biffen, tripleee
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 11:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Extract part of a filename shell script
3 answers
I need to get text that includes alphabetic characters and _ from this format of file name: oracle_NAME_OF_DB_USER.log , so PARAM=NAME_OF_DB_USER.
Couldn't find the best regex to use in a for loop:
LIST=oracle_*.log
for file in $LIST; do
.
.
.
USER=${extracted_file_name}
regex bash shell unix
marked as duplicate by Wiktor Stribiżew, Kent, Biffen, tripleee
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 11:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
is the file of pattern oracle_<username>.log ?
– stack0114106
Nov 21 '18 at 10:25
@stack0114106 yes
– nfrank
Nov 21 '18 at 10:32
add a comment |
This question already has an answer here:
Extract part of a filename shell script
3 answers
I need to get text that includes alphabetic characters and _ from this format of file name: oracle_NAME_OF_DB_USER.log , so PARAM=NAME_OF_DB_USER.
Couldn't find the best regex to use in a for loop:
LIST=oracle_*.log
for file in $LIST; do
.
.
.
USER=${extracted_file_name}
regex bash shell unix
This question already has an answer here:
Extract part of a filename shell script
3 answers
I need to get text that includes alphabetic characters and _ from this format of file name: oracle_NAME_OF_DB_USER.log , so PARAM=NAME_OF_DB_USER.
Couldn't find the best regex to use in a for loop:
LIST=oracle_*.log
for file in $LIST; do
.
.
.
USER=${extracted_file_name}
This question already has an answer here:
Extract part of a filename shell script
3 answers
regex bash shell unix
regex bash shell unix
asked Nov 21 '18 at 9:59
nfranknfrank
106
106
marked as duplicate by Wiktor Stribiżew, Kent, Biffen, tripleee
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 11:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Wiktor Stribiżew, Kent, Biffen, tripleee
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 11:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
is the file of pattern oracle_<username>.log ?
– stack0114106
Nov 21 '18 at 10:25
@stack0114106 yes
– nfrank
Nov 21 '18 at 10:32
add a comment |
is the file of pattern oracle_<username>.log ?
– stack0114106
Nov 21 '18 at 10:25
@stack0114106 yes
– nfrank
Nov 21 '18 at 10:32
is the file of pattern oracle_<username>.log ?
– stack0114106
Nov 21 '18 at 10:25
is the file of pattern oracle_<username>.log ?
– stack0114106
Nov 21 '18 at 10:25
@stack0114106 yes
– nfrank
Nov 21 '18 at 10:32
@stack0114106 yes
– nfrank
Nov 21 '18 at 10:32
add a comment |
3 Answers
3
active
oldest
votes
Here is a pure BASH answer:
for file in oracle_ABC_USER_1.log oracle_ABC_USER_2.log oracle_ABC_USER_ADMIN_1.log oracle_ABC_USER_ADMIN_2.log oracle_NAME_OF_DB_USER.log; do
[[ $file =~ oracle_(.*)[.]log ]]
echo ${BASH_REMATCH[1]}
done
And here is the output of the commands above:
ABC_USER_1
ABC_USER_2
ABC_USER_ADMIN_1
ABC_USER_ADMIN_2
NAME_OF_DB_USER
Thank you @accdias BASH_REMATCH is new to me, happy to discover it now :)
– nfrank
Nov 21 '18 at 12:03
You're welcome. :-)
– accdias
Nov 21 '18 at 14:04
add a comment |
Assuming that you have a set of files like below
> ls -1 oracle*
oracle_ABC_USER_1.log
oracle_ABC_USER_2.log
oracle_ABC_USER_ADMIN_1.log
oracle_ABC_USER_ADMIN_2.log
oracle_NAME_OF_DB_USER.log
The below perl command should get you the names that you are expecting
> perl -ne ' BEGIN { @files=glob("oracle*.log"); foreach (@files) {s/^oracle_(.*).log/_1/g; print "$_n"} exit } '
_ABC_USER_1
_ABC_USER_2
_ABC_USER_ADMIN_1
_ABC_USER_ADMIN_2
_NAME_OF_DB_USER
>
Does this help?.
More compact one:
> perl -ne ' BEGIN { s/^oracle_(.*).log/_1/g and print "$_n" for glob("oracle*.log"); exit}'
_ABC_USER_1
_ABC_USER_2
_ABC_USER_ADMIN_1
_ABC_USER_ADMIN_2
_NAME_OF_DB_USER
>
add a comment |
What about this:
<Prompt>:/C/Temp_Folder/tralala$ ls -ltra
total 0
drwxrwxrwx 1 Username Groupname 512 Nov 21 11:43 ..
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_123.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC_123.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 something_else.log
drwxrwxrwx 1 Username Groupname 512 Nov 21 11:48 .
<Prompt>:/C/Temp_Folder/tralala$ ls -ltra | grep "oracle[_A-Za-z0-9]*.log"
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_123.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC_123.log
The regular expression oracle[_A-Za-z0-9]*.log mentions all characters, small letters and capitals, digits and underscore.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is a pure BASH answer:
for file in oracle_ABC_USER_1.log oracle_ABC_USER_2.log oracle_ABC_USER_ADMIN_1.log oracle_ABC_USER_ADMIN_2.log oracle_NAME_OF_DB_USER.log; do
[[ $file =~ oracle_(.*)[.]log ]]
echo ${BASH_REMATCH[1]}
done
And here is the output of the commands above:
ABC_USER_1
ABC_USER_2
ABC_USER_ADMIN_1
ABC_USER_ADMIN_2
NAME_OF_DB_USER
Thank you @accdias BASH_REMATCH is new to me, happy to discover it now :)
– nfrank
Nov 21 '18 at 12:03
You're welcome. :-)
– accdias
Nov 21 '18 at 14:04
add a comment |
Here is a pure BASH answer:
for file in oracle_ABC_USER_1.log oracle_ABC_USER_2.log oracle_ABC_USER_ADMIN_1.log oracle_ABC_USER_ADMIN_2.log oracle_NAME_OF_DB_USER.log; do
[[ $file =~ oracle_(.*)[.]log ]]
echo ${BASH_REMATCH[1]}
done
And here is the output of the commands above:
ABC_USER_1
ABC_USER_2
ABC_USER_ADMIN_1
ABC_USER_ADMIN_2
NAME_OF_DB_USER
Thank you @accdias BASH_REMATCH is new to me, happy to discover it now :)
– nfrank
Nov 21 '18 at 12:03
You're welcome. :-)
– accdias
Nov 21 '18 at 14:04
add a comment |
Here is a pure BASH answer:
for file in oracle_ABC_USER_1.log oracle_ABC_USER_2.log oracle_ABC_USER_ADMIN_1.log oracle_ABC_USER_ADMIN_2.log oracle_NAME_OF_DB_USER.log; do
[[ $file =~ oracle_(.*)[.]log ]]
echo ${BASH_REMATCH[1]}
done
And here is the output of the commands above:
ABC_USER_1
ABC_USER_2
ABC_USER_ADMIN_1
ABC_USER_ADMIN_2
NAME_OF_DB_USER
Here is a pure BASH answer:
for file in oracle_ABC_USER_1.log oracle_ABC_USER_2.log oracle_ABC_USER_ADMIN_1.log oracle_ABC_USER_ADMIN_2.log oracle_NAME_OF_DB_USER.log; do
[[ $file =~ oracle_(.*)[.]log ]]
echo ${BASH_REMATCH[1]}
done
And here is the output of the commands above:
ABC_USER_1
ABC_USER_2
ABC_USER_ADMIN_1
ABC_USER_ADMIN_2
NAME_OF_DB_USER
answered Nov 21 '18 at 11:00
accdiasaccdias
493610
493610
Thank you @accdias BASH_REMATCH is new to me, happy to discover it now :)
– nfrank
Nov 21 '18 at 12:03
You're welcome. :-)
– accdias
Nov 21 '18 at 14:04
add a comment |
Thank you @accdias BASH_REMATCH is new to me, happy to discover it now :)
– nfrank
Nov 21 '18 at 12:03
You're welcome. :-)
– accdias
Nov 21 '18 at 14:04
Thank you @accdias BASH_REMATCH is new to me, happy to discover it now :)
– nfrank
Nov 21 '18 at 12:03
Thank you @accdias BASH_REMATCH is new to me, happy to discover it now :)
– nfrank
Nov 21 '18 at 12:03
You're welcome. :-)
– accdias
Nov 21 '18 at 14:04
You're welcome. :-)
– accdias
Nov 21 '18 at 14:04
add a comment |
Assuming that you have a set of files like below
> ls -1 oracle*
oracle_ABC_USER_1.log
oracle_ABC_USER_2.log
oracle_ABC_USER_ADMIN_1.log
oracle_ABC_USER_ADMIN_2.log
oracle_NAME_OF_DB_USER.log
The below perl command should get you the names that you are expecting
> perl -ne ' BEGIN { @files=glob("oracle*.log"); foreach (@files) {s/^oracle_(.*).log/_1/g; print "$_n"} exit } '
_ABC_USER_1
_ABC_USER_2
_ABC_USER_ADMIN_1
_ABC_USER_ADMIN_2
_NAME_OF_DB_USER
>
Does this help?.
More compact one:
> perl -ne ' BEGIN { s/^oracle_(.*).log/_1/g and print "$_n" for glob("oracle*.log"); exit}'
_ABC_USER_1
_ABC_USER_2
_ABC_USER_ADMIN_1
_ABC_USER_ADMIN_2
_NAME_OF_DB_USER
>
add a comment |
Assuming that you have a set of files like below
> ls -1 oracle*
oracle_ABC_USER_1.log
oracle_ABC_USER_2.log
oracle_ABC_USER_ADMIN_1.log
oracle_ABC_USER_ADMIN_2.log
oracle_NAME_OF_DB_USER.log
The below perl command should get you the names that you are expecting
> perl -ne ' BEGIN { @files=glob("oracle*.log"); foreach (@files) {s/^oracle_(.*).log/_1/g; print "$_n"} exit } '
_ABC_USER_1
_ABC_USER_2
_ABC_USER_ADMIN_1
_ABC_USER_ADMIN_2
_NAME_OF_DB_USER
>
Does this help?.
More compact one:
> perl -ne ' BEGIN { s/^oracle_(.*).log/_1/g and print "$_n" for glob("oracle*.log"); exit}'
_ABC_USER_1
_ABC_USER_2
_ABC_USER_ADMIN_1
_ABC_USER_ADMIN_2
_NAME_OF_DB_USER
>
add a comment |
Assuming that you have a set of files like below
> ls -1 oracle*
oracle_ABC_USER_1.log
oracle_ABC_USER_2.log
oracle_ABC_USER_ADMIN_1.log
oracle_ABC_USER_ADMIN_2.log
oracle_NAME_OF_DB_USER.log
The below perl command should get you the names that you are expecting
> perl -ne ' BEGIN { @files=glob("oracle*.log"); foreach (@files) {s/^oracle_(.*).log/_1/g; print "$_n"} exit } '
_ABC_USER_1
_ABC_USER_2
_ABC_USER_ADMIN_1
_ABC_USER_ADMIN_2
_NAME_OF_DB_USER
>
Does this help?.
More compact one:
> perl -ne ' BEGIN { s/^oracle_(.*).log/_1/g and print "$_n" for glob("oracle*.log"); exit}'
_ABC_USER_1
_ABC_USER_2
_ABC_USER_ADMIN_1
_ABC_USER_ADMIN_2
_NAME_OF_DB_USER
>
Assuming that you have a set of files like below
> ls -1 oracle*
oracle_ABC_USER_1.log
oracle_ABC_USER_2.log
oracle_ABC_USER_ADMIN_1.log
oracle_ABC_USER_ADMIN_2.log
oracle_NAME_OF_DB_USER.log
The below perl command should get you the names that you are expecting
> perl -ne ' BEGIN { @files=glob("oracle*.log"); foreach (@files) {s/^oracle_(.*).log/_1/g; print "$_n"} exit } '
_ABC_USER_1
_ABC_USER_2
_ABC_USER_ADMIN_1
_ABC_USER_ADMIN_2
_NAME_OF_DB_USER
>
Does this help?.
More compact one:
> perl -ne ' BEGIN { s/^oracle_(.*).log/_1/g and print "$_n" for glob("oracle*.log"); exit}'
_ABC_USER_1
_ABC_USER_2
_ABC_USER_ADMIN_1
_ABC_USER_ADMIN_2
_NAME_OF_DB_USER
>
edited Nov 21 '18 at 10:49
answered Nov 21 '18 at 10:38
stack0114106stack0114106
4,7602423
4,7602423
add a comment |
add a comment |
What about this:
<Prompt>:/C/Temp_Folder/tralala$ ls -ltra
total 0
drwxrwxrwx 1 Username Groupname 512 Nov 21 11:43 ..
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_123.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC_123.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 something_else.log
drwxrwxrwx 1 Username Groupname 512 Nov 21 11:48 .
<Prompt>:/C/Temp_Folder/tralala$ ls -ltra | grep "oracle[_A-Za-z0-9]*.log"
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_123.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC_123.log
The regular expression oracle[_A-Za-z0-9]*.log mentions all characters, small letters and capitals, digits and underscore.
add a comment |
What about this:
<Prompt>:/C/Temp_Folder/tralala$ ls -ltra
total 0
drwxrwxrwx 1 Username Groupname 512 Nov 21 11:43 ..
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_123.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC_123.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 something_else.log
drwxrwxrwx 1 Username Groupname 512 Nov 21 11:48 .
<Prompt>:/C/Temp_Folder/tralala$ ls -ltra | grep "oracle[_A-Za-z0-9]*.log"
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_123.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC_123.log
The regular expression oracle[_A-Za-z0-9]*.log mentions all characters, small letters and capitals, digits and underscore.
add a comment |
What about this:
<Prompt>:/C/Temp_Folder/tralala$ ls -ltra
total 0
drwxrwxrwx 1 Username Groupname 512 Nov 21 11:43 ..
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_123.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC_123.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 something_else.log
drwxrwxrwx 1 Username Groupname 512 Nov 21 11:48 .
<Prompt>:/C/Temp_Folder/tralala$ ls -ltra | grep "oracle[_A-Za-z0-9]*.log"
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_123.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC_123.log
The regular expression oracle[_A-Za-z0-9]*.log mentions all characters, small letters and capitals, digits and underscore.
What about this:
<Prompt>:/C/Temp_Folder/tralala$ ls -ltra
total 0
drwxrwxrwx 1 Username Groupname 512 Nov 21 11:43 ..
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_123.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC_123.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 something_else.log
drwxrwxrwx 1 Username Groupname 512 Nov 21 11:48 .
<Prompt>:/C/Temp_Folder/tralala$ ls -ltra | grep "oracle[_A-Za-z0-9]*.log"
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_123.log
-rwxrwxrwx 1 Username Groupname 0 Nov 21 11:44 oracle_ABC_123.log
The regular expression oracle[_A-Za-z0-9]*.log mentions all characters, small letters and capitals, digits and underscore.
answered Nov 21 '18 at 10:49
DominiqueDominique
2,33542042
2,33542042
add a comment |
add a comment |
is the file of pattern oracle_<username>.log ?
– stack0114106
Nov 21 '18 at 10:25
@stack0114106 yes
– nfrank
Nov 21 '18 at 10:32