extract substring (filename) with bash/grep/awk/etc











up vote
1
down vote

favorite












I have a console output process string like this:



plex     24029 33.3  3.4 330284 63656 ?        Sl   22:37   6:01 /volume1/@appstore/Plex Media Server/Plex Transcoder -codec:0 mpeg2video -codec:1 ac3 -i /volume1/video/classic/Now We've Seen It All!.1976.mkv -filter_complex [0:0]yadif[0];[0]scale=w=768:h=576[1];[1]format=pix_fmts=yuv420p|nv12[2] -filter_complex [0:1] aresample=async=1:ocl='stereo':osr=48000[3] -map [2] -metadata:s:0 language=eng -codec:0 libx264 -crf:0 16 -maxrate:0 7752k -bufsize:0 15504k -r:0 25 -preset:0 veryfast -x264opts:0 subme=1:me_range=4:rc_lookahead=10:me=hex:8x8dct=0:partitions=none -force_key_frames:0 expr:gte(t,0+n_forced*5) -map [3] -metadata:s:1 language=rus -codec:1 aac -b:1 256k -segment_format mpegts -f ssegment -individual_header_trailer 0 -segment_time 5 -segment_start_number 0 -segment_copyts 1 -segment_time_delta 0.0625 -segment_list http://127.0.0.1:32400/video/:/transcode/session/vgh38cdsdmd70euan70xdqh8/cf3a3188-52f9-4b26-8b63-1c348a7c9348/seglist -segment_list_type csv -segment_list_size 2147483647 -segment_list_separate_stream_times 1 -max_delay 5000000 -avoid_negative_ts disabled -map_metadata -1 -map_chapters -1 media-%05d.ts -start_at_zero -copyts -vsync cfr -y -nostats -loglevel quiet -loglevel_plex error -progressurl http://127.0.0.1:32400/video/:/transcode/session/vgh38cdsdmd70euan70xdqh8/cf3a3188-52f9-4b26-8b63-1c348a7c9348/progress


How can I easily extract this filename /volume1/video/classic/Now We've Seen It All!.1976.mkv?



Path and file extensions can be different (.avi,.mkv,.mov,.mpeg).










