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

Do Rabbis admit emotional involvement in their rulings?

Why is there a cap on 401k contributions?

Why does the electron wavefunction not collapse within atoms at room temperature in gas, liquids or solids due to decoherence?

Was the Highlands Ranch shooting the 115th mass shooting in the US in 2019

A Latin text with dependency tree

Is it a good idea to copy a trader when investing?

Has everyone forgotten about wildfire?

how to find out if there's files in a folder and exit accordingly (in KSH)

Why Faces eat each other?

Is it a Munchausen Number?

Can a planet still function with a damaged moon?

Row vectors and column vectors (Mathematica vs Matlab)

Lorentz invariance of Maxwell's equations in matter

Do Monks gain the 9th level Unarmored Movement benefit when wearing armor or using a shield?

How can I test a shell script in a "safe environment" to avoid harm to my computer?

Examples where existence is harder than evaluation

I might have messed up in the 'Future Work' section of my thesis

What is the Ancient One's mistake?

Names of the Six Tastes

Are wands in any sort of book going to be too much like Harry Potter?

resoldering copper waste pipe

How do carbureted and fuel injected engines compare in high altitude?

Are double contractions formal? Eg: "couldn't've" for "could not have"

What replaces x86 intrinsics for C when Apple ditches Intel CPUs for their own chips?



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;








4















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




























    4















    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
























      4












      4








      4








      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 May 5 at 20:00









      Ogrish ManOgrish Man

      6141616




      6141616




















          3 Answers
          3






          active

          oldest

          votes


















          4














          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






























            3














            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






























              0














              Done by below method using combination of sed and awk



              command



               k=`awk 'print NR' file2| sed -n '$p'`

              for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done


              output

              for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done
              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

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                4














                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



























                  4














                  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

























                    4












                    4








                    4







                    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 May 5 at 21:26









                    John1024John1024

                    49.1k5114129




                    49.1k5114129























                        3














                        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



























                          3














                          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

























                            3












                            3








                            3







                            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 May 5 at 21:36









                            jimmijjimmij

                            32.9k877112




                            32.9k877112





















                                0














                                Done by below method using combination of sed and awk



                                command



                                 k=`awk 'print NR' file2| sed -n '$p'`

                                for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done


                                output

                                for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done
                                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



























                                  0














                                  Done by below method using combination of sed and awk



                                  command



                                   k=`awk 'print NR' file2| sed -n '$p'`

                                  for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done


                                  output

                                  for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done
                                  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

























                                    0












                                    0








                                    0







                                    Done by below method using combination of sed and awk



                                    command



                                     k=`awk 'print NR' file2| sed -n '$p'`

                                    for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done


                                    output

                                    for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done
                                    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













                                    Done by below method using combination of sed and awk



                                    command



                                     k=`awk 'print NR' file2| sed -n '$p'`

                                    for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done


                                    output

                                    for ((i=1;i<=$k;i++)); do j=`awk -v i="$i" 'NR==iprint $0' file2`; sed -n "/$j/,/+++/p" file1; done
                                    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 2 days ago









                                    Praveen Kumar BSPraveen Kumar BS

                                    1,8532311




                                    1,8532311



























                                        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

                                        Grendel Contents Story Scholarship Depictions Notes References Navigation menu10.1093/notesj/gjn112Berserkeree

                                        Area configuration aggregation error after install Porto themeMagento 2.1 CE Installed but front/backend not loading/workingCSS not loading on page within Magento 2 pageCannot install module in Magento 2no commands defined in the “setup” namespace. in Magento2Magento 2: Static files are present but shows 404Why do i have to always run the commands to clean cache in Magento 2.1.8?Failure reason: 'Unable to unserialize value.'Error 500 after magento migrationIn production mode the site does not loadMagento 2 : Error 500 after installing

                                        Middle Expansion Olielle Resaix Definition: Uttering songs of triumph shouting with joy triumphant exulting Sejunction Journal 붙다 달 고급 품목 외출 The stretch trades the screeching tin. Definition: The act of speaking with a drawl a drawl Cough Sand Definition: An uproar a quarrel a noisy outbreak Shake Iron Publicize Horse House Baby 사과 Resaix Flaggy Jelly Temporary Unequaled Puppet A drop in the bucket Shrew 성격 회원 성질 미팅 The burn frames the tacky quality. Materialistic The smoke reduces the way. Yammoe Nondescript Cheek 얼굴 배 약하다 날리다 타다 The illegal country shows the iron. Help Rule Drearien Smoke Teaching Meaty Wasp Abraham Lincoln Jaws 진심 수리하다 Size Cork Idea Convert Think Lark John Lennon 거울 청소 군 추천하다 아이스크림