Using grep to filtergrep *stringA*[number]Using the grep filterHow to filter content between specific keywords using grep?Using individual lines from grep outputs“egrep: empty (sub)expression” when attempting to filter out words from a listHow to filter pair of bracesOutput current day using calGrep multiple files and output to multiple files in a single commandHow to use grep to search for a line with one of two words but not both?Use multiple expressions in grep consumer

Housemarks (superimposed & combined letters, heraldry)

Diatonic chords of a pentatonic vs blues scale?

Assigning function to function pointer, const argument correctness?

How and why do references in academic papers work?

How can one's career as a reviewer be ended?

Why isn't Bash trap working if output is redirected to stdout?

Proving that a Russian cryptographic standard is too structured

How to befriend someone who doesn't like to talk?

How do you play "tenth" chords on the guitar?

What plausible reason could I give for my FTL drive only working in space

A Salute to Poetry

Trying to get (more) accurate readings from thermistor (electronics, math, and code inside)

Are the guests in Westworld forbidden to tell the hosts that they are robots?

If there's something that implicates the president why is there then a national security issue? (John Dowd)

Why is the length of the Kelvin unit of temperature equal to that of the Celsius unit?

NUL delimited variable

Multiband vertical antenna not working as expected

How durable are silver inlays on a blade?

What do you call the action of "describing events as they happen" like sports anchors do?

Confused with atmospheric pressure equals plastic balloon’s inner pressure

Tikz-cd diagram arrow passing under a node - not crossing it

Is the Keras Embedding layer dependent on the target label?

How was the airlock installed on the Space Shuttle mid deck?

Does the Nuka-Cola bottler actually generate nuka cola?



Using grep to filter


grep *stringA*[number]Using the grep filterHow to filter content between specific keywords using grep?Using individual lines from grep outputs“egrep: empty (sub)expression” when attempting to filter out words from a listHow to filter pair of bracesOutput current day using calGrep multiple files and output to multiple files in a single commandHow to use grep to search for a line with one of two words but not both?Use multiple expressions in grep consumer






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















Suppose I have a file.txt with the following lines :



hello myname1 is yellow.pcapng red
festive myname33 is hddd.pcapng dfdf
crude myname44 is hello.pcapng


Now my goal is to filter to the lines so it outputs to out.txt as follows :



myname1 yellow.pcapng
myname33 hddd.pcapng
myname44 hello.pcapng


Now I know that I can use :



grep -oh "w*mynamew*" /tmp/file.txt > /tmp/out.txt
grep -o '[^ ]+g' /tmp/file.txt > /tmp/out.txt


to get the both respective parts of the expression individually.
How do I combine these commands so that I get my desired output?










share|improve this question









New contributor



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














  • 3





    do you have to use grep? You have a problem that's begging for an awk solution, and awk should be available everywhere that grep is.

    – Jeff Schaller
    Jun 3 at 18:05












  • I was only familiar with grep and not awk, could you show me how to do using awk possibly?

    – gangsignwall
    Jun 3 at 18:13











  • @JeffSchaller It's actually begging for a cut solution, since this is exactly what cut is supposed to do

    – ChatterOne
    Jun 4 at 9:57











  • Good thing there is a cut solution, @ChatterOne!

    – Jeff Schaller
    Jun 4 at 10:30


















2















Suppose I have a file.txt with the following lines :



hello myname1 is yellow.pcapng red
festive myname33 is hddd.pcapng dfdf
crude myname44 is hello.pcapng


Now my goal is to filter to the lines so it outputs to out.txt as follows :



myname1 yellow.pcapng
myname33 hddd.pcapng
myname44 hello.pcapng


Now I know that I can use :



grep -oh "w*mynamew*" /tmp/file.txt > /tmp/out.txt
grep -o '[^ ]+g' /tmp/file.txt > /tmp/out.txt


to get the both respective parts of the expression individually.
How do I combine these commands so that I get my desired output?










share|improve this question









New contributor



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














  • 3





    do you have to use grep? You have a problem that's begging for an awk solution, and awk should be available everywhere that grep is.

    – Jeff Schaller
    Jun 3 at 18:05












  • I was only familiar with grep and not awk, could you show me how to do using awk possibly?

    – gangsignwall
    Jun 3 at 18:13











  • @JeffSchaller It's actually begging for a cut solution, since this is exactly what cut is supposed to do

    – ChatterOne
    Jun 4 at 9:57











  • Good thing there is a cut solution, @ChatterOne!

    – Jeff Schaller
    Jun 4 at 10:30














2












2








2


2






Suppose I have a file.txt with the following lines :



hello myname1 is yellow.pcapng red
festive myname33 is hddd.pcapng dfdf
crude myname44 is hello.pcapng


Now my goal is to filter to the lines so it outputs to out.txt as follows :



myname1 yellow.pcapng
myname33 hddd.pcapng
myname44 hello.pcapng


Now I know that I can use :



grep -oh "w*mynamew*" /tmp/file.txt > /tmp/out.txt
grep -o '[^ ]+g' /tmp/file.txt > /tmp/out.txt


to get the both respective parts of the expression individually.
How do I combine these commands so that I get my desired output?










share|improve this question









New contributor



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











Suppose I have a file.txt with the following lines :



hello myname1 is yellow.pcapng red
festive myname33 is hddd.pcapng dfdf
crude myname44 is hello.pcapng


Now my goal is to filter to the lines so it outputs to out.txt as follows :



myname1 yellow.pcapng
myname33 hddd.pcapng
myname44 hello.pcapng


Now I know that I can use :



grep -oh "w*mynamew*" /tmp/file.txt > /tmp/out.txt
grep -o '[^ ]+g' /tmp/file.txt > /tmp/out.txt


to get the both respective parts of the expression individually.
How do I combine these commands so that I get my desired output?







grep






share|improve this question









New contributor



gangsignwall 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



gangsignwall 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








edited Jun 3 at 18:06









Jeff Schaller

46.4k1166151




46.4k1166151






New contributor



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








asked Jun 3 at 18:01









gangsignwallgangsignwall

132




132




New contributor



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




New contributor




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









  • 3





    do you have to use grep? You have a problem that's begging for an awk solution, and awk should be available everywhere that grep is.

    – Jeff Schaller
    Jun 3 at 18:05












  • I was only familiar with grep and not awk, could you show me how to do using awk possibly?

    – gangsignwall
    Jun 3 at 18:13











  • @JeffSchaller It's actually begging for a cut solution, since this is exactly what cut is supposed to do

    – ChatterOne
    Jun 4 at 9:57











  • Good thing there is a cut solution, @ChatterOne!

    – Jeff Schaller
    Jun 4 at 10:30













  • 3





    do you have to use grep? You have a problem that's begging for an awk solution, and awk should be available everywhere that grep is.

    – Jeff Schaller
    Jun 3 at 18:05












  • I was only familiar with grep and not awk, could you show me how to do using awk possibly?

    – gangsignwall
    Jun 3 at 18:13











  • @JeffSchaller It's actually begging for a cut solution, since this is exactly what cut is supposed to do

    – ChatterOne
    Jun 4 at 9:57











  • Good thing there is a cut solution, @ChatterOne!

    – Jeff Schaller
    Jun 4 at 10:30








3




3





do you have to use grep? You have a problem that's begging for an awk solution, and awk should be available everywhere that grep is.

– Jeff Schaller
Jun 3 at 18:05






do you have to use grep? You have a problem that's begging for an awk solution, and awk should be available everywhere that grep is.

– Jeff Schaller
Jun 3 at 18:05














I was only familiar with grep and not awk, could you show me how to do using awk possibly?

– gangsignwall
Jun 3 at 18:13





I was only familiar with grep and not awk, could you show me how to do using awk possibly?

– gangsignwall
Jun 3 at 18:13













@JeffSchaller It's actually begging for a cut solution, since this is exactly what cut is supposed to do

– ChatterOne
Jun 4 at 9:57





@JeffSchaller It's actually begging for a cut solution, since this is exactly what cut is supposed to do

– ChatterOne
Jun 4 at 9:57













Good thing there is a cut solution, @ChatterOne!

– Jeff Schaller
Jun 4 at 10:30






Good thing there is a cut solution, @ChatterOne!

– Jeff Schaller
Jun 4 at 10:30











5 Answers
5






active

oldest

votes


















5














Given your sample data, you could assume that words #2 and #4 are what you want to extract; you'd express that in awk with:



awk ' print $2, $4 ' < /tmp/file.txt > /tmp/out.txt





share|improve this answer
































    3














    You could use cut:



    cut -d' ' -f2,4 < /tmp/file.txt > /tmp/out.txt 





    share|improve this answer






























      2














      Using awk instead of grep.



      awk 'print $2,$4' /tmp/file.txt > /tmp/out.txt


      You are piping the output of cat file.txt to awk.



      Then using awk expressions 'print $2,$4', you are printing the 2nd and 4th field of the split line, with a space to separate.



      Your output will be how you desire.






      share|improve this answer










      New contributor



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


























        1














        Could do this too:



        cat file.txt | sed "s/.*(myname[0-9]*) is ([^ ]*).*/1 2/g" | grep ^myname > /tmp/out.txt






        share|improve this answer






























          0














          Since shell script method has been provided above, Tried in Python and worked fine too



          #!/usr/bin/python
          o=[]
          k=open('i','r')
          for z in k:
          o.append(z.strip().split(' ')[1])
          o.append(z.strip().split(' ')[3])
          for d in range(0,len(o),2):
          print " ".join(o[d:d+2])


          output



          myname1 yellow.pcapng
          myname33 hddd.pcapng
          myname44 hello.pcapng





          share|improve this answer























            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            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: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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
            );



            );






            gangsignwall 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%2funix.stackexchange.com%2fquestions%2f522665%2fusing-grep-to-filter%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            5














            Given your sample data, you could assume that words #2 and #4 are what you want to extract; you'd express that in awk with:



            awk ' print $2, $4 ' < /tmp/file.txt > /tmp/out.txt





            share|improve this answer





























              5














              Given your sample data, you could assume that words #2 and #4 are what you want to extract; you'd express that in awk with:



              awk ' print $2, $4 ' < /tmp/file.txt > /tmp/out.txt





              share|improve this answer



























                5












                5








                5







                Given your sample data, you could assume that words #2 and #4 are what you want to extract; you'd express that in awk with:



                awk ' print $2, $4 ' < /tmp/file.txt > /tmp/out.txt





                share|improve this answer















                Given your sample data, you could assume that words #2 and #4 are what you want to extract; you'd express that in awk with:



                awk ' print $2, $4 ' < /tmp/file.txt > /tmp/out.txt






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jun 3 at 18:42









                Kusalananda

                150k18288474




                150k18288474










                answered Jun 3 at 18:18









                Jeff SchallerJeff Schaller

                46.4k1166151




                46.4k1166151























                    3














                    You could use cut:



                    cut -d' ' -f2,4 < /tmp/file.txt > /tmp/out.txt 





                    share|improve this answer



























                      3














                      You could use cut:



                      cut -d' ' -f2,4 < /tmp/file.txt > /tmp/out.txt 





                      share|improve this answer

























                        3












                        3








                        3







                        You could use cut:



                        cut -d' ' -f2,4 < /tmp/file.txt > /tmp/out.txt 





                        share|improve this answer













                        You could use cut:



                        cut -d' ' -f2,4 < /tmp/file.txt > /tmp/out.txt 






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jun 4 at 7:02









                        pLumopLumo

                        5,7921125




                        5,7921125





















                            2














                            Using awk instead of grep.



                            awk 'print $2,$4' /tmp/file.txt > /tmp/out.txt


                            You are piping the output of cat file.txt to awk.



                            Then using awk expressions 'print $2,$4', you are printing the 2nd and 4th field of the split line, with a space to separate.



                            Your output will be how you desire.






                            share|improve this answer










                            New contributor



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























                              2














                              Using awk instead of grep.



                              awk 'print $2,$4' /tmp/file.txt > /tmp/out.txt


                              You are piping the output of cat file.txt to awk.



                              Then using awk expressions 'print $2,$4', you are printing the 2nd and 4th field of the split line, with a space to separate.



                              Your output will be how you desire.






                              share|improve this answer










                              New contributor



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





















                                2












                                2








                                2







                                Using awk instead of grep.



                                awk 'print $2,$4' /tmp/file.txt > /tmp/out.txt


                                You are piping the output of cat file.txt to awk.



                                Then using awk expressions 'print $2,$4', you are printing the 2nd and 4th field of the split line, with a space to separate.



                                Your output will be how you desire.






                                share|improve this answer










                                New contributor



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









                                Using awk instead of grep.



                                awk 'print $2,$4' /tmp/file.txt > /tmp/out.txt


                                You are piping the output of cat file.txt to awk.



                                Then using awk expressions 'print $2,$4', you are printing the 2nd and 4th field of the split line, with a space to separate.



                                Your output will be how you desire.







                                share|improve this answer










                                New contributor



                                Mylo Mylo 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 answer



                                share|improve this answer








                                edited Jun 4 at 6:33









                                Nasir Riley

                                3,3252410




                                3,3252410






                                New contributor



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








                                answered Jun 3 at 18:19









                                Mylo MyloMylo Mylo

                                211




                                211




                                New contributor



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




                                New contributor




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























                                    1














                                    Could do this too:



                                    cat file.txt | sed "s/.*(myname[0-9]*) is ([^ ]*).*/1 2/g" | grep ^myname > /tmp/out.txt






                                    share|improve this answer



























                                      1














                                      Could do this too:



                                      cat file.txt | sed "s/.*(myname[0-9]*) is ([^ ]*).*/1 2/g" | grep ^myname > /tmp/out.txt






                                      share|improve this answer

























                                        1












                                        1








                                        1







                                        Could do this too:



                                        cat file.txt | sed "s/.*(myname[0-9]*) is ([^ ]*).*/1 2/g" | grep ^myname > /tmp/out.txt






                                        share|improve this answer













                                        Could do this too:



                                        cat file.txt | sed "s/.*(myname[0-9]*) is ([^ ]*).*/1 2/g" | grep ^myname > /tmp/out.txt







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Jun 3 at 18:23









                                        Ulysses_Ulysses_

                                        115




                                        115





















                                            0














                                            Since shell script method has been provided above, Tried in Python and worked fine too



                                            #!/usr/bin/python
                                            o=[]
                                            k=open('i','r')
                                            for z in k:
                                            o.append(z.strip().split(' ')[1])
                                            o.append(z.strip().split(' ')[3])
                                            for d in range(0,len(o),2):
                                            print " ".join(o[d:d+2])


                                            output



                                            myname1 yellow.pcapng
                                            myname33 hddd.pcapng
                                            myname44 hello.pcapng





                                            share|improve this answer



























                                              0














                                              Since shell script method has been provided above, Tried in Python and worked fine too



                                              #!/usr/bin/python
                                              o=[]
                                              k=open('i','r')
                                              for z in k:
                                              o.append(z.strip().split(' ')[1])
                                              o.append(z.strip().split(' ')[3])
                                              for d in range(0,len(o),2):
                                              print " ".join(o[d:d+2])


                                              output



                                              myname1 yellow.pcapng
                                              myname33 hddd.pcapng
                                              myname44 hello.pcapng





                                              share|improve this answer

























                                                0












                                                0








                                                0







                                                Since shell script method has been provided above, Tried in Python and worked fine too



                                                #!/usr/bin/python
                                                o=[]
                                                k=open('i','r')
                                                for z in k:
                                                o.append(z.strip().split(' ')[1])
                                                o.append(z.strip().split(' ')[3])
                                                for d in range(0,len(o),2):
                                                print " ".join(o[d:d+2])


                                                output



                                                myname1 yellow.pcapng
                                                myname33 hddd.pcapng
                                                myname44 hello.pcapng





                                                share|improve this answer













                                                Since shell script method has been provided above, Tried in Python and worked fine too



                                                #!/usr/bin/python
                                                o=[]
                                                k=open('i','r')
                                                for z in k:
                                                o.append(z.strip().split(' ')[1])
                                                o.append(z.strip().split(' ')[3])
                                                for d in range(0,len(o),2):
                                                print " ".join(o[d:d+2])


                                                output



                                                myname1 yellow.pcapng
                                                myname33 hddd.pcapng
                                                myname44 hello.pcapng






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Jun 4 at 16:09









                                                Praveen Kumar BSPraveen Kumar BS

                                                2,0572311




                                                2,0572311




















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









                                                    draft saved

                                                    draft discarded


















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












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











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














                                                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                                    • 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%2funix.stackexchange.com%2fquestions%2f522665%2fusing-grep-to-filter%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

                                                    Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                                                    Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

                                                    Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?