share|improve this question




























    up vote
    1
    down vote

    favorite












    I have a console output process string like this:



    plex     24029 33.3  3.4 330284 63656 ?        Sl   22:37   6:01 /volume1/@appstore/Plex Media Server/Plex Transcoder -codec:0 mpeg2video -codec:1 ac3 -i /volume1/video/classic/Now We've Seen It All!.1976.mkv -filter_complex [0:0]yadif[0];[0]scale=w=768:h=576[1];[1]format=pix_fmts=yuv420p|nv12[2] -filter_complex [0:1] aresample=async=1:ocl='stereo':osr=48000[3] -map [2] -metadata:s:0 language=eng -codec:0 libx264 -crf:0 16 -maxrate:0 7752k -bufsize:0 15504k -r:0 25 -preset:0 veryfast -x264opts:0 subme=1:me_range=4:rc_lookahead=10:me=hex:8x8dct=0:partitions=none -force_key_frames:0 expr:gte(t,0+n_forced*5) -map [3] -metadata:s:1 language=rus -codec:1 aac -b:1 256k -segment_format mpegts -f ssegment -individual_header_trailer 0 -segment_time 5 -segment_start_number 0 -segment_copyts 1 -segment_time_delta 0.0625 -segment_list http://127.0.0.1:32400/video/:/transcode/session/vgh38cdsdmd70euan70xdqh8/cf3a3188-52f9-4b26-8b63-1c348a7c9348/seglist -segment_list_type csv -segment_list_size 2147483647 -segment_list_separate_stream_times 1 -max_delay 5000000 -avoid_negative_ts disabled -map_metadata -1 -map_chapters -1 media-%05d.ts -start_at_zero -copyts -vsync cfr -y -nostats -loglevel quiet -loglevel_plex error -progressurl http://127.0.0.1:32400/video/:/transcode/session/vgh38cdsdmd70euan70xdqh8/cf3a3188-52f9-4b26-8b63-1c348a7c9348/progress


    How can I easily extract this filename /volume1/video/classic/Now We've Seen It All!.1976.mkv?



    Path and file extensions can be different (.avi,.mkv,.mov,.mpeg).










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a console output process string like this:



      plex     24029 33.3  3.4 330284 63656 ?        Sl   22:37   6:01 /volume1/@appstore/Plex Media Server/Plex Transcoder -codec:0 mpeg2video -codec:1 ac3 -i /volume1/video/classic/Now We've Seen It All!.1976.mkv -filter_complex [0:0]yadif[0];[0]scale=w=768:h=576[1];[1]format=pix_fmts=yuv420p|nv12[2] -filter_complex [0:1] aresample=async=1:ocl='stereo':osr=48000[3] -map [2] -metadata:s:0 language=eng -codec:0 libx264 -crf:0 16 -maxrate:0 7752k -bufsize:0 15504k -r:0 25 -preset:0 veryfast -x264opts:0 subme=1:me_range=4:rc_lookahead=10:me=hex:8x8dct=0:partitions=none -force_key_frames:0 expr:gte(t,0+n_forced*5) -map [3] -metadata:s:1 language=rus -codec:1 aac -b:1 256k -segment_format mpegts -f ssegment -individual_header_trailer 0 -segment_time 5 -segment_start_number 0 -segment_copyts 1 -segment_time_delta 0.0625 -segment_list http://127.0.0.1:32400/video/:/transcode/session/vgh38cdsdmd70euan70xdqh8/cf3a3188-52f9-4b26-8b63-1c348a7c9348/seglist -segment_list_type csv -segment_list_size 2147483647 -segment_list_separate_stream_times 1 -max_delay 5000000 -avoid_negative_ts disabled -map_metadata -1 -map_chapters -1 media-%05d.ts -start_at_zero -copyts -vsync cfr -y -nostats -loglevel quiet -loglevel_plex error -progressurl http://127.0.0.1:32400/video/:/transcode/session/vgh38cdsdmd70euan70xdqh8/cf3a3188-52f9-4b26-8b63-1c348a7c9348/progress


      How can I easily extract this filename /volume1/video/classic/Now We've Seen It All!.1976.mkv?



      Path and file extensions can be different (.avi,.mkv,.mov,.mpeg).










      share|improve this question















      I have a console output process string like this:



      plex     24029 33.3  3.4 330284 63656 ?        Sl   22:37   6:01 /volume1/@appstore/Plex Media Server/Plex Transcoder -codec:0 mpeg2video -codec:1 ac3 -i /volume1/video/classic/Now We've Seen It All!.1976.mkv -filter_complex [0:0]yadif[0];[0]scale=w=768:h=576[1];[1]format=pix_fmts=yuv420p|nv12[2] -filter_complex [0:1] aresample=async=1:ocl='stereo':osr=48000[3] -map [2] -metadata:s:0 language=eng -codec:0 libx264 -crf:0 16 -maxrate:0 7752k -bufsize:0 15504k -r:0 25 -preset:0 veryfast -x264opts:0 subme=1:me_range=4:rc_lookahead=10:me=hex:8x8dct=0:partitions=none -force_key_frames:0 expr:gte(t,0+n_forced*5) -map [3] -metadata:s:1 language=rus -codec:1 aac -b:1 256k -segment_format mpegts -f ssegment -individual_header_trailer 0 -segment_time 5 -segment_start_number 0 -segment_copyts 1 -segment_time_delta 0.0625 -segment_list http://127.0.0.1:32400/video/:/transcode/session/vgh38cdsdmd70euan70xdqh8/cf3a3188-52f9-4b26-8b63-1c348a7c9348/seglist -segment_list_type csv -segment_list_size 2147483647 -segment_list_separate_stream_times 1 -max_delay 5000000 -avoid_negative_ts disabled -map_metadata -1 -map_chapters -1 media-%05d.ts -start_at_zero -copyts -vsync cfr -y -nostats -loglevel quiet -loglevel_plex error -progressurl http://127.0.0.1:32400/video/:/transcode/session/vgh38cdsdmd70euan70xdqh8/cf3a3188-52f9-4b26-8b63-1c348a7c9348/progress


      How can I easily extract this filename /volume1/video/classic/Now We've Seen It All!.1976.mkv?



      Path and file extensions can be different (.avi,.mkv,.mov,.mpeg).







      command-line bash text-processing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 at 19:59









      dessert

      21.5k55896




      21.5k55896










      asked Nov 26 at 14:01









      Vlad G

      61




      61






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote













          Assuming the arguments are always in the same order:



          grep -oP '(?<=-i ).*?(?= -filter_complex)'


          Explanation:





          • -o - Print only the matching part of the line


          • -P - Use Perl extensions


          • (?<=-i ) - Lookbehind for -i


          • .*? - Match zero or more characters, non-greedily


          • (?= -filter_complex) - Lookahead for -filter_complex






          share|improve this answer






























            up vote
            1
            down vote













            You can filter out the string between “-i ” and “ -filter_complex” with:



            sed 's/.*-i (.*) -filter_complex.*/1/'


            It saves the string between these two as group 1 and replaces the whole line by it.



            Example run



            $ echo "plex     24029 33.3  3.4 330284 63656 ?        Sl   22:37   6:01 /volume1/@appstore/Plex Media Server/Plex Transcoder -codec:0 mpeg2video -codec:1 ac3 -i /volume1/video/classic/Now We've Seen It All"'!'".1976.mkv -filter_complex" | sed 's/.*-i (.*) -filter_complex.*/1/'
            /volume1/video/classic/Now We've Seen It All!.1976.mkv





            share|improve this answer




























              up vote
              0
              down vote













              It's not easy and error-prone without using a proper options parser such as getopts but you could try using ack-grep like that:



              ps ... | ack -o '-i .+?(-|$)' | sed 's,^-i ,,' | sed 's,-$,,'


              You can also use GNU grep experimental -P option:



              ps ... | grep -oP '-i .+?(-|$)' | sed 's,^-i ,,' | sed 's,-$,,'





              share|improve this answer





















                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "89"
                };
                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',
                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
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1096181%2fextract-substring-filename-with-bash-grep-awk-etc%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                1
                down vote













                Assuming the arguments are always in the same order:



                grep -oP '(?<=-i ).*?(?= -filter_complex)'


                Explanation:





                • -o - Print only the matching part of the line


                • -P - Use Perl extensions


                • (?<=-i ) - Lookbehind for -i


                • .*? - Match zero or more characters, non-greedily


                • (?= -filter_complex) - Lookahead for -filter_complex






                share|improve this answer



























                  up vote
                  1
                  down vote













                  Assuming the arguments are always in the same order:



                  grep -oP '(?<=-i ).*?(?= -filter_complex)'


                  Explanation:





                  • -o - Print only the matching part of the line


                  • -P - Use Perl extensions


                  • (?<=-i ) - Lookbehind for -i


                  • .*? - Match zero or more characters, non-greedily


                  • (?= -filter_complex) - Lookahead for -filter_complex






                  share|improve this answer

























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    Assuming the arguments are always in the same order:



                    grep -oP '(?<=-i ).*?(?= -filter_complex)'


                    Explanation:





                    • -o - Print only the matching part of the line


                    • -P - Use Perl extensions


                    • (?<=-i ) - Lookbehind for -i


                    • .*? - Match zero or more characters, non-greedily


                    • (?= -filter_complex) - Lookahead for -filter_complex






                    share|improve this answer














                    Assuming the arguments are always in the same order:



                    grep -oP '(?<=-i ).*?(?= -filter_complex)'


                    Explanation:





                    • -o - Print only the matching part of the line


                    • -P - Use Perl extensions


                    • (?<=-i ) - Lookbehind for -i


                    • .*? - Match zero or more characters, non-greedily


                    • (?= -filter_complex) - Lookahead for -filter_complex







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 26 at 14:41

























                    answered Nov 26 at 14:27









                    wjandrea

                    8,05142258




                    8,05142258
























                        up vote
                        1
                        down vote













                        You can filter out the string between “-i ” and “ -filter_complex” with:



                        sed 's/.*-i (.*) -filter_complex.*/1/'


                        It saves the string between these two as group 1 and replaces the whole line by it.



                        Example run



                        $ echo "plex     24029 33.3  3.4 330284 63656 ?        Sl   22:37   6:01 /volume1/@appstore/Plex Media Server/Plex Transcoder -codec:0 mpeg2video -codec:1 ac3 -i /volume1/video/classic/Now We've Seen It All"'!'".1976.mkv -filter_complex" | sed 's/.*-i (.*) -filter_complex.*/1/'
                        /volume1/video/classic/Now We've Seen It All!.1976.mkv





                        share|improve this answer

























                          up vote
                          1
                          down vote













                          You can filter out the string between “-i ” and “ -filter_complex” with:



                          sed 's/.*-i (.*) -filter_complex.*/1/'


                          It saves the string between these two as group 1 and replaces the whole line by it.



                          Example run



                          $ echo "plex     24029 33.3  3.4 330284 63656 ?        Sl   22:37   6:01 /volume1/@appstore/Plex Media Server/Plex Transcoder -codec:0 mpeg2video -codec:1 ac3 -i /volume1/video/classic/Now We've Seen It All"'!'".1976.mkv -filter_complex" | sed 's/.*-i (.*) -filter_complex.*/1/'
                          /volume1/video/classic/Now We've Seen It All!.1976.mkv





                          share|improve this answer























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            You can filter out the string between “-i ” and “ -filter_complex” with:



                            sed 's/.*-i (.*) -filter_complex.*/1/'


                            It saves the string between these two as group 1 and replaces the whole line by it.



                            Example run



                            $ echo "plex     24029 33.3  3.4 330284 63656 ?        Sl   22:37   6:01 /volume1/@appstore/Plex Media Server/Plex Transcoder -codec:0 mpeg2video -codec:1 ac3 -i /volume1/video/classic/Now We've Seen It All"'!'".1976.mkv -filter_complex" | sed 's/.*-i (.*) -filter_complex.*/1/'
                            /volume1/video/classic/Now We've Seen It All!.1976.mkv





                            share|improve this answer












                            You can filter out the string between “-i ” and “ -filter_complex” with:



                            sed 's/.*-i (.*) -filter_complex.*/1/'


                            It saves the string between these two as group 1 and replaces the whole line by it.



                            Example run



                            $ echo "plex     24029 33.3  3.4 330284 63656 ?        Sl   22:37   6:01 /volume1/@appstore/Plex Media Server/Plex Transcoder -codec:0 mpeg2video -codec:1 ac3 -i /volume1/video/classic/Now We've Seen It All"'!'".1976.mkv -filter_complex" | sed 's/.*-i (.*) -filter_complex.*/1/'
                            /volume1/video/classic/Now We've Seen It All!.1976.mkv






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 26 at 20:14









                            dessert

                            21.5k55896




                            21.5k55896






















                                up vote
                                0
                                down vote













                                It's not easy and error-prone without using a proper options parser such as getopts but you could try using ack-grep like that:



                                ps ... | ack -o '-i .+?(-|$)' | sed 's,^-i ,,' | sed 's,-$,,'


                                You can also use GNU grep experimental -P option:



                                ps ... | grep -oP '-i .+?(-|$)' | sed 's,^-i ,,' | sed 's,-$,,'





                                share|improve this answer

























                                  up vote
                                  0
                                  down vote













                                  It's not easy and error-prone without using a proper options parser such as getopts but you could try using ack-grep like that:



                                  ps ... | ack -o '-i .+?(-|$)' | sed 's,^-i ,,' | sed 's,-$,,'


                                  You can also use GNU grep experimental -P option:



                                  ps ... | grep -oP '-i .+?(-|$)' | sed 's,^-i ,,' | sed 's,-$,,'





                                  share|improve this answer























                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    It's not easy and error-prone without using a proper options parser such as getopts but you could try using ack-grep like that:



                                    ps ... | ack -o '-i .+?(-|$)' | sed 's,^-i ,,' | sed 's,-$,,'


                                    You can also use GNU grep experimental -P option:



                                    ps ... | grep -oP '-i .+?(-|$)' | sed 's,^-i ,,' | sed 's,-$,,'





                                    share|improve this answer












                                    It's not easy and error-prone without using a proper options parser such as getopts but you could try using ack-grep like that:



                                    ps ... | ack -o '-i .+?(-|$)' | sed 's,^-i ,,' | sed 's,-$,,'


                                    You can also use GNU grep experimental -P option:



                                    ps ... | grep -oP '-i .+?(-|$)' | sed 's,^-i ,,' | sed 's,-$,,'






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 26 at 14:18









                                    Arkadiusz Drabczyk

                                    97149




                                    97149






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Ask Ubuntu!


                                        • 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.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • 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%2faskubuntu.com%2fquestions%2f1096181%2fextract-substring-filename-with-bash-grep-awk-etc%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

                                        How to change which sound is reproduced for terminal bell?

                                        Can I use Tabulator js library in my java Spring + Thymeleaf project?

                                        Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents