Is there a way to count the number of lines of text in a file including non-delimited ones?How to add a...

Submitting a new paper just after another was accepted by the same journal

80's/90's superhero cartoon with a man on fire and a man who made ice runways like Frozone

In which case does the Security misconfiguration vulnerability apply to?

Does fossil fuels use since 1990 account for half of all the fossil fuels used in history?

How do I call a 6 digit Austrailian phone number with a US based mobile phone?

A continuous water "planet" ring around a star

Is it possible to grow new organs through exposure to radioactivity?

Are there any other rule mechanics that could grant Thieves' Cant?

Markov-chain sentence generator in Python

Are there really no countries that protect Freedom of Speech as the United States does?

(A room / an office) where an artist works

Help, I cannot decide when to start the story

Are differences between uniformly distributed numbers uniformly distributed?

What should I call bands of armed men in the Middle Ages?

PhD advisor lost funding, need advice

How to take the beginning and end parts of a list with simpler syntax?

Flood on the top floor

Why is there a large performance impact when looping over an array over 240 elements?

Corroded Metal vs Magical Armor, should it melt it?

Telephone number in spoken words

Why does my purified Pokémon need to be healed?

How exactly are corporate bonds priced at issue

Is there a way to encourage or even force airlines and booking engines to show options with overnight layovers?

Is there a standardised way to check fake news?



Is there a way to count the number of lines of text in a file including non-delimited ones?


How to add a newline to the end of a file?The simplest method to count lines matching specific patterns, including '0' if line is not found?Count lines of non-terminating inputHow to put a newline special character into a file using the echo command and redirection operator?How to efficiently split up a large text file wihout splitting multiline records?Why does wc get wrong result with output from psCounting files, directories, etc. in given directory and formatting output on screenBetter shell solution when blank lines may be piped to wcsearching letters in one line using grep and wcWhy is my multi-line perl string replace adding a blank line?






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







4















The POSIX wc command counts how many POSIX lines in a file. The POSIX standard defines a line as a text string with the suffix n. Without n, a pure text string can't be called a line.



But to me, it's more natural to count how many lines of text string in a file. Is there an easy way to do that?



root:[~]# printf "aanbb" | wc -l
1
root:[~]# printf "aanbbn" | wc -l
2
root:[~]#









share|improve this question



























  • Related: How to add a newline to the end of a file? (when the file doesn’t already have one).

    – Stephen Kitt
    13 hours ago


















4















The POSIX wc command counts how many POSIX lines in a file. The POSIX standard defines a line as a text string with the suffix n. Without n, a pure text string can't be called a line.



But to me, it's more natural to count how many lines of text string in a file. Is there an easy way to do that?



root:[~]# printf "aanbb" | wc -l
1
root:[~]# printf "aanbbn" | wc -l
2
root:[~]#









share|improve this question



























  • Related: How to add a newline to the end of a file? (when the file doesn’t already have one).

    – Stephen Kitt
    13 hours ago














4












4








4








The POSIX wc command counts how many POSIX lines in a file. The POSIX standard defines a line as a text string with the suffix n. Without n, a pure text string can't be called a line.



But to me, it's more natural to count how many lines of text string in a file. Is there an easy way to do that?



root:[~]# printf "aanbb" | wc -l
1
root:[~]# printf "aanbbn" | wc -l
2
root:[~]#









share|improve this question
















The POSIX wc command counts how many POSIX lines in a file. The POSIX standard defines a line as a text string with the suffix n. Without n, a pure text string can't be called a line.



But to me, it's more natural to count how many lines of text string in a file. Is there an easy way to do that?



root:[~]# printf "aanbb" | wc -l
1
root:[~]# printf "aanbbn" | wc -l
2
root:[~]#






newlines wc line






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 14 hours ago









Stéphane Chazelas

329k57 gold badges641 silver badges1008 bronze badges




329k57 gold badges641 silver badges1008 bronze badges










asked 15 hours ago









Just a learnerJust a learner

6481 gold badge6 silver badges17 bronze badges




6481 gold badge6 silver badges17 bronze badges
















  • Related: How to add a newline to the end of a file? (when the file doesn’t already have one).

    – Stephen Kitt
    13 hours ago



















  • Related: How to add a newline to the end of a file? (when the file doesn’t already have one).

    – Stephen Kitt
    13 hours ago

















Related: How to add a newline to the end of a file? (when the file doesn’t already have one).

– Stephen Kitt
13 hours ago





Related: How to add a newline to the end of a file? (when the file doesn’t already have one).

– Stephen Kitt
13 hours ago










2 Answers
2






active

oldest

votes


















7














With GNU sed, you can use:



sed '$=;d'


As GNU sed does consider those extra characters after the last newline as an extra line. GNU sed like most GNU utilities also supports NUL characters in its input and doesn't have a limitation on the length of lines (the two other criteria that make an input non-text as per POSIX).



POSIXLy, building-up on @Inian's answer to support too-long lines and NUL bytes:



LC_ALL=C tr -cs 'n' '[x*]' | awk 'END {print NR}'


That tr command translates all sequences of one or more character (each byte interpreted as a character in the C locale to avoid decoding issues) other than newline to one x character, so awk input records will be either 0 or 1 byte long and its input contain only x and newline characters.



$ printf '%10000snabncnd' | wc -l
3

$ printf '%10000snabncnd' | mawk 'END{print NR}'
2
$ printf '%10000snabncnd' | busybox awk 'END{print NR}'
5
$ printf '%10000snabncnd' | gawk 'END{print NR}'
4

$ printf '%10000snabncnd' | LC_ALL=C tr -cs 'n' '[x*]' | mawk 'END{print NR}'
4





share|improve this answer



































    4














    You can use awk for this which has a special variable NR which tracks the number of current record from the start of the file. The variable gets incremented at the end of each line. When printed at the END block i.e. after all the input lines are processed it prints the number of the last record processed.



    printf "aanbb" | awk 'END { print NR }'
    2

    printf "aanbbn" | awk 'END { print NR }'
    2





    share|improve this answer





















    • 2





      Note that with some awk implementations, that still implies the input doesn't contain NUL characters (which would also make that input non-text as per POSIX).

      – Stéphane Chazelas
      14 hours ago














    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%2f535308%2fis-there-a-way-to-count-the-number-of-lines-of-text-in-a-file-including-non-deli%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    7














    With GNU sed, you can use:



    sed '$=;d'


    As GNU sed does consider those extra characters after the last newline as an extra line. GNU sed like most GNU utilities also supports NUL characters in its input and doesn't have a limitation on the length of lines (the two other criteria that make an input non-text as per POSIX).



    POSIXLy, building-up on @Inian's answer to support too-long lines and NUL bytes:



    LC_ALL=C tr -cs 'n' '[x*]' | awk 'END {print NR}'


    That tr command translates all sequences of one or more character (each byte interpreted as a character in the C locale to avoid decoding issues) other than newline to one x character, so awk input records will be either 0 or 1 byte long and its input contain only x and newline characters.



    $ printf '%10000snabncnd' | wc -l
    3

    $ printf '%10000snabncnd' | mawk 'END{print NR}'
    2
    $ printf '%10000snabncnd' | busybox awk 'END{print NR}'
    5
    $ printf '%10000snabncnd' | gawk 'END{print NR}'
    4

    $ printf '%10000snabncnd' | LC_ALL=C tr -cs 'n' '[x*]' | mawk 'END{print NR}'
    4





    share|improve this answer
































      7














      With GNU sed, you can use:



      sed '$=;d'


      As GNU sed does consider those extra characters after the last newline as an extra line. GNU sed like most GNU utilities also supports NUL characters in its input and doesn't have a limitation on the length of lines (the two other criteria that make an input non-text as per POSIX).



      POSIXLy, building-up on @Inian's answer to support too-long lines and NUL bytes:



      LC_ALL=C tr -cs 'n' '[x*]' | awk 'END {print NR}'


      That tr command translates all sequences of one or more character (each byte interpreted as a character in the C locale to avoid decoding issues) other than newline to one x character, so awk input records will be either 0 or 1 byte long and its input contain only x and newline characters.



      $ printf '%10000snabncnd' | wc -l
      3

      $ printf '%10000snabncnd' | mawk 'END{print NR}'
      2
      $ printf '%10000snabncnd' | busybox awk 'END{print NR}'
      5
      $ printf '%10000snabncnd' | gawk 'END{print NR}'
      4

      $ printf '%10000snabncnd' | LC_ALL=C tr -cs 'n' '[x*]' | mawk 'END{print NR}'
      4





      share|improve this answer






























        7












        7








        7







        With GNU sed, you can use:



        sed '$=;d'


        As GNU sed does consider those extra characters after the last newline as an extra line. GNU sed like most GNU utilities also supports NUL characters in its input and doesn't have a limitation on the length of lines (the two other criteria that make an input non-text as per POSIX).



        POSIXLy, building-up on @Inian's answer to support too-long lines and NUL bytes:



        LC_ALL=C tr -cs 'n' '[x*]' | awk 'END {print NR}'


        That tr command translates all sequences of one or more character (each byte interpreted as a character in the C locale to avoid decoding issues) other than newline to one x character, so awk input records will be either 0 or 1 byte long and its input contain only x and newline characters.



        $ printf '%10000snabncnd' | wc -l
        3

        $ printf '%10000snabncnd' | mawk 'END{print NR}'
        2
        $ printf '%10000snabncnd' | busybox awk 'END{print NR}'
        5
        $ printf '%10000snabncnd' | gawk 'END{print NR}'
        4

        $ printf '%10000snabncnd' | LC_ALL=C tr -cs 'n' '[x*]' | mawk 'END{print NR}'
        4





        share|improve this answer















        With GNU sed, you can use:



        sed '$=;d'


        As GNU sed does consider those extra characters after the last newline as an extra line. GNU sed like most GNU utilities also supports NUL characters in its input and doesn't have a limitation on the length of lines (the two other criteria that make an input non-text as per POSIX).



        POSIXLy, building-up on @Inian's answer to support too-long lines and NUL bytes:



        LC_ALL=C tr -cs 'n' '[x*]' | awk 'END {print NR}'


        That tr command translates all sequences of one or more character (each byte interpreted as a character in the C locale to avoid decoding issues) other than newline to one x character, so awk input records will be either 0 or 1 byte long and its input contain only x and newline characters.



        $ printf '%10000snabncnd' | wc -l
        3

        $ printf '%10000snabncnd' | mawk 'END{print NR}'
        2
        $ printf '%10000snabncnd' | busybox awk 'END{print NR}'
        5
        $ printf '%10000snabncnd' | gawk 'END{print NR}'
        4

        $ printf '%10000snabncnd' | LC_ALL=C tr -cs 'n' '[x*]' | mawk 'END{print NR}'
        4






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 14 hours ago

























        answered 14 hours ago









        Stéphane ChazelasStéphane Chazelas

        329k57 gold badges641 silver badges1008 bronze badges




        329k57 gold badges641 silver badges1008 bronze badges




























            4














            You can use awk for this which has a special variable NR which tracks the number of current record from the start of the file. The variable gets incremented at the end of each line. When printed at the END block i.e. after all the input lines are processed it prints the number of the last record processed.



            printf "aanbb" | awk 'END { print NR }'
            2

            printf "aanbbn" | awk 'END { print NR }'
            2





            share|improve this answer





















            • 2





              Note that with some awk implementations, that still implies the input doesn't contain NUL characters (which would also make that input non-text as per POSIX).

              – Stéphane Chazelas
              14 hours ago
















            4














            You can use awk for this which has a special variable NR which tracks the number of current record from the start of the file. The variable gets incremented at the end of each line. When printed at the END block i.e. after all the input lines are processed it prints the number of the last record processed.



            printf "aanbb" | awk 'END { print NR }'
            2

            printf "aanbbn" | awk 'END { print NR }'
            2





            share|improve this answer





















            • 2





              Note that with some awk implementations, that still implies the input doesn't contain NUL characters (which would also make that input non-text as per POSIX).

              – Stéphane Chazelas
              14 hours ago














            4












            4








            4







            You can use awk for this which has a special variable NR which tracks the number of current record from the start of the file. The variable gets incremented at the end of each line. When printed at the END block i.e. after all the input lines are processed it prints the number of the last record processed.



            printf "aanbb" | awk 'END { print NR }'
            2

            printf "aanbbn" | awk 'END { print NR }'
            2





            share|improve this answer













            You can use awk for this which has a special variable NR which tracks the number of current record from the start of the file. The variable gets incremented at the end of each line. When printed at the END block i.e. after all the input lines are processed it prints the number of the last record processed.



            printf "aanbb" | awk 'END { print NR }'
            2

            printf "aanbbn" | awk 'END { print NR }'
            2






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 15 hours ago









            InianInian

            7,03017 silver badges34 bronze badges




            7,03017 silver badges34 bronze badges











            • 2





              Note that with some awk implementations, that still implies the input doesn't contain NUL characters (which would also make that input non-text as per POSIX).

              – Stéphane Chazelas
              14 hours ago














            • 2





              Note that with some awk implementations, that still implies the input doesn't contain NUL characters (which would also make that input non-text as per POSIX).

              – Stéphane Chazelas
              14 hours ago








            2




            2





            Note that with some awk implementations, that still implies the input doesn't contain NUL characters (which would also make that input non-text as per POSIX).

            – Stéphane Chazelas
            14 hours ago





            Note that with some awk implementations, that still implies the input doesn't contain NUL characters (which would also make that input non-text as per POSIX).

            – Stéphane Chazelas
            14 hours ago


















            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%2f535308%2fis-there-a-way-to-count-the-number-of-lines-of-text-in-a-file-including-non-deli%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

            Hudson River Historic District Contents Geography History The district today Aesthetics Cultural...

            The number designs the writing. Feandra Aversely Definition: The act of ingrafting a sprig or shoot of one...

            Ayherre Geografie Demografie Externe links Navigatiemenu43° 23′ NB, 1° 15′ WL43° 23′ NB, 1°...