Filtering records of a file based on a value of a column

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite
1












I would like to extract records of a file based on a condition. The file contains 47 fields, and I would like to extract only those records that match a certain value in one of the columns, e.g.



1 XX 45 N
2 YY 34 y
3 ZZ 44 N
4 XX 89 Y
5 XX 45 N
6 YY 84 D
7 ZZ 22 S
8 ZX 12 6
9 ZA 32 8


From this file, I would like to extract all records whose COL2=XX and YY, ZZ and all other records will be excluded (as shown below).



1 XX 45 N
2 YY 34 y
4 XX 89 Y
5 XX 45 N
6 YY 84 D


Does anybody know how to do this using sed or awk or any other UNIX tool? Thank you.










share|improve this question



























    up vote
    2
    down vote

    favorite
    1












    I would like to extract records of a file based on a condition. The file contains 47 fields, and I would like to extract only those records that match a certain value in one of the columns, e.g.



    1 XX 45 N
    2 YY 34 y
    3 ZZ 44 N
    4 XX 89 Y
    5 XX 45 N
    6 YY 84 D
    7 ZZ 22 S
    8 ZX 12 6
    9 ZA 32 8


    From this file, I would like to extract all records whose COL2=XX and YY, ZZ and all other records will be excluded (as shown below).



    1 XX 45 N
    2 YY 34 y
    4 XX 89 Y
    5 XX 45 N
    6 YY 84 D


    Does anybody know how to do this using sed or awk or any other UNIX tool? Thank you.










    share|improve this question

























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      I would like to extract records of a file based on a condition. The file contains 47 fields, and I would like to extract only those records that match a certain value in one of the columns, e.g.



      1 XX 45 N
      2 YY 34 y
      3 ZZ 44 N
      4 XX 89 Y
      5 XX 45 N
      6 YY 84 D
      7 ZZ 22 S
      8 ZX 12 6
      9 ZA 32 8


      From this file, I would like to extract all records whose COL2=XX and YY, ZZ and all other records will be excluded (as shown below).



      1 XX 45 N
      2 YY 34 y
      4 XX 89 Y
      5 XX 45 N
      6 YY 84 D


      Does anybody know how to do this using sed or awk or any other UNIX tool? Thank you.










      share|improve this question















      I would like to extract records of a file based on a condition. The file contains 47 fields, and I would like to extract only those records that match a certain value in one of the columns, e.g.



      1 XX 45 N
      2 YY 34 y
      3 ZZ 44 N
      4 XX 89 Y
      5 XX 45 N
      6 YY 84 D
      7 ZZ 22 S
      8 ZX 12 6
      9 ZA 32 8


      From this file, I would like to extract all records whose COL2=XX and YY, ZZ and all other records will be excluded (as shown below).



      1 XX 45 N
      2 YY 34 y
      4 XX 89 Y
      5 XX 45 N
      6 YY 84 D


      Does anybody know how to do this using sed or awk or any other UNIX tool? Thank you.







      text-processing awk sed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 4 at 9:18









      Jeff Schaller

      33.6k851113




      33.6k851113










      asked Oct 4 at 4:25









      Shervan

      32210




      32210




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          Let's say your data is listed in a file called file.txt.You can try awk as follows:



          awk '$2 ~ /XX|YY|ZZ/' file.txt
          1 XX 45 N
          2 YY 34 y
          3 ZZ 44 N
          4 XX 89 Y
          5 XX 45 N
          6 YY 84 D
          7 ZZ 22 S


          or grep



          grep 'XX|YY|ZZ' file.txt
          1 XX 45 N
          2 YY 34 y
          3 ZZ 44 N
          4 XX 89 Y
          5 XX 45 N
          6 YY 84 D
          7 ZZ 22 S


          To sort the data based on the third column, you can pipe the output data to the command sort as follows:



          grep 'XX|YY|ZZ' file.txt | sort -k3,3n
          7 ZZ 22 S
          2 YY 34 y
          3 ZZ 44 N
          1 XX 45 N
          5 XX 45 N
          6 YY 84 D
          4 XX 89 Y





          share|improve this answer






















          • thank you. I have one question pelase how can i organize the new list based on the numbers in the third column
            – Shervan
            Oct 4 at 4:36










          • Please see my edits.
            – Goro
            Oct 4 at 4:39






          • 1




            Thank you so much @goro for the great help!
            – Shervan
            Oct 4 at 4:41










          • @Shervan note that the grep variant matches the values anywhere in the line, not just in the second field.
            – Stephen Kitt
            Oct 4 at 4:45






          • 1




            @Goro I was only wondering why you specified the starting character (.1), that’s all. Note that your sort applies to the rest of the line, starting with the third field; I think -k3,3n would be more appropriate.
            – Stephen Kitt
            Oct 4 at 5:12










          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473133%2ffiltering-records-of-a-file-based-on-a-value-of-a-column%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          Let's say your data is listed in a file called file.txt.You can try awk as follows:



          awk '$2 ~ /XX|YY|ZZ/' file.txt
          1 XX 45 N
          2 YY 34 y
          3 ZZ 44 N
          4 XX 89 Y
          5 XX 45 N
          6 YY 84 D
          7 ZZ 22 S


          or grep



          grep 'XX|YY|ZZ' file.txt
          1 XX 45 N
          2 YY 34 y
          3 ZZ 44 N
          4 XX 89 Y
          5 XX 45 N
          6 YY 84 D
          7 ZZ 22 S


          To sort the data based on the third column, you can pipe the output data to the command sort as follows:



          grep 'XX|YY|ZZ' file.txt | sort -k3,3n
          7 ZZ 22 S
          2 YY 34 y
          3 ZZ 44 N
          1 XX 45 N
          5 XX 45 N
          6 YY 84 D
          4 XX 89 Y





          share|improve this answer






















          • thank you. I have one question pelase how can i organize the new list based on the numbers in the third column
            – Shervan
            Oct 4 at 4:36










          • Please see my edits.
            – Goro
            Oct 4 at 4:39






          • 1




            Thank you so much @goro for the great help!
            – Shervan
            Oct 4 at 4:41










          • @Shervan note that the grep variant matches the values anywhere in the line, not just in the second field.
            – Stephen Kitt
            Oct 4 at 4:45






          • 1




            @Goro I was only wondering why you specified the starting character (.1), that’s all. Note that your sort applies to the rest of the line, starting with the third field; I think -k3,3n would be more appropriate.
            – Stephen Kitt
            Oct 4 at 5:12














          up vote
          4
          down vote



          accepted










          Let's say your data is listed in a file called file.txt.You can try awk as follows:



          awk '$2 ~ /XX|YY|ZZ/' file.txt
          1 XX 45 N
          2 YY 34 y
          3 ZZ 44 N
          4 XX 89 Y
          5 XX 45 N
          6 YY 84 D
          7 ZZ 22 S


          or grep



          grep 'XX|YY|ZZ' file.txt
          1 XX 45 N
          2 YY 34 y
          3 ZZ 44 N
          4 XX 89 Y
          5 XX 45 N
          6 YY 84 D
          7 ZZ 22 S


          To sort the data based on the third column, you can pipe the output data to the command sort as follows:



          grep 'XX|YY|ZZ' file.txt | sort -k3,3n
          7 ZZ 22 S
          2 YY 34 y
          3 ZZ 44 N
          1 XX 45 N
          5 XX 45 N
          6 YY 84 D
          4 XX 89 Y





          share|improve this answer






















          • thank you. I have one question pelase how can i organize the new list based on the numbers in the third column
            – Shervan
            Oct 4 at 4:36










          • Please see my edits.
            – Goro
            Oct 4 at 4:39






          • 1




            Thank you so much @goro for the great help!
            – Shervan
            Oct 4 at 4:41










          • @Shervan note that the grep variant matches the values anywhere in the line, not just in the second field.
            – Stephen Kitt
            Oct 4 at 4:45






          • 1




            @Goro I was only wondering why you specified the starting character (.1), that’s all. Note that your sort applies to the rest of the line, starting with the third field; I think -k3,3n would be more appropriate.
            – Stephen Kitt
            Oct 4 at 5:12












          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          Let's say your data is listed in a file called file.txt.You can try awk as follows:



          awk '$2 ~ /XX|YY|ZZ/' file.txt
          1 XX 45 N
          2 YY 34 y
          3 ZZ 44 N
          4 XX 89 Y
          5 XX 45 N
          6 YY 84 D
          7 ZZ 22 S


          or grep



          grep 'XX|YY|ZZ' file.txt
          1 XX 45 N
          2 YY 34 y
          3 ZZ 44 N
          4 XX 89 Y
          5 XX 45 N
          6 YY 84 D
          7 ZZ 22 S


          To sort the data based on the third column, you can pipe the output data to the command sort as follows:



          grep 'XX|YY|ZZ' file.txt | sort -k3,3n
          7 ZZ 22 S
          2 YY 34 y
          3 ZZ 44 N
          1 XX 45 N
          5 XX 45 N
          6 YY 84 D
          4 XX 89 Y





          share|improve this answer














          Let's say your data is listed in a file called file.txt.You can try awk as follows:



          awk '$2 ~ /XX|YY|ZZ/' file.txt
          1 XX 45 N
          2 YY 34 y
          3 ZZ 44 N
          4 XX 89 Y
          5 XX 45 N
          6 YY 84 D
          7 ZZ 22 S


          or grep



          grep 'XX|YY|ZZ' file.txt
          1 XX 45 N
          2 YY 34 y
          3 ZZ 44 N
          4 XX 89 Y
          5 XX 45 N
          6 YY 84 D
          7 ZZ 22 S


          To sort the data based on the third column, you can pipe the output data to the command sort as follows:



          grep 'XX|YY|ZZ' file.txt | sort -k3,3n
          7 ZZ 22 S
          2 YY 34 y
          3 ZZ 44 N
          1 XX 45 N
          5 XX 45 N
          6 YY 84 D
          4 XX 89 Y






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 4 at 5:25









          Stephen Kitt

          149k23331397




          149k23331397










          answered Oct 4 at 4:29









          Goro

          7,24753168




          7,24753168











          • thank you. I have one question pelase how can i organize the new list based on the numbers in the third column
            – Shervan
            Oct 4 at 4:36










          • Please see my edits.
            – Goro
            Oct 4 at 4:39






          • 1




            Thank you so much @goro for the great help!
            – Shervan
            Oct 4 at 4:41










          • @Shervan note that the grep variant matches the values anywhere in the line, not just in the second field.
            – Stephen Kitt
            Oct 4 at 4:45






          • 1




            @Goro I was only wondering why you specified the starting character (.1), that’s all. Note that your sort applies to the rest of the line, starting with the third field; I think -k3,3n would be more appropriate.
            – Stephen Kitt
            Oct 4 at 5:12
















          • thank you. I have one question pelase how can i organize the new list based on the numbers in the third column
            – Shervan
            Oct 4 at 4:36










          • Please see my edits.
            – Goro
            Oct 4 at 4:39






          • 1




            Thank you so much @goro for the great help!
            – Shervan
            Oct 4 at 4:41










          • @Shervan note that the grep variant matches the values anywhere in the line, not just in the second field.
            – Stephen Kitt
            Oct 4 at 4:45






          • 1




            @Goro I was only wondering why you specified the starting character (.1), that’s all. Note that your sort applies to the rest of the line, starting with the third field; I think -k3,3n would be more appropriate.
            – Stephen Kitt
            Oct 4 at 5:12















          thank you. I have one question pelase how can i organize the new list based on the numbers in the third column
          – Shervan
          Oct 4 at 4:36




          thank you. I have one question pelase how can i organize the new list based on the numbers in the third column
          – Shervan
          Oct 4 at 4:36












          Please see my edits.
          – Goro
          Oct 4 at 4:39




          Please see my edits.
          – Goro
          Oct 4 at 4:39




          1




          1




          Thank you so much @goro for the great help!
          – Shervan
          Oct 4 at 4:41




          Thank you so much @goro for the great help!
          – Shervan
          Oct 4 at 4:41












          @Shervan note that the grep variant matches the values anywhere in the line, not just in the second field.
          – Stephen Kitt
          Oct 4 at 4:45




          @Shervan note that the grep variant matches the values anywhere in the line, not just in the second field.
          – Stephen Kitt
          Oct 4 at 4:45




          1




          1




          @Goro I was only wondering why you specified the starting character (.1), that’s all. Note that your sort applies to the rest of the line, starting with the third field; I think -k3,3n would be more appropriate.
          – Stephen Kitt
          Oct 4 at 5:12




          @Goro I was only wondering why you specified the starting character (.1), that’s all. Note that your sort applies to the rest of the line, starting with the third field; I think -k3,3n would be more appropriate.
          – Stephen Kitt
          Oct 4 at 5:12

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473133%2ffiltering-records-of-a-file-based-on-a-value-of-a-column%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay