Adding an empty line between first and second line in a text file












2















A file (foo.csv) contain entries (four columns) as follows:



A 5.3 3.2 1.2 
A 2.1 3.4 6.7
A 3.4 2.1 5.6
A 0.4 2.2 4.2


In this file, I want to add the total number of lines in the first line followed by an empty line.



I want the output to be as follow.



4

A 5.3 3.2 1.2
A 2.1 3.4 6.7
A 3.4 2.1 5.6
A 0.4 2.2 4.2


Here is what I tried.



#to get the total number of lines in the file foo.csv
t=$((wc -l foo.csv | cut -d" " -f1))
#to add an empty line
sed -i "1i\" foo.csv
#to insert the total number at the top; this works fine.
sed -i "1i $t" foo.csv


I need to do this for a bunch of files. So, script will be useful. The problem seems to be in sed -i "1i\" foo.csv. How to correct this?










share|improve this question




















  • 1





    Could you please add more realistic samples of input and expected output in your post as it is not clear.

    – RavinderSingh13
    Nov 28 '18 at 3:33






  • 1





    This would do : echo -e "$tnn$(cat foo.csv)", you should assign variable t as t=$(wc -l < /path_of_file/foo.csv), no other sed commands required.

    – User123
    Nov 28 '18 at 4:06


















2















A file (foo.csv) contain entries (four columns) as follows:



A 5.3 3.2 1.2 
A 2.1 3.4 6.7
A 3.4 2.1 5.6
A 0.4 2.2 4.2


In this file, I want to add the total number of lines in the first line followed by an empty line.



I want the output to be as follow.



4

A 5.3 3.2 1.2
A 2.1 3.4 6.7
A 3.4 2.1 5.6
A 0.4 2.2 4.2


Here is what I tried.



#to get the total number of lines in the file foo.csv
t=$((wc -l foo.csv | cut -d" " -f1))
#to add an empty line
sed -i "1i\" foo.csv
#to insert the total number at the top; this works fine.
sed -i "1i $t" foo.csv


I need to do this for a bunch of files. So, script will be useful. The problem seems to be in sed -i "1i\" foo.csv. How to correct this?










share|improve this question




















  • 1





    Could you please add more realistic samples of input and expected output in your post as it is not clear.

    – RavinderSingh13
    Nov 28 '18 at 3:33






  • 1





    This would do : echo -e "$tnn$(cat foo.csv)", you should assign variable t as t=$(wc -l < /path_of_file/foo.csv), no other sed commands required.

    – User123
    Nov 28 '18 at 4:06
















2












2








2








A file (foo.csv) contain entries (four columns) as follows:



A 5.3 3.2 1.2 
A 2.1 3.4 6.7
A 3.4 2.1 5.6
A 0.4 2.2 4.2


In this file, I want to add the total number of lines in the first line followed by an empty line.



I want the output to be as follow.



4

A 5.3 3.2 1.2
A 2.1 3.4 6.7
A 3.4 2.1 5.6
A 0.4 2.2 4.2


Here is what I tried.



#to get the total number of lines in the file foo.csv
t=$((wc -l foo.csv | cut -d" " -f1))
#to add an empty line
sed -i "1i\" foo.csv
#to insert the total number at the top; this works fine.
sed -i "1i $t" foo.csv


I need to do this for a bunch of files. So, script will be useful. The problem seems to be in sed -i "1i\" foo.csv. How to correct this?










share|improve this question
















A file (foo.csv) contain entries (four columns) as follows:



A 5.3 3.2 1.2 
A 2.1 3.4 6.7
A 3.4 2.1 5.6
A 0.4 2.2 4.2


In this file, I want to add the total number of lines in the first line followed by an empty line.



I want the output to be as follow.



4

A 5.3 3.2 1.2
A 2.1 3.4 6.7
A 3.4 2.1 5.6
A 0.4 2.2 4.2


Here is what I tried.



#to get the total number of lines in the file foo.csv
t=$((wc -l foo.csv | cut -d" " -f1))
#to add an empty line
sed -i "1i\" foo.csv
#to insert the total number at the top; this works fine.
sed -i "1i $t" foo.csv


