Merge 2nd line of txt files togetherHow do I parse command line arguments in Bash?How do I tell if a regular file does not exist in Bash?How to count all the lines of code in a directory recursively?Linux: Merging multiple files, each on a new lineHow to merge two files line by line in BashHow to set a variable to the output of a command in Bash?Concatenate multiple files but include filename as section headersHow to merge every two lines into one from the command line?Read a file line by line assigning the value to a variableMerge two files using awk in linux

How much theory knowledge is actually used while playing?

How to convince somebody that he is fit for something else, but not this job?

Why do ¬, ∀ and ∃ have the same precedence?

Is there a nicer/politer/more positive alternative for "negates"?

"It doesn't matter" or "it won't matter"?

What kind of floor tile is this?

Giving feedback to someone without sounding prejudiced

Does the reader need to like the PoV character?

What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?

How do I tell my boss that I'm quitting soon, especially given that a colleague just left this week

Doesn't the system of the Supreme Court oppose justice?

PTIJ: Why is Haman obsessed with Bose?

Which was the first story featuring espers?

Do we have to expect a queue for the shuttle from Watford Junction to Harry Potter Studio?

The IT department bottlenecks progress, how should I handle this?

What fields between the rationals and the reals allow a good notion of 2D distance?

Change the color of a single dot in `ddot` symbol

How would you translate "more" for use as an interface button?

What features enable the Su-25 Frogfoot to operate with such a wide variety of fuels?

Is my low blitz game drawing rate at www.chess.com an indicator that I am weak in chess?

What's the name of the logical fallacy where a debater extends a statement far beyond the original statement to make it true?

Does the Linux kernel need a file system to run?

Why does this expression simplify as such?

What does Apple's new App Store requirement mean



Merge 2nd line of txt files together


How do I parse command line arguments in Bash?How do I tell if a regular file does not exist in Bash?How to count all the lines of code in a directory recursively?Linux: Merging multiple files, each on a new lineHow to merge two files line by line in BashHow to set a variable to the output of a command in Bash?Concatenate multiple files but include filename as section headersHow to merge every two lines into one from the command line?Read a file line by line assigning the value to a variableMerge two files using awk in linux













1















I have a few hundred txt files with 2 lines each.



To merge them, I would generally do:



cat *.txt > final.txt


however, I only need to do that for 2nd line of each file so the final output is like



2nd line of 1st file
2nd line of 2nd file
2nd line of 3rd file
(and so on..)


Any ideas how I can accomplish this?










share|improve this question



















  • 2





    awk 'NR==2' file will print the 2nd line of the file

    – PS.
    Mar 7 at 19:09















1















I have a few hundred txt files with 2 lines each.



To merge them, I would generally do:



cat *.txt > final.txt


however, I only need to do that for 2nd line of each file so the final output is like



2nd line of 1st file
2nd line of 2nd file
2nd line of 3rd file
(and so on..)


Any ideas how I can accomplish this?










share|improve this question



















  • 2





    awk 'NR==2' file will print the 2nd line of the file

    – PS.
    Mar 7 at 19:09













1












1








1


1






I have a few hundred txt files with 2 lines each.



To merge them, I would generally do:



cat *.txt > final.txt


however, I only need to do that for 2nd line of each file so the final output is like



2nd line of 1st file
2nd line of 2nd file
2nd line of 3rd file
(and so on..)


Any ideas how I can accomplish this?










share|improve this question
















I have a few hundred txt files with 2 lines each.



To merge them, I would generally do:



cat *.txt > final.txt


however, I only need to do that for 2nd line of each file so the final output is like



2nd line of 1st file
2nd line of 2nd file
2nd line of 3rd file
(and so on..)


Any ideas how I can accomplish this?







bash awk cat






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 23:47









RavinderSingh13

30.2k41639




30.2k41639










asked Mar 7 at 19:06









Allison64Allison64

82




82







  • 2





    awk 'NR==2' file will print the 2nd line of the file

    – PS.
    Mar 7 at 19:09












  • 2





    awk 'NR==2' file will print the 2nd line of the file

    – PS.
    Mar 7 at 19:09







2




2





awk 'NR==2' file will print the 2nd line of the file

– PS.
Mar 7 at 19:09





awk 'NR==2' file will print the 2nd line of the file

– PS.
Mar 7 at 19:09












5 Answers
5






active

oldest

votes


















1














1st solution: Could you please try following with GNU awk. nextfile is very nice option in GNU awk which will skip all lines in current Input_file when a condition is MET.



awk 'FNR==2print;nextfile' *.txt > output_file




2nd solution: In case you don't have GNU awk try. Here since we are assuming there is NO nextfile in awk so I am creating a flag on each file's 2nd line and when it is TRUE simply going next line/skipping them and trying to save sometime. NOTE that this flag value will be RESET on each file's first line too.



awk 'FNR==1flag="" FNR==2print;flag=1 flagnext' *.txt > output_file




3rd solution: Adding while and find approach too here with head and tail. AFAIK head and tail shouldn't read whole file.



while read line
do
head -n +2 "$line" | tail -1
done < <(find -type f -name "*.txt") > "output_file"





share|improve this answer
































    1














    With GNU sed:



    sed -n -s 2p *.txt > final.txt


    or



    sed -s '2!d' *.txt > final.txt


    From man sed:




    -s: consider files as separate rather than as a single continuous long stream.







    share|improve this answer




















    • 1





      -s added to notes :)

      – PS.
      Mar 7 at 19:24


















    0














    find . -name "*.txt" -type f -exec awk 'NR==2' ;





    share|improve this answer






























      0














      Use awk



      awk 'FNR == 2 print; nextfile ' *.txt > final.txt


      FNR contains the line number within the current file. When it's on line 2, it will print the line and then go to the next file.






      share|improve this answer























      • Hi Barmar sir, since you are an expert of bash would like to request you to please check my 3rd(while+find) answer if that will good in terms of speed etc, kindly provide your views on same.

        – RavinderSingh13
        Mar 7 at 19:29






      • 1





        It's acceptable, just very verbose and runs lots of processes.

        – Barmar
        Mar 7 at 19:31











      • Thank you for your view, appreciate it.

        – RavinderSingh13
        Mar 7 at 19:33


















      0














      Using Perl



       perl -ne ' if($.==2) print ; close(ARGV) ' *.txt


      with sample files



      $ cat allison1.txt
      line1 in file1
      line2 in file1

      $ cat allison2.txt
      line1 in file2
      line2 in file2

      $ cat allison3.txt
      line1 in file3
      line2 in file3

      $ perl -ne ' if($.==2) print ; close(ARGV) ' allison*txt
      line2 in file1
      line2 in file2
      line2 in file3

      $





      share|improve this answer






















        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        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: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        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%2fstackoverflow.com%2fquestions%2f55051127%2fmerge-2nd-line-of-txt-files-together%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        1st solution: Could you please try following with GNU awk. nextfile is very nice option in GNU awk which will skip all lines in current Input_file when a condition is MET.



        awk 'FNR==2print;nextfile' *.txt > output_file




        2nd solution: In case you don't have GNU awk try. Here since we are assuming there is NO nextfile in awk so I am creating a flag on each file's 2nd line and when it is TRUE simply going next line/skipping them and trying to save sometime. NOTE that this flag value will be RESET on each file's first line too.



        awk 'FNR==1flag="" FNR==2print;flag=1 flagnext' *.txt > output_file




        3rd solution: Adding while and find approach too here with head and tail. AFAIK head and tail shouldn't read whole file.



        while read line
        do
        head -n +2 "$line" | tail -1
        done < <(find -type f -name "*.txt") > "output_file"





        share|improve this answer





























          1














          1st solution: Could you please try following with GNU awk. nextfile is very nice option in GNU awk which will skip all lines in current Input_file when a condition is MET.



          awk 'FNR==2print;nextfile' *.txt > output_file




          2nd solution: In case you don't have GNU awk try. Here since we are assuming there is NO nextfile in awk so I am creating a flag on each file's 2nd line and when it is TRUE simply going next line/skipping them and trying to save sometime. NOTE that this flag value will be RESET on each file's first line too.



          awk 'FNR==1flag="" FNR==2print;flag=1 flagnext' *.txt > output_file




          3rd solution: Adding while and find approach too here with head and tail. AFAIK head and tail shouldn't read whole file.



          while read line
          do
          head -n +2 "$line" | tail -1
          done < <(find -type f -name "*.txt") > "output_file"





          share|improve this answer



























            1












            1








            1







            1st solution: Could you please try following with GNU awk. nextfile is very nice option in GNU awk which will skip all lines in current Input_file when a condition is MET.



            awk 'FNR==2print;nextfile' *.txt > output_file




            2nd solution: In case you don't have GNU awk try. Here since we are assuming there is NO nextfile in awk so I am creating a flag on each file's 2nd line and when it is TRUE simply going next line/skipping them and trying to save sometime. NOTE that this flag value will be RESET on each file's first line too.



            awk 'FNR==1flag="" FNR==2print;flag=1 flagnext' *.txt > output_file




            3rd solution: Adding while and find approach too here with head and tail. AFAIK head and tail shouldn't read whole file.



            while read line
            do
            head -n +2 "$line" | tail -1
            done < <(find -type f -name "*.txt") > "output_file"





            share|improve this answer















            1st solution: Could you please try following with GNU awk. nextfile is very nice option in GNU awk which will skip all lines in current Input_file when a condition is MET.



            awk 'FNR==2print;nextfile' *.txt > output_file




            2nd solution: In case you don't have GNU awk try. Here since we are assuming there is NO nextfile in awk so I am creating a flag on each file's 2nd line and when it is TRUE simply going next line/skipping them and trying to save sometime. NOTE that this flag value will be RESET on each file's first line too.



            awk 'FNR==1flag="" FNR==2print;flag=1 flagnext' *.txt > output_file




            3rd solution: Adding while and find approach too here with head and tail. AFAIK head and tail shouldn't read whole file.



            while read line
            do
            head -n +2 "$line" | tail -1
            done < <(find -type f -name "*.txt") > "output_file"






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 7 at 19:39

























            answered Mar 7 at 19:14









            RavinderSingh13RavinderSingh13

            30.2k41639




            30.2k41639























                1














                With GNU sed:



                sed -n -s 2p *.txt > final.txt


                or



                sed -s '2!d' *.txt > final.txt


                From man sed:




                -s: consider files as separate rather than as a single continuous long stream.







                share|improve this answer




















                • 1





                  -s added to notes :)

                  – PS.
                  Mar 7 at 19:24















                1














                With GNU sed:



                sed -n -s 2p *.txt > final.txt


                or



                sed -s '2!d' *.txt > final.txt


                From man sed:




                -s: consider files as separate rather than as a single continuous long stream.







                share|improve this answer




















                • 1





                  -s added to notes :)

                  – PS.
                  Mar 7 at 19:24













                1












                1








                1







                With GNU sed:



                sed -n -s 2p *.txt > final.txt


                or



                sed -s '2!d' *.txt > final.txt


                From man sed:




                -s: consider files as separate rather than as a single continuous long stream.







                share|improve this answer















                With GNU sed:



                sed -n -s 2p *.txt > final.txt


                or



                sed -s '2!d' *.txt > final.txt


                From man sed:




                -s: consider files as separate rather than as a single continuous long stream.








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 7 at 19:25

























                answered Mar 7 at 19:23









                CyrusCyrus

                46.7k43880




                46.7k43880







                • 1





                  -s added to notes :)

                  – PS.
                  Mar 7 at 19:24












                • 1





                  -s added to notes :)

                  – PS.
                  Mar 7 at 19:24







                1




                1





                -s added to notes :)

                – PS.
                Mar 7 at 19:24





                -s added to notes :)

                – PS.
                Mar 7 at 19:24











                0














                find . -name "*.txt" -type f -exec awk 'NR==2' ;





                share|improve this answer



























                  0














                  find . -name "*.txt" -type f -exec awk 'NR==2' ;





                  share|improve this answer

























                    0












                    0








                    0







                    find . -name "*.txt" -type f -exec awk 'NR==2' ;





                    share|improve this answer













                    find . -name "*.txt" -type f -exec awk 'NR==2' ;






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 7 at 19:12









                    PS.PS.

                    7,577828




                    7,577828





















                        0














                        Use awk



                        awk 'FNR == 2 print; nextfile ' *.txt > final.txt


                        FNR contains the line number within the current file. When it's on line 2, it will print the line and then go to the next file.






                        share|improve this answer























                        • Hi Barmar sir, since you are an expert of bash would like to request you to please check my 3rd(while+find) answer if that will good in terms of speed etc, kindly provide your views on same.

                          – RavinderSingh13
                          Mar 7 at 19:29






                        • 1





                          It's acceptable, just very verbose and runs lots of processes.

                          – Barmar
                          Mar 7 at 19:31











                        • Thank you for your view, appreciate it.

                          – RavinderSingh13
                          Mar 7 at 19:33















                        0














                        Use awk



                        awk 'FNR == 2 print; nextfile ' *.txt > final.txt


                        FNR contains the line number within the current file. When it's on line 2, it will print the line and then go to the next file.






                        share|improve this answer























                        • Hi Barmar sir, since you are an expert of bash would like to request you to please check my 3rd(while+find) answer if that will good in terms of speed etc, kindly provide your views on same.

                          – RavinderSingh13
                          Mar 7 at 19:29






                        • 1





                          It's acceptable, just very verbose and runs lots of processes.

                          – Barmar
                          Mar 7 at 19:31











                        • Thank you for your view, appreciate it.

                          – RavinderSingh13
                          Mar 7 at 19:33













                        0












                        0








                        0







                        Use awk



                        awk 'FNR == 2 print; nextfile ' *.txt > final.txt


                        FNR contains the line number within the current file. When it's on line 2, it will print the line and then go to the next file.






                        share|improve this answer













                        Use awk



                        awk 'FNR == 2 print; nextfile ' *.txt > final.txt


                        FNR contains the line number within the current file. When it's on line 2, it will print the line and then go to the next file.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Mar 7 at 19:12









                        BarmarBarmar

                        433k36257359




                        433k36257359












                        • Hi Barmar sir, since you are an expert of bash would like to request you to please check my 3rd(while+find) answer if that will good in terms of speed etc, kindly provide your views on same.

                          – RavinderSingh13
                          Mar 7 at 19:29






                        • 1





                          It's acceptable, just very verbose and runs lots of processes.

                          – Barmar
                          Mar 7 at 19:31











                        • Thank you for your view, appreciate it.

                          – RavinderSingh13
                          Mar 7 at 19:33

















                        • Hi Barmar sir, since you are an expert of bash would like to request you to please check my 3rd(while+find) answer if that will good in terms of speed etc, kindly provide your views on same.

                          – RavinderSingh13
                          Mar 7 at 19:29






                        • 1





                          It's acceptable, just very verbose and runs lots of processes.

                          – Barmar
                          Mar 7 at 19:31











                        • Thank you for your view, appreciate it.

                          – RavinderSingh13
                          Mar 7 at 19:33
















                        Hi Barmar sir, since you are an expert of bash would like to request you to please check my 3rd(while+find) answer if that will good in terms of speed etc, kindly provide your views on same.

                        – RavinderSingh13
                        Mar 7 at 19:29





                        Hi Barmar sir, since you are an expert of bash would like to request you to please check my 3rd(while+find) answer if that will good in terms of speed etc, kindly provide your views on same.

                        – RavinderSingh13
                        Mar 7 at 19:29




                        1




                        1





                        It's acceptable, just very verbose and runs lots of processes.

                        – Barmar
                        Mar 7 at 19:31





                        It's acceptable, just very verbose and runs lots of processes.

                        – Barmar
                        Mar 7 at 19:31













                        Thank you for your view, appreciate it.

                        – RavinderSingh13
                        Mar 7 at 19:33





                        Thank you for your view, appreciate it.

                        – RavinderSingh13
                        Mar 7 at 19:33











                        0














                        Using Perl



                         perl -ne ' if($.==2) print ; close(ARGV) ' *.txt


                        with sample files



                        $ cat allison1.txt
                        line1 in file1
                        line2 in file1

                        $ cat allison2.txt
                        line1 in file2
                        line2 in file2

                        $ cat allison3.txt
                        line1 in file3
                        line2 in file3

                        $ perl -ne ' if($.==2) print ; close(ARGV) ' allison*txt
                        line2 in file1
                        line2 in file2
                        line2 in file3

                        $





                        share|improve this answer



























                          0














                          Using Perl



                           perl -ne ' if($.==2) print ; close(ARGV) ' *.txt


                          with sample files



                          $ cat allison1.txt
                          line1 in file1
                          line2 in file1

                          $ cat allison2.txt
                          line1 in file2
                          line2 in file2

                          $ cat allison3.txt
                          line1 in file3
                          line2 in file3

                          $ perl -ne ' if($.==2) print ; close(ARGV) ' allison*txt
                          line2 in file1
                          line2 in file2
                          line2 in file3

                          $





                          share|improve this answer

























                            0












                            0








                            0







                            Using Perl



                             perl -ne ' if($.==2) print ; close(ARGV) ' *.txt


                            with sample files



                            $ cat allison1.txt
                            line1 in file1
                            line2 in file1

                            $ cat allison2.txt
                            line1 in file2
                            line2 in file2

                            $ cat allison3.txt
                            line1 in file3
                            line2 in file3

                            $ perl -ne ' if($.==2) print ; close(ARGV) ' allison*txt
                            line2 in file1
                            line2 in file2
                            line2 in file3

                            $





                            share|improve this answer













                            Using Perl



                             perl -ne ' if($.==2) print ; close(ARGV) ' *.txt


                            with sample files



                            $ cat allison1.txt
                            line1 in file1
                            line2 in file1

                            $ cat allison2.txt
                            line1 in file2
                            line2 in file2

                            $ cat allison3.txt
                            line1 in file3
                            line2 in file3

                            $ perl -ne ' if($.==2) print ; close(ARGV) ' allison*txt
                            line2 in file1
                            line2 in file2
                            line2 in file3

                            $






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 8 at 5:02









                            stack0114106stack0114106

                            4,7602423




                            4,7602423



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • 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%2fstackoverflow.com%2fquestions%2f55051127%2fmerge-2nd-line-of-txt-files-together%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

                                Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                                Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                                2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived