How to use awk to extract data from a file based on the content of another file?How to use ^#$ as record separator in awk?awk : parse and write to another fileextract the data from 2 fileshow to change record or field separator of a fileHow to use Unindented line as the Record Separate in awk cliHow to EDIT only the last line (or any specific line number(s)) using awk command?Delete lines from one file if they contain a regex of content in another filemake awk print the line that match a variable and the next n lines and use a variable in awkUsing AWK To Extract Numbers From .CSV FileAwk: setting a record to a pattern match, then print only the last record

How long did it take Captain Marvel to travel to Earth?

Can I combine SELECT TOP() with the IN operator?

Does Thanos's ship land in the middle of the battlefield in "Avengers: Endgame"?

Can an Iranian citizen enter the USA on a Dutch passport?

Why doesn't a particle exert force on itself?

Why increasing of the temperature of the objects like wood, paper etc. doesn't fire them?

Can anyone identify this unknown 1988 PC card from The Palantir Corporation?

How important are good looking people in a novel/story?

Class Not Passing SObject By Reference

Python 3 - simple temperature program version 1.3

Where did Lovecraft write about Carcosa?

Given a safe domain, are subdirectories safe as well?

How can I finally understand the confusing modal verb "мочь"?

How to deal with employer who keeps me at work after working hours

How did the Apollo guidance computer handle parity bit errors?

Is it normal for gliders not to have attitude indicators?

Determine if a grid contains another grid

Problem with estimating a sequence with intuition

How can I obtain and work with a Platonic dodecahedron?

Convert Numbers To Emoji Math

Why is the blank symbol not considered part of the input alphabet of a Turing machine?

All of my Firefox add-ons been disabled suddenly, how can I re-enable them?

Copper as an adjective to refer to something made of copper

Picking a theme as a discovery writer



How to use awk to extract data from a file based on the content of another file?


How to use ^#$ as record separator in awk?awk : parse and write to another fileextract the data from 2 fileshow to change record or field separator of a fileHow to use Unindented line as the Record Separate in awk cliHow to EDIT only the last line (or any specific line number(s)) using awk command?Delete lines from one file if they contain a regex of content in another filemake awk print the line that match a variable and the next n lines and use a variable in awkUsing AWK To Extract Numbers From .CSV FileAwk: setting a record to a pattern match, then print only the last record






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








2















I have two files. One file includes structured data and be low is a sample.



article 1 title
article 1 body line 1
article 1 body line 2
+++
article 2 title
article 2 body line 1
article 2 body line 2
article 2 body line 3
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3


As you can see, +++ is the separator for records. For each record, the first line is the title, all other lines are the content of this record. Another file is a simple text file with a list of titles. For example:



article 1 title
article 3 title
article 4 title


What I want is the records with their title listed in the second file. So for the aforementioned example, the expected result is:



article 1 title
article 1 body line 1
article 1 body line 2
+++
article 3 title
article 3 body line 1
article 3 body line 2
+++
article 4 title
article 4 body line 1
article 4 body line 2
article 4 body line 3


I think awk could probably solve my problem but I don't know how.



What I've tried is this:



awk 'BEGINRS="(r?n)?+3(r?n)?"; FS="r?n"; ORS="+++" NR==FNRa[$0];next ...' title_list.txt data.txt


My problem is that the RS for the two files should be different and I don't know how to make it work.










