How extract string from file name in shell script? [duplicate]












-2
















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}









share|improve this question













marked as duplicate by Wiktor Stribiżew, Kent, Biffen, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

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
















-2
















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}









share|improve this question













marked as duplicate by Wiktor Stribiżew, Kent, Biffen, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

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














-2












-2








-2









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}









share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 9:59









nfranknfrank

106




106




marked as duplicate by Wiktor Stribiżew, Kent, Biffen, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

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 bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

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



















  • 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












3 Answers
3






active

oldest

votes


















2














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





share|improve this answer
























  • 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



















0














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
>





share|improve this answer

































    0














    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.






    share|improve this answer






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      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





      share|improve this answer
























      • 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
















      2














      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





      share|improve this answer
























      • 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














      2












      2








      2







      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





      share|improve this answer













      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






      share|improve this answer












      share|improve this answer



      share|improve this answer










      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



















      • 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













      0














      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
      >





      share|improve this answer






























        0














        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
        >





        share|improve this answer




























          0












          0








          0







          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
          >





          share|improve this answer















          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
          >






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 10:49

























          answered Nov 21 '18 at 10:38









          stack0114106stack0114106

          4,7602423




          4,7602423























              0














              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.






              share|improve this answer




























                0














                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.






                share|improve this answer


























                  0












                  0








                  0







                  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.






                  share|improve this answer













                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 10:49









                  DominiqueDominique

                  2,33542042




                  2,33542042















                      Popular posts from this blog

                      How to pass form data using jquery Ajax to insert data in database?

                      National Museum of Racing and Hall of Fame

                      Guess what letter conforming each word