Extract date from file name Announcing the arrival of Valued Associate #679: Cesar Manara ...

Find 108 by using 3,4,6

What initially awakened the Balrog?

How fail-safe is nr as stop bytes?

Why wasn't DOSKEY integrated with COMMAND.COM?

What is "gratricide"?

If Windows 7 doesn't support WSL, then what does Linux subsystem option mean?

How would a mousetrap for use in space work?

What is the appropriate index architecture when forced to implement IsDeleted (soft deletes)?

AppleTVs create a chatty alternate WiFi network

How does the math work when buying airline miles?

Did Deadpool rescue all of the X-Force?

What would you call this weird metallic apparatus that allows you to lift people?

Is a ledger board required if the side of my house is wood?

Why does the remaining Rebel fleet at the end of Rogue One seem dramatically larger than the one in A New Hope?

Drawing without replacement: why is the order of draw irrelevant?

ArcGIS Pro Python arcpy.CreatePersonalGDB_management

Why weren't discrete x86 CPUs ever used in game hardware?

Why should I vote and accept answers?

Converted a Scalar function to a TVF function for parallel execution-Still running in Serial mode

Time to Settle Down!

What is a fractional matching?

How do I use the new nonlinear finite element in Mathematica 12 for this equation?

How often does castling occur in grandmaster games?

When a candle burns, why does the top of wick glow if bottom of flame is hottest?



Extract date from file name



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)Windows command-line: create a file with the current date in its nameExtract substring using bashHow to save echo result into a variable?How to extract the filename without the extension from a full path?How to make a bash script that names files with today's datebash extract first number from filenameRemove version information from maven generated war fileHow to change date format in file name using bash/linux?Extract contents from log file for the last 5 minutesLatest file name saving it a variable





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm trying to extract the date from a filename and storing it to a variable and also needs to change the date format . If I have the name of the file consistent, can I go by position and extract the date?



File Name: GHDC_src_04142019_05_09_32.data.gz


looking the result of that date



04142019 as var=2019-04-14









share|improve this question







New contributor




Seshi Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



























    0















    I'm trying to extract the date from a filename and storing it to a variable and also needs to change the date format . If I have the name of the file consistent, can I go by position and extract the date?



    File Name: GHDC_src_04142019_05_09_32.data.gz


    looking the result of that date



    04142019 as var=2019-04-14









    share|improve this question







    New contributor




    Seshi Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0








      I'm trying to extract the date from a filename and storing it to a variable and also needs to change the date format . If I have the name of the file consistent, can I go by position and extract the date?



      File Name: GHDC_src_04142019_05_09_32.data.gz


      looking the result of that date



      04142019 as var=2019-04-14









      share|improve this question







      New contributor




      Seshi Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I'm trying to extract the date from a filename and storing it to a variable and also needs to change the date format . If I have the name of the file consistent, can I go by position and extract the date?



      File Name: GHDC_src_04142019_05_09_32.data.gz


      looking the result of that date



      04142019 as var=2019-04-14






      bash shell






      share|improve this question







      New contributor




      Seshi Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Seshi Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Seshi Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 10 hours ago









      Seshi KumarSeshi Kumar

      1




      1




      New contributor




      Seshi Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Seshi Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Seshi Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Since you can "go by position," try:



          $ name=GHDC_src_04142019_05_09_32.data.gz
          $ echo "${name:13:4}-${name:9:2}-${name:11:2}"
          2019-04-14


          Or, to save in a variable:



          name=GHDC_src_04142019_05_09_32.data.gz
          var="${name:13:4}-${name:9:2}-${name:11:2}"


          Since this uses pure bash, no subprocess needs to be started and no external command needs to be launched. This makes this approach efficient.



          The form ${name:offset:length} is called substring expansion. It selects characters from $name starting at offset and continuing for length. In our case, ${name:13:4} captures the year, ${name:9:2} the month, and ${name:11:2} the day.






          share|improve this answer


























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });






            Seshi Kumar is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1427125%2fextract-date-from-file-name%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Since you can "go by position," try:



            $ name=GHDC_src_04142019_05_09_32.data.gz
            $ echo "${name:13:4}-${name:9:2}-${name:11:2}"
            2019-04-14


            Or, to save in a variable:



            name=GHDC_src_04142019_05_09_32.data.gz
            var="${name:13:4}-${name:9:2}-${name:11:2}"


            Since this uses pure bash, no subprocess needs to be started and no external command needs to be launched. This makes this approach efficient.



            The form ${name:offset:length} is called substring expansion. It selects characters from $name starting at offset and continuing for length. In our case, ${name:13:4} captures the year, ${name:9:2} the month, and ${name:11:2} the day.






            share|improve this answer






























              1














              Since you can "go by position," try:



              $ name=GHDC_src_04142019_05_09_32.data.gz
              $ echo "${name:13:4}-${name:9:2}-${name:11:2}"
              2019-04-14


              Or, to save in a variable:



              name=GHDC_src_04142019_05_09_32.data.gz
              var="${name:13:4}-${name:9:2}-${name:11:2}"


              Since this uses pure bash, no subprocess needs to be started and no external command needs to be launched. This makes this approach efficient.



              The form ${name:offset:length} is called substring expansion. It selects characters from $name starting at offset and continuing for length. In our case, ${name:13:4} captures the year, ${name:9:2} the month, and ${name:11:2} the day.






              share|improve this answer




























                1












                1








                1







                Since you can "go by position," try:



                $ name=GHDC_src_04142019_05_09_32.data.gz
                $ echo "${name:13:4}-${name:9:2}-${name:11:2}"
                2019-04-14


                Or, to save in a variable:



                name=GHDC_src_04142019_05_09_32.data.gz
                var="${name:13:4}-${name:9:2}-${name:11:2}"


                Since this uses pure bash, no subprocess needs to be started and no external command needs to be launched. This makes this approach efficient.



                The form ${name:offset:length} is called substring expansion. It selects characters from $name starting at offset and continuing for length. In our case, ${name:13:4} captures the year, ${name:9:2} the month, and ${name:11:2} the day.






                share|improve this answer















                Since you can "go by position," try:



                $ name=GHDC_src_04142019_05_09_32.data.gz
                $ echo "${name:13:4}-${name:9:2}-${name:11:2}"
                2019-04-14


                Or, to save in a variable:



                name=GHDC_src_04142019_05_09_32.data.gz
                var="${name:13:4}-${name:9:2}-${name:11:2}"


                Since this uses pure bash, no subprocess needs to be started and no external command needs to be launched. This makes this approach efficient.



                The form ${name:offset:length} is called substring expansion. It selects characters from $name starting at offset and continuing for length. In our case, ${name:13:4} captures the year, ${name:9:2} the month, and ${name:11:2} the day.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 10 hours ago

























                answered 10 hours ago









                John1024John1024

                12.9k43433




                12.9k43433






















                    Seshi Kumar is a new contributor. Be nice, and check out our Code of Conduct.










                    draft saved

                    draft discarded


















                    Seshi Kumar is a new contributor. Be nice, and check out our Code of Conduct.













                    Seshi Kumar is a new contributor. Be nice, and check out our Code of Conduct.












                    Seshi Kumar is a new contributor. Be nice, and check out our Code of Conduct.
















                    Thanks for contributing an answer to Super User!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1427125%2fextract-date-from-file-name%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    VNC viewer RFB protocol error: bad desktop size 0x0I Cannot Type the Key 'd' (lowercase) in VNC Viewer...

                    Tribunal Administrativo e Fiscal de Mirandela Referências Menu de...

                    looking for continuous Screen Capture for retroactivly reproducing errors, timeback machineRolling desktop...