I need to do this for a bunch of files. So, script will be useful. The problem seems to be in sed -i "1i\" foo.csv. How to correct this?







awk sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 8 '18 at 14:58









marc_s

581k13011211268




581k13011211268










asked Nov 28 '18 at 3:32









phenomenonphenomenon

1338




1338








  • 1





    Could you please add more realistic samples of input and expected output in your post as it is not clear.

    – RavinderSingh13
    Nov 28 '18 at 3:33






  • 1





    This would do : echo -e "$tnn$(cat foo.csv)", you should assign variable t as t=$(wc -l < /path_of_file/foo.csv), no other sed commands required.

    – User123
    Nov 28 '18 at 4:06
















  • 1





    Could you please add more realistic samples of input and expected output in your post as it is not clear.

    – RavinderSingh13
    Nov 28 '18 at 3:33






  • 1





    This would do : echo -e "$tnn$(cat foo.csv)", you should assign variable t as t=$(wc -l < /path_of_file/foo.csv), no other sed commands required.

    – User123
    Nov 28 '18 at 4:06










1




1





Could you please add more realistic samples of input and expected output in your post as it is not clear.

– RavinderSingh13
Nov 28 '18 at 3:33





Could you please add more realistic samples of input and expected output in your post as it is not clear.

– RavinderSingh13
Nov 28 '18 at 3:33




1




1





This would do : echo -e "$tnn$(cat foo.csv)", you should assign variable t as t=$(wc -l < /path_of_file/foo.csv), no other sed commands required.

– User123
Nov 28 '18 at 4:06







This would do : echo -e "$tnn$(cat foo.csv)", you should assign variable t as t=$(wc -l < /path_of_file/foo.csv), no other sed commands required.

– User123
Nov 28 '18 at 4:06














4 Answers
4






active

oldest

votes


















2














If you are ok with awk could you please try following.



awk -v line=$(wc -l < Input_file) 'FNR==1{print line ORS} 1'  Input_file


In case you want to add output into Input_file itself then append > temp_file && mv temp_file Input_file to above code then.



Explanation: Adding explanation for above code too now.



awk -v line=$(wc -l < Input_file  ) '       ##Creating variable line whose value is bash command wc -l to get line count for Input_file as per OP request.
FNR==1{ ##Checking if line number is 1 here then do following.
print line ORS ##Printing variable line here with ORS whose value is new line here.
} ##Closing FNR block here.
1 ##awk works on method of pattern and action mentioning 1 making condition TRUE and no action will make print to happen.
' Input_file ##Mentioning Input_file name here.





share|improve this answer

































    3














    do the line counting with awk as well.



    $ awk 'NR==FNR{next} FNR==1{print NR-1 ORS}1' file{,}


    or, with tac...tac



    $ tac file | awk '1; END{print ORS NR}' | tac





    share|improve this answer

































      1














      You can do it quite simply using sed with the 0,addr2 form (see man sed under "Addresses") with general substitution, e.g.



      $ sed '0,/^/s/^/4nn/' file
      4

      A 5.3 3.2 1.2
      A 2.1 3.4 6.7
      A 3.4 2.1 5.6
      A 0.4 2.2 4.2


      The sed expression simply finds the first occurrence of the beginning of the line 0,/^/ and then substitutes the beginning of the line with 4nn, using s/^/4nn/



      Add the -i option to edit-in-place (or -i.bak to create a back of the original (e.g. file.bak) while editing in place.



      If you are interested in setting the number of lines, then you can simply get the lines with wc -l using command substitution, e.g.



      $ sed "0,/^/s/^/$(wc -l <file2)nn/" file2
      8

      A 5.3 3.2 1.2
      A 2.1 3.4 6.7
      A 3.4 2.1 5.6
      A 0.4 2.2 4.2
      A 5.3 3.2 1.2
      A 2.1 3.4 6.7
      A 3.4 2.1 5.6
      A 0.4 2.2 4.2


      (note: the use of double-quotes instead of single-quotes to allow expansion of the command substitution)






      share|improve this answer


























      • The number (for this file 4) may vary for each file. So, I need to use variable in 'sed'.

        – phenomenon
        Nov 28 '18 at 4:22






      • 1





        You can always use wc -l to get the number of lines -- I'll edit.

        – David C. Rankin
        Nov 28 '18 at 4:24



















      0














      This might work for you (GNU sed):



      sed -e '1e wc -l <file' -e '1H;1g' file


      or to do everything in sed:



      sed -e '1e sed "$=;d" file' -e '1H;1g' file


      This uses the e command to evaluate unix commands. Normally this is done using the e flag to the s command, but it can be used following an address, as in this situation.



      An alternative, using a pipe:



      wc -l <file | sed '1G' - file


      or:



      sed '$=;d' file | sed '1G' - file


      Use the result of a wc or sed command as the first input file.



      On retrospect, the easiest solution (although not the most efficient):



      sed 'H;$!d;=;x' file


      Which slurps the file into the hold space and inserts the number of lines and a blank line before printing out the hold space.






      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%2f53511698%2fadding-an-empty-line-between-first-and-second-line-in-a-text-file%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









        2














        If you are ok with awk could you please try following.



        awk -v line=$(wc -l < Input_file) 'FNR==1{print line ORS} 1'  Input_file


        In case you want to add output into Input_file itself then append > temp_file && mv temp_file Input_file to above code then.



        Explanation: Adding explanation for above code too now.



        awk -v line=$(wc -l < Input_file  ) '       ##Creating variable line whose value is bash command wc -l to get line count for Input_file as per OP request.
        FNR==1{ ##Checking if line number is 1 here then do following.
        print line ORS ##Printing variable line here with ORS whose value is new line here.
        } ##Closing FNR block here.
        1 ##awk works on method of pattern and action mentioning 1 making condition TRUE and no action will make print to happen.
        ' Input_file ##Mentioning Input_file name here.





        share|improve this answer






























          2














          If you are ok with awk could you please try following.



          awk -v line=$(wc -l < Input_file) 'FNR==1{print line ORS} 1'  Input_file


          In case you want to add output into Input_file itself then append > temp_file && mv temp_file Input_file to above code then.



          Explanation: Adding explanation for above code too now.



          awk -v line=$(wc -l < Input_file  ) '       ##Creating variable line whose value is bash command wc -l to get line count for Input_file as per OP request.
          FNR==1{ ##Checking if line number is 1 here then do following.
          print line ORS ##Printing variable line here with ORS whose value is new line here.
          } ##Closing FNR block here.
          1 ##awk works on method of pattern and action mentioning 1 making condition TRUE and no action will make print to happen.
          ' Input_file ##Mentioning Input_file name here.





          share|improve this answer




























            2












            2








            2







            If you are ok with awk could you please try following.



            awk -v line=$(wc -l < Input_file) 'FNR==1{print line ORS} 1'  Input_file


            In case you want to add output into Input_file itself then append > temp_file && mv temp_file Input_file to above code then.



            Explanation: Adding explanation for above code too now.



            awk -v line=$(wc -l < Input_file  ) '       ##Creating variable line whose value is bash command wc -l to get line count for Input_file as per OP request.
            FNR==1{ ##Checking if line number is 1 here then do following.
            print line ORS ##Printing variable line here with ORS whose value is new line here.
            } ##Closing FNR block here.
            1 ##awk works on method of pattern and action mentioning 1 making condition TRUE and no action will make print to happen.
            ' Input_file ##Mentioning Input_file name here.





            share|improve this answer















            If you are ok with awk could you please try following.



            awk -v line=$(wc -l < Input_file) 'FNR==1{print line ORS} 1'  Input_file


            In case you want to add output into Input_file itself then append > temp_file && mv temp_file Input_file to above code then.



            Explanation: Adding explanation for above code too now.



            awk -v line=$(wc -l < Input_file  ) '       ##Creating variable line whose value is bash command wc -l to get line count for Input_file as per OP request.
            FNR==1{ ##Checking if line number is 1 here then do following.
            print line ORS ##Printing variable line here with ORS whose value is new line here.
            } ##Closing FNR block here.
            1 ##awk works on method of pattern and action mentioning 1 making condition TRUE and no action will make print to happen.
            ' Input_file ##Mentioning Input_file name here.






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 28 '18 at 4:06

























            answered Nov 28 '18 at 4:01









            RavinderSingh13RavinderSingh13

            29.7k41639




            29.7k41639

























                3














                do the line counting with awk as well.



                $ awk 'NR==FNR{next} FNR==1{print NR-1 ORS}1' file{,}


                or, with tac...tac



                $ tac file | awk '1; END{print ORS NR}' | tac





                share|improve this answer






























                  3














                  do the line counting with awk as well.



                  $ awk 'NR==FNR{next} FNR==1{print NR-1 ORS}1' file{,}


                  or, with tac...tac



                  $ tac file | awk '1; END{print ORS NR}' | tac





                  share|improve this answer




























                    3












                    3








                    3







                    do the line counting with awk as well.



                    $ awk 'NR==FNR{next} FNR==1{print NR-1 ORS}1' file{,}


                    or, with tac...tac



                    $ tac file | awk '1; END{print ORS NR}' | tac





                    share|improve this answer















                    do the line counting with awk as well.



                    $ awk 'NR==FNR{next} FNR==1{print NR-1 ORS}1' file{,}


                    or, with tac...tac



                    $ tac file | awk '1; END{print ORS NR}' | tac






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 28 '18 at 4:38

























                    answered Nov 28 '18 at 4:33









                    karakfakarakfa

                    50.6k52839




                    50.6k52839























                        1














                        You can do it quite simply using sed with the 0,addr2 form (see man sed under "Addresses") with general substitution, e.g.



                        $ sed '0,/^/s/^/4nn/' file
                        4

                        A 5.3 3.2 1.2
                        A 2.1 3.4 6.7
                        A 3.4 2.1 5.6
                        A 0.4 2.2 4.2


                        The sed expression simply finds the first occurrence of the beginning of the line 0,/^/ and then substitutes the beginning of the line with 4nn, using s/^/4nn/



                        Add the -i option to edit-in-place (or -i.bak to create a back of the original (e.g. file.bak) while editing in place.



                        If you are interested in setting the number of lines, then you can simply get the lines with wc -l using command substitution, e.g.



                        $ sed "0,/^/s/^/$(wc -l <file2)nn/" file2
                        8

                        A 5.3 3.2 1.2
                        A 2.1 3.4 6.7
                        A 3.4 2.1 5.6
                        A 0.4 2.2 4.2
                        A 5.3 3.2 1.2
                        A 2.1 3.4 6.7
                        A 3.4 2.1 5.6
                        A 0.4 2.2 4.2


                        (note: the use of double-quotes instead of single-quotes to allow expansion of the command substitution)






                        share|improve this answer


























                        • The number (for this file 4) may vary for each file. So, I need to use variable in 'sed'.

                          – phenomenon
                          Nov 28 '18 at 4:22






                        • 1





                          You can always use wc -l to get the number of lines -- I'll edit.

                          – David C. Rankin
                          Nov 28 '18 at 4:24
















                        1














                        You can do it quite simply using sed with the 0,addr2 form (see man sed under "Addresses") with general substitution, e.g.



                        $ sed '0,/^/s/^/4nn/' file
                        4

                        A 5.3 3.2 1.2
                        A 2.1 3.4 6.7
                        A 3.4 2.1 5.6
                        A 0.4 2.2 4.2


                        The sed expression simply finds the first occurrence of the beginning of the line 0,/^/ and then substitutes the beginning of the line with 4nn, using s/^/4nn/



                        Add the -i option to edit-in-place (or -i.bak to create a back of the original (e.g. file.bak) while editing in place.



                        If you are interested in setting the number of lines, then you can simply get the lines with wc -l using command substitution, e.g.



                        $ sed "0,/^/s/^/$(wc -l <file2)nn/" file2
                        8

                        A 5.3 3.2 1.2
                        A 2.1 3.4 6.7
                        A 3.4 2.1 5.6
                        A 0.4 2.2 4.2
                        A 5.3 3.2 1.2
                        A 2.1 3.4 6.7
                        A 3.4 2.1 5.6
                        A 0.4 2.2 4.2


                        (note: the use of double-quotes instead of single-quotes to allow expansion of the command substitution)






                        share|improve this answer


























                        • The number (for this file 4) may vary for each file. So, I need to use variable in 'sed'.

                          – phenomenon
                          Nov 28 '18 at 4:22






                        • 1





                          You can always use wc -l to get the number of lines -- I'll edit.

                          – David C. Rankin
                          Nov 28 '18 at 4:24














                        1












                        1








                        1







                        You can do it quite simply using sed with the 0,addr2 form (see man sed under "Addresses") with general substitution, e.g.



                        $ sed '0,/^/s/^/4nn/' file
                        4

                        A 5.3 3.2 1.2
                        A 2.1 3.4 6.7
                        A 3.4 2.1 5.6
                        A 0.4 2.2 4.2


                        The sed expression simply finds the first occurrence of the beginning of the line 0,/^/ and then substitutes the beginning of the line with 4nn, using s/^/4nn/



                        Add the -i option to edit-in-place (or -i.bak to create a back of the original (e.g. file.bak) while editing in place.



                        If you are interested in setting the number of lines, then you can simply get the lines with wc -l using command substitution, e.g.



                        $ sed "0,/^/s/^/$(wc -l <file2)nn/" file2
                        8

                        A 5.3 3.2 1.2
                        A 2.1 3.4 6.7
                        A 3.4 2.1 5.6
                        A 0.4 2.2 4.2
                        A 5.3 3.2 1.2
                        A 2.1 3.4 6.7
                        A 3.4 2.1 5.6
                        A 0.4 2.2 4.2


                        (note: the use of double-quotes instead of single-quotes to allow expansion of the command substitution)






                        share|improve this answer















                        You can do it quite simply using sed with the 0,addr2 form (see man sed under "Addresses") with general substitution, e.g.



                        $ sed '0,/^/s/^/4nn/' file
                        4

                        A 5.3 3.2 1.2
                        A 2.1 3.4 6.7
                        A 3.4 2.1 5.6
                        A 0.4 2.2 4.2


                        The sed expression simply finds the first occurrence of the beginning of the line 0,/^/ and then substitutes the beginning of the line with 4nn, using s/^/4nn/



                        Add the -i option to edit-in-place (or -i.bak to create a back of the original (e.g. file.bak) while editing in place.



                        If you are interested in setting the number of lines, then you can simply get the lines with wc -l using command substitution, e.g.



                        $ sed "0,/^/s/^/$(wc -l <file2)nn/" file2
                        8

                        A 5.3 3.2 1.2
                        A 2.1 3.4 6.7
                        A 3.4 2.1 5.6
                        A 0.4 2.2 4.2
                        A 5.3 3.2 1.2
                        A 2.1 3.4 6.7
                        A 3.4 2.1 5.6
                        A 0.4 2.2 4.2


                        (note: the use of double-quotes instead of single-quotes to allow expansion of the command substitution)







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Nov 28 '18 at 4:27

























                        answered Nov 28 '18 at 4:08









                        David C. RankinDavid C. Rankin

                        42.8k33050




                        42.8k33050













                        • The number (for this file 4) may vary for each file. So, I need to use variable in 'sed'.

                          – phenomenon
                          Nov 28 '18 at 4:22






                        • 1





                          You can always use wc -l to get the number of lines -- I'll edit.

                          – David C. Rankin
                          Nov 28 '18 at 4:24



















                        • The number (for this file 4) may vary for each file. So, I need to use variable in 'sed'.

                          – phenomenon
                          Nov 28 '18 at 4:22






                        • 1





                          You can always use wc -l to get the number of lines -- I'll edit.

                          – David C. Rankin
                          Nov 28 '18 at 4:24

















                        The number (for this file 4) may vary for each file. So, I need to use variable in 'sed'.

                        – phenomenon
                        Nov 28 '18 at 4:22





                        The number (for this file 4) may vary for each file. So, I need to use variable in 'sed'.

                        – phenomenon
                        Nov 28 '18 at 4:22




                        1




                        1





                        You can always use wc -l to get the number of lines -- I'll edit.

                        – David C. Rankin
                        Nov 28 '18 at 4:24





                        You can always use wc -l to get the number of lines -- I'll edit.

                        – David C. Rankin
                        Nov 28 '18 at 4:24











                        0














                        This might work for you (GNU sed):



                        sed -e '1e wc -l <file' -e '1H;1g' file


                        or to do everything in sed:



                        sed -e '1e sed "$=;d" file' -e '1H;1g' file


                        This uses the e command to evaluate unix commands. Normally this is done using the e flag to the s command, but it can be used following an address, as in this situation.



                        An alternative, using a pipe:



                        wc -l <file | sed '1G' - file


                        or:



                        sed '$=;d' file | sed '1G' - file


                        Use the result of a wc or sed command as the first input file.



                        On retrospect, the easiest solution (although not the most efficient):



                        sed 'H;$!d;=;x' file


                        Which slurps the file into the hold space and inserts the number of lines and a blank line before printing out the hold space.






                        share|improve this answer






























                          0














                          This might work for you (GNU sed):



                          sed -e '1e wc -l <file' -e '1H;1g' file


                          or to do everything in sed:



                          sed -e '1e sed "$=;d" file' -e '1H;1g' file


                          This uses the e command to evaluate unix commands. Normally this is done using the e flag to the s command, but it can be used following an address, as in this situation.



                          An alternative, using a pipe:



                          wc -l <file | sed '1G' - file


                          or:



                          sed '$=;d' file | sed '1G' - file


                          Use the result of a wc or sed command as the first input file.



                          On retrospect, the easiest solution (although not the most efficient):



                          sed 'H;$!d;=;x' file


                          Which slurps the file into the hold space and inserts the number of lines and a blank line before printing out the hold space.






                          share|improve this answer




























                            0












                            0








                            0







                            This might work for you (GNU sed):



                            sed -e '1e wc -l <file' -e '1H;1g' file


                            or to do everything in sed:



                            sed -e '1e sed "$=;d" file' -e '1H;1g' file


                            This uses the e command to evaluate unix commands. Normally this is done using the e flag to the s command, but it can be used following an address, as in this situation.



                            An alternative, using a pipe:



                            wc -l <file | sed '1G' - file


                            or:



                            sed '$=;d' file | sed '1G' - file


                            Use the result of a wc or sed command as the first input file.



                            On retrospect, the easiest solution (although not the most efficient):



                            sed 'H;$!d;=;x' file


                            Which slurps the file into the hold space and inserts the number of lines and a blank line before printing out the hold space.






                            share|improve this answer















                            This might work for you (GNU sed):



                            sed -e '1e wc -l <file' -e '1H;1g' file


                            or to do everything in sed:



                            sed -e '1e sed "$=;d" file' -e '1H;1g' file


                            This uses the e command to evaluate unix commands. Normally this is done using the e flag to the s command, but it can be used following an address, as in this situation.



                            An alternative, using a pipe:



                            wc -l <file | sed '1G' - file


                            or:



                            sed '$=;d' file | sed '1G' - file


                            Use the result of a wc or sed command as the first input file.



                            On retrospect, the easiest solution (although not the most efficient):



                            sed 'H;$!d;=;x' file


                            Which slurps the file into the hold space and inserts the number of lines and a blank line before printing out the hold space.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 28 '18 at 14:38

























                            answered Nov 28 '18 at 9:06









                            potongpotong

                            36.1k43062




                            36.1k43062






























                                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%2f53511698%2fadding-an-empty-line-between-first-and-second-line-in-a-text-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

                                Contact image not getting when fetch all contact list from iPhone by CNContact

                                count number of partitions of a set with n elements into k subsets

                                A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks