Linux: how to extract rows of certain IDs and the IDs are writtein in another files?

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












2














I have a csv file of IDs.



1
3
8


I have another file of information, not only for the IDs, but also others. If I want to extract the information only of the IDs, how to do it?



1 96 283 4
2 87 918 6
2 49 208 5
3 57 294 0
3 24 284 0
4 28 345 290
5 23 234 209
6 98 245 02
7 18 329 89
8 19 239 78
8 18 289 90
9 28 390 09
9 19 238 09
10 23 899 7


I want to make this file



1 96 283 4
3 57 294 0
3 24 284 0
8 19 239 78
8 18 289 90









share|improve this question























  • If it wasn't for numbers, the join command (join -j 1 id.csv informations.csv) would have done it. As it is more tinkering or other tools might be needed, because I don't find a way to have it sort numerically
    – A.B
    Dec 24 '18 at 16:48
















2














I have a csv file of IDs.



1
3
8


I have another file of information, not only for the IDs, but also others. If I want to extract the information only of the IDs, how to do it?



1 96 283 4
2 87 918 6
2 49 208 5
3 57 294 0
3 24 284 0
4 28 345 290
5 23 234 209
6 98 245 02
7 18 329 89
8 19 239 78
8 18 289 90
9 28 390 09
9 19 238 09
10 23 899 7


I want to make this file



1 96 283 4
3 57 294 0
3 24 284 0
8 19 239 78
8 18 289 90









share|improve this question























  • If it wasn't for numbers, the join command (join -j 1 id.csv informations.csv) would have done it. As it is more tinkering or other tools might be needed, because I don't find a way to have it sort numerically
    – A.B
    Dec 24 '18 at 16:48














2












2








2


1





I have a csv file of IDs.



1
3
8


I have another file of information, not only for the IDs, but also others. If I want to extract the information only of the IDs, how to do it?



1 96 283 4
2 87 918 6
2 49 208 5
3 57 294 0
3 24 284 0
4 28 345 290
5 23 234 209
6 98 245 02
7 18 329 89
8 19 239 78
8 18 289 90
9 28 390 09
9 19 238 09
10 23 899 7


I want to make this file



1 96 283 4
3 57 294 0
3 24 284 0
8 19 239 78
8 18 289 90









share|improve this question















I have a csv file of IDs.



1
3
8


I have another file of information, not only for the IDs, but also others. If I want to extract the information only of the IDs, how to do it?



1 96 283 4
2 87 918 6
2 49 208 5
3 57 294 0
3 24 284 0
4 28 345 290
5 23 234 209
6 98 245 02
7 18 329 89
8 19 239 78
8 18 289 90
9 28 390 09
9 19 238 09
10 23 899 7


I want to make this file



1 96 283 4
3 57 294 0
3 24 284 0
8 19 239 78
8 18 289 90






linux text-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 26 '18 at 23:15









jimmij

30.9k870105




30.9k870105










asked Dec 24 '18 at 4:22









user10345633

111




111











  • If it wasn't for numbers, the join command (join -j 1 id.csv informations.csv) would have done it. As it is more tinkering or other tools might be needed, because I don't find a way to have it sort numerically
    – A.B
    Dec 24 '18 at 16:48

















  • If it wasn't for numbers, the join command (join -j 1 id.csv informations.csv) would have done it. As it is more tinkering or other tools might be needed, because I don't find a way to have it sort numerically
    – A.B
    Dec 24 '18 at 16:48
















If it wasn't for numbers, the join command (join -j 1 id.csv informations.csv) would have done it. As it is more tinkering or other tools might be needed, because I don't find a way to have it sort numerically
– A.B
Dec 24 '18 at 16:48





If it wasn't for numbers, the join command (join -j 1 id.csv informations.csv) would have done it. As it is more tinkering or other tools might be needed, because I don't find a way to have it sort numerically
– A.B
Dec 24 '18 at 16:48











4 Answers
4






active

oldest

votes


















1














If the files are space-delimited and have Unix-style line endings:



cat file1.txt | xargs -I '' -n 1 grep '^ ' file2.txt