share|improve this question




























    2















    I have two files. One file includes structured data and be low is a sample.



    article 1 title
    article 1 body line 1
    article 1 body line 2
    +++
    article 2 title
    article 2 body line 1
    article 2 body line 2
    article 2 body line 3
    +++
    article 3 title
    article 3 body line 1
    article 3 body line 2
    +++
    article 4 title
    article 4 body line 1
    article 4 body line 2
    article 4 body line 3


    As you can see, +++ is the separator for records. For each record, the first line is the title, all other lines are the content of this record. Another file is a simple text file with a list of titles. For example:



    article 1 title
    article 3 title
    article 4 title


    What I want is the records with their title listed in the second file. So for the aforementioned example, the expected result is:



    article 1 title
    article 1 body line 1
    article 1 body line 2
    +++
    article 3 title
    article 3 body line 1
    article 3 body line 2
    +++
    article 4 title
    article 4 body line 1
    article 4 body line 2
    article 4 body line 3


    I think awk could probably solve my problem but I don't know how.



    What I've tried is this:



    awk 'BEGINRS="(r?n)?+3(r?n)?"; FS="r?n"; ORS="+++" NR==FNRa[$0];next ...' title_list.txt data.txt


    My problem is that the RS for the two files should be different and I don't know how to make it work.










    share|improve this question
























      2












      2








      2








      I have two files. One file includes structured data and be low is a sample.



      article 1 title
      article 1 body line 1
      article 1 body line 2
      +++
      article 2 title
      article 2 body line 1
      article 2 body line 2
      article 2 body line 3
      +++
      article 3 title
      article 3 body line 1
      article 3 body line 2
      +++
      article 4 title
      article 4 body line 1
      article 4 body line 2
      article 4 body line 3


      As you can see, +++ is the separator for records. For each record, the first line is the title, all other lines are the content of this record. Another file is a simple text file with a list of titles. For example:



      article 1 title
      article 3 title
      article 4 title


      What I want is the records with their title listed in the second file. So for the aforementioned example, the expected result is:



      article 1 title
      article 1 body line 1
      article 1 body line 2
      +++
      article 3 title
      article 3 body line 1
      article 3 body line 2
      +++
      article 4 title
      article 4 body line 1
      article 4 body line 2
      article 4 body line 3


      I think awk could probably solve my problem but I don't know how.



      What I've tried is this:



      awk 'BEGINRS="(r?n)?+3(r?n)?"; FS="r?n"; ORS="+++" NR==FNRa[$0];next ...' title_list.txt data.txt


      My problem is that the RS for the two files should be different and I don't know how to make it work.










      share|improve this question














      I have two files. One file includes structured data and be low is a sample.



      article 1 title
      article 1 body line 1
      article 1 body line 2
      +++
      article 2 title
      article 2 body line 1
      article 2 body line 2
      article 2 body line 3
      +++
      article 3 title
      article 3 body line 1
      article 3 body line 2
      +++
      article 4 title
      article 4 body line 1
      article 4 body line 2
      article 4 body line 3


      As you can see, +++ is the separator for records. For each record, the first line is the title, all other lines are the content of this record. Another file is a simple text file with a list of titles. For example:



      article 1 title
      article 3 title
      article 4 title


      What I want is the records with their title listed in the second file. So for the aforementioned example, the expected result is:



      article 1 title
      article 1 body line 1
      article 1 body line 2
      +++
      article 3 title
      article 3 body line 1
      article 3 body line 2
      +++
      article 4 title
      article 4 body line 1
      article 4 body line 2
      article 4 body line 3


      I think awk could probably solve my problem but I don't know how.



      What I've tried is this:



      awk 'BEGINRS="(r?n)?+3(r?n)?"; FS="r?n"; ORS="+++" NR==FNRa[$0];next ...' title_list.txt data.txt


      My problem is that the RS for the two files should be different and I don't know how to make it work.







      text-processing awk






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 hours ago









      Ogrish ManOgrish Man

      5941516




      5941516




















          2 Answers
          2






          active

          oldest

          votes


















          2














          You can set variables like RS separately for each file. For example:



          $ awk 'NR==FNRa[$0];next $1 in a' RS='r?n' title_list.txt RS='+++r?n' FS='r?n' ORS='+++n' data.txt
          article 1 title
          article 1 body line 1
          article 1 body line 2
          +++
          article 3 title
          article 3 body line 1
          article 3 body line 2
          +++
          article 4 title
          article 4 body line 1
          article 4 body line 2
          article 4 body line 3
          +++





          share|improve this answer






























            1














            In gawk you can use special blocks BEGINFILE and ENDFILE to set whatever rules you need before/after reading new file, for example:



            $ awk 'NR==FNRa[$0]++;nextENDFILERS="+++n";FS="n"a[$1]printf $0RT' title_list.txt data.txt 

            article 1 title
            article 1 body line 1
            article 1 body line 2
            +++
            article 3 title
            article 3 body line 1
            article 3 body line 2
            +++
            article 4 title
            article 4 body line 1
            article 4 body line 2
            article 4 body line 3





            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%2f517265%2fhow-to-use-awk-to-extract-data-from-a-file-based-on-the-content-of-another-file%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              You can set variables like RS separately for each file. For example:



              $ awk 'NR==FNRa[$0];next $1 in a' RS='r?n' title_list.txt RS='+++r?n' FS='r?n' ORS='+++n' data.txt
              article 1 title
              article 1 body line 1
              article 1 body line 2
              +++
              article 3 title
              article 3 body line 1
              article 3 body line 2
              +++
              article 4 title
              article 4 body line 1
              article 4 body line 2
              article 4 body line 3
              +++





              share|improve this answer



























                2














                You can set variables like RS separately for each file. For example:



                $ awk 'NR==FNRa[$0];next $1 in a' RS='r?n' title_list.txt RS='+++r?n' FS='r?n' ORS='+++n' data.txt
                article 1 title
                article 1 body line 1
                article 1 body line 2
                +++
                article 3 title
                article 3 body line 1
                article 3 body line 2
                +++
                article 4 title
                article 4 body line 1
                article 4 body line 2
                article 4 body line 3
                +++





                share|improve this answer

























                  2












                  2








                  2







                  You can set variables like RS separately for each file. For example:



                  $ awk 'NR==FNRa[$0];next $1 in a' RS='r?n' title_list.txt RS='+++r?n' FS='r?n' ORS='+++n' data.txt
                  article 1 title
                  article 1 body line 1
                  article 1 body line 2
                  +++
                  article 3 title
                  article 3 body line 1
                  article 3 body line 2
                  +++
                  article 4 title
                  article 4 body line 1
                  article 4 body line 2
                  article 4 body line 3
                  +++





                  share|improve this answer













                  You can set variables like RS separately for each file. For example:



                  $ awk 'NR==FNRa[$0];next $1 in a' RS='r?n' title_list.txt RS='+++r?n' FS='r?n' ORS='+++n' data.txt
                  article 1 title
                  article 1 body line 1
                  article 1 body line 2
                  +++
                  article 3 title
                  article 3 body line 1
                  article 3 body line 2
                  +++
                  article 4 title
                  article 4 body line 1
                  article 4 body line 2
                  article 4 body line 3
                  +++






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 1 hour ago









                  John1024John1024

                  49k5114129




                  49k5114129























                      1














                      In gawk you can use special blocks BEGINFILE and ENDFILE to set whatever rules you need before/after reading new file, for example:



                      $ awk 'NR==FNRa[$0]++;nextENDFILERS="+++n";FS="n"a[$1]printf $0RT' title_list.txt data.txt 

                      article 1 title
                      article 1 body line 1
                      article 1 body line 2
                      +++
                      article 3 title
                      article 3 body line 1
                      article 3 body line 2
                      +++
                      article 4 title
                      article 4 body line 1
                      article 4 body line 2
                      article 4 body line 3





                      share|improve this answer



























                        1














                        In gawk you can use special blocks BEGINFILE and ENDFILE to set whatever rules you need before/after reading new file, for example:



                        $ awk 'NR==FNRa[$0]++;nextENDFILERS="+++n";FS="n"a[$1]printf $0RT' title_list.txt data.txt 

                        article 1 title
                        article 1 body line 1
                        article 1 body line 2
                        +++
                        article 3 title
                        article 3 body line 1
                        article 3 body line 2
                        +++
                        article 4 title
                        article 4 body line 1
                        article 4 body line 2
                        article 4 body line 3





                        share|improve this answer

























                          1












                          1








                          1







                          In gawk you can use special blocks BEGINFILE and ENDFILE to set whatever rules you need before/after reading new file, for example:



                          $ awk 'NR==FNRa[$0]++;nextENDFILERS="+++n";FS="n"a[$1]printf $0RT' title_list.txt data.txt 

                          article 1 title
                          article 1 body line 1
                          article 1 body line 2
                          +++
                          article 3 title
                          article 3 body line 1
                          article 3 body line 2
                          +++
                          article 4 title
                          article 4 body line 1
                          article 4 body line 2
                          article 4 body line 3





                          share|improve this answer













                          In gawk you can use special blocks BEGINFILE and ENDFILE to set whatever rules you need before/after reading new file, for example:



                          $ awk 'NR==FNRa[$0]++;nextENDFILERS="+++n";FS="n"a[$1]printf $0RT' title_list.txt data.txt 

                          article 1 title
                          article 1 body line 1
                          article 1 body line 2
                          +++
                          article 3 title
                          article 3 body line 1
                          article 3 body line 2
                          +++
                          article 4 title
                          article 4 body line 1
                          article 4 body line 2
                          article 4 body line 3






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 1 hour ago









                          jimmijjimmij

                          32.8k876111




                          32.8k876111



























                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f517265%2fhow-to-use-awk-to-extract-data-from-a-file-based-on-the-content-of-another-file%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

                              Siegen Nawigatsjuun

                              Log på Navigationsmenu

                              Log på Navigationsmenu