share|improve this answer


















  • 1




    With 4 space before your command you can activate the code block feature which is better readable.
    – ploth
    Dec 24 '18 at 11:58


















0














If you can homogenize the first field in each file (eg by adding a leading '0'), you can use join(1):



f1:



01
03
08


f2:



01 96 283 4
02 87 918 6
02 49 208 5
03 57 294 0
03 24 284 0
04 28 345 290
05 23 234 209
06 98 245 02
07 18 329 89
08 19 239 78
08 18 289 90
09 28 390 09
09 19 238 09
10 23 899 7


Then:



$ join f f2
01 96 283 4
03 57 294 0
03 24 284 0
08 19 239 78
08 18 289 90





share|improve this answer




















  • Thank you so much for your prompt reply. I tried with join ids.txt table.txt > output.txt, but it does not work well. Any ideas? Thank you!
    – user10345633
    Dec 24 '18 at 5:24


















0














I would use awk:



awk 'NR==FNRa[$0]++;nexta[$1]' file1 file2


where your IDs are in file1 and the rest in file2. The output:



1 96 283 4
3 57 294 0
3 24 284 0
8 19 239 78
8 18 289 90





share|improve this answer




























    0














    using Miller you could do in this way



    mlr --implicit-csv-header --headerless-csv-output --ifs " " --csv join -j 1 -r 1 -f joinInputOne.csv joinInputTwo.csv


    to obtain




    1,96,283,4
    3,57,294,0
    3,24,284,0
    8,19,239,78
    8,18,289,90



    joinInputOne.csv is




    1
    3
    8



    and



    joinInputTwo.csv is




    1 96 283 4
    2 87 918 6
    2 49 208 5
    3 57 294 0
    3 24 284 0
    4 28 345 290
    5 23 234 209
    6 98 245 02
    7 18 329 89
    8 19 239 78
    8 18 289 90
    9 28 390 09
    9 19 238 09
    10 23 899 7






    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
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f490711%2flinux-how-to-extract-rows-of-certain-ids-and-the-ids-are-writtein-in-another-fi%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      If the files are space-delimited and have Unix-style line endings:



      cat file1.txt | xargs -I '' -n 1 grep '^ ' file2.txt





      share|improve this answer


















      • 1




        With 4 space before your command you can activate the code block feature which is better readable.
        – ploth
        Dec 24 '18 at 11:58















      1














      If the files are space-delimited and have Unix-style line endings:



      cat file1.txt | xargs -I '' -n 1 grep '^ ' file2.txt





      share|improve this answer


















      • 1




        With 4 space before your command you can activate the code block feature which is better readable.
        – ploth
        Dec 24 '18 at 11:58













      1












      1








      1






      If the files are space-delimited and have Unix-style line endings:



      cat file1.txt | xargs -I '' -n 1 grep '^ ' file2.txt





      share|improve this answer














      If the files are space-delimited and have Unix-style line endings:



      cat file1.txt | xargs -I '' -n 1 grep '^ ' file2.txt






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Dec 24 '18 at 12:13









      steve

      13.9k22452




      13.9k22452










      answered Dec 24 '18 at 11:54









      artem

      111




      111







      • 1




        With 4 space before your command you can activate the code block feature which is better readable.
        – ploth
        Dec 24 '18 at 11:58












      • 1




        With 4 space before your command you can activate the code block feature which is better readable.
        – ploth
        Dec 24 '18 at 11:58







      1




      1




      With 4 space before your command you can activate the code block feature which is better readable.
      – ploth
      Dec 24 '18 at 11:58




      With 4 space before your command you can activate the code block feature which is better readable.
      – ploth
      Dec 24 '18 at 11:58













      0














      If you can homogenize the first field in each file (eg by adding a leading '0'), you can use join(1):



      f1:



      01
      03
      08


      f2:



      01 96 283 4
      02 87 918 6
      02 49 208 5
      03 57 294 0
      03 24 284 0
      04 28 345 290
      05 23 234 209
      06 98 245 02
      07 18 329 89
      08 19 239 78
      08 18 289 90
      09 28 390 09
      09 19 238 09
      10 23 899 7


      Then:



      $ join f f2
      01 96 283 4
      03 57 294 0
      03 24 284 0
      08 19 239 78
      08 18 289 90





      share|improve this answer




















      • Thank you so much for your prompt reply. I tried with join ids.txt table.txt > output.txt, but it does not work well. Any ideas? Thank you!
        – user10345633
        Dec 24 '18 at 5:24















      0














      If you can homogenize the first field in each file (eg by adding a leading '0'), you can use join(1):



      f1:



      01
      03
      08


      f2:



      01 96 283 4
      02 87 918 6
      02 49 208 5
      03 57 294 0
      03 24 284 0
      04 28 345 290
      05 23 234 209
      06 98 245 02
      07 18 329 89
      08 19 239 78
      08 18 289 90
      09 28 390 09
      09 19 238 09
      10 23 899 7


      Then:



      $ join f f2
      01 96 283 4
      03 57 294 0
      03 24 284 0
      08 19 239 78
      08 18 289 90





      share|improve this answer




















      • Thank you so much for your prompt reply. I tried with join ids.txt table.txt > output.txt, but it does not work well. Any ideas? Thank you!
        – user10345633
        Dec 24 '18 at 5:24













      0












      0








      0






      If you can homogenize the first field in each file (eg by adding a leading '0'), you can use join(1):



      f1:



      01
      03
      08


      f2:



      01 96 283 4
      02 87 918 6
      02 49 208 5
      03 57 294 0
      03 24 284 0
      04 28 345 290
      05 23 234 209
      06 98 245 02
      07 18 329 89
      08 19 239 78
      08 18 289 90
      09 28 390 09
      09 19 238 09
      10 23 899 7


      Then:



      $ join f f2
      01 96 283 4
      03 57 294 0
      03 24 284 0
      08 19 239 78
      08 18 289 90





      share|improve this answer












      If you can homogenize the first field in each file (eg by adding a leading '0'), you can use join(1):



      f1:



      01
      03
      08


      f2:



      01 96 283 4
      02 87 918 6
      02 49 208 5
      03 57 294 0
      03 24 284 0
      04 28 345 290
      05 23 234 209
      06 98 245 02
      07 18 329 89
      08 19 239 78
      08 18 289 90
      09 28 390 09
      09 19 238 09
      10 23 899 7


      Then:



      $ join f f2
      01 96 283 4
      03 57 294 0
      03 24 284 0
      08 19 239 78
      08 18 289 90






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Dec 24 '18 at 4:37









      wef

      993




      993











      • Thank you so much for your prompt reply. I tried with join ids.txt table.txt > output.txt, but it does not work well. Any ideas? Thank you!
        – user10345633
        Dec 24 '18 at 5:24
















      • Thank you so much for your prompt reply. I tried with join ids.txt table.txt > output.txt, but it does not work well. Any ideas? Thank you!
        – user10345633
        Dec 24 '18 at 5:24















      Thank you so much for your prompt reply. I tried with join ids.txt table.txt > output.txt, but it does not work well. Any ideas? Thank you!
      – user10345633
      Dec 24 '18 at 5:24




      Thank you so much for your prompt reply. I tried with join ids.txt table.txt > output.txt, but it does not work well. Any ideas? Thank you!
      – user10345633
      Dec 24 '18 at 5:24











      0














      I would use awk:



      awk 'NR==FNRa[$0]++;nexta[$1]' file1 file2


      where your IDs are in file1 and the rest in file2. The output:



      1 96 283 4
      3 57 294 0
      3 24 284 0
      8 19 239 78
      8 18 289 90





      share|improve this answer

























        0














        I would use awk:



        awk 'NR==FNRa[$0]++;nexta[$1]' file1 file2


        where your IDs are in file1 and the rest in file2. The output:



        1 96 283 4
        3 57 294 0
        3 24 284 0
        8 19 239 78
        8 18 289 90





        share|improve this answer























          0












          0








          0






          I would use awk:



          awk 'NR==FNRa[$0]++;nexta[$1]' file1 file2


          where your IDs are in file1 and the rest in file2. The output:



          1 96 283 4
          3 57 294 0
          3 24 284 0
          8 19 239 78
          8 18 289 90





          share|improve this answer












          I would use awk:



          awk 'NR==FNRa[$0]++;nexta[$1]' file1 file2


          where your IDs are in file1 and the rest in file2. The output:



          1 96 283 4
          3 57 294 0
          3 24 284 0
          8 19 239 78
          8 18 289 90






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 26 '18 at 23:14









          jimmij

          30.9k870105




          30.9k870105





















              0














              using Miller you could do in this way



              mlr --implicit-csv-header --headerless-csv-output --ifs " " --csv join -j 1 -r 1 -f joinInputOne.csv joinInputTwo.csv


              to obtain




              1,96,283,4
              3,57,294,0
              3,24,284,0
              8,19,239,78
              8,18,289,90



              joinInputOne.csv is




              1
              3
              8



              and



              joinInputTwo.csv is




              1 96 283 4
              2 87 918 6
              2 49 208 5
              3 57 294 0
              3 24 284 0
              4 28 345 290
              5 23 234 209
              6 98 245 02
              7 18 329 89
              8 19 239 78
              8 18 289 90
              9 28 390 09
              9 19 238 09
              10 23 899 7






              share|improve this answer

























                0














                using Miller you could do in this way



                mlr --implicit-csv-header --headerless-csv-output --ifs " " --csv join -j 1 -r 1 -f joinInputOne.csv joinInputTwo.csv


                to obtain




                1,96,283,4
                3,57,294,0
                3,24,284,0
                8,19,239,78
                8,18,289,90



                joinInputOne.csv is




                1
                3
                8



                and



                joinInputTwo.csv is




                1 96 283 4
                2 87 918 6
                2 49 208 5
                3 57 294 0
                3 24 284 0
                4 28 345 290
                5 23 234 209
                6 98 245 02
                7 18 329 89
                8 19 239 78
                8 18 289 90
                9 28 390 09
                9 19 238 09
                10 23 899 7






                share|improve this answer























                  0












                  0








                  0






                  using Miller you could do in this way



                  mlr --implicit-csv-header --headerless-csv-output --ifs " " --csv join -j 1 -r 1 -f joinInputOne.csv joinInputTwo.csv


                  to obtain




                  1,96,283,4
                  3,57,294,0
                  3,24,284,0
                  8,19,239,78
                  8,18,289,90



                  joinInputOne.csv is




                  1
                  3
                  8



                  and



                  joinInputTwo.csv is




                  1 96 283 4
                  2 87 918 6
                  2 49 208 5
                  3 57 294 0
                  3 24 284 0
                  4 28 345 290
                  5 23 234 209
                  6 98 245 02
                  7 18 329 89
                  8 19 239 78
                  8 18 289 90
                  9 28 390 09
                  9 19 238 09
                  10 23 899 7






                  share|improve this answer












                  using Miller you could do in this way



                  mlr --implicit-csv-header --headerless-csv-output --ifs " " --csv join -j 1 -r 1 -f joinInputOne.csv joinInputTwo.csv


                  to obtain




                  1,96,283,4
                  3,57,294,0
                  3,24,284,0
                  8,19,239,78
                  8,18,289,90



                  joinInputOne.csv is




                  1
                  3
                  8



                  and



                  joinInputTwo.csv is




                  1 96 283 4
                  2 87 918 6
                  2 49 208 5
                  3 57 294 0
                  3 24 284 0
                  4 28 345 290
                  5 23 234 209
                  6 98 245 02
                  7 18 329 89
                  8 19 239 78
                  8 18 289 90
                  9 28 390 09
                  9 19 238 09
                  10 23 899 7







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 31 '18 at 8:23









                  aborruso

                  1466




                  1466



























                      draft saved

                      draft discarded
















































                      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.





                      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%2funix.stackexchange.com%2fquestions%2f490711%2flinux-how-to-extract-rows-of-certain-ids-and-the-ids-are-writtein-in-another-fi%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 check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?