3 More generally.
source | link

As an alternative without the problem of spaces (POSIX):

$ sed -n '$=' ~/myfile.txt

Captured in a variable:

$ nl=$(sed -n '$=' ~/myfile.txt)
$ echo "1-$nl-"
1-100-

Note that sed counts the last line even if it doesn't end in a newline character (as it should in a properly formatted text file). More generally, sed may fail to print the number of newline characters in non-text files.


Another alternative (not that I recommend using it):

$ nl=$(catgrep -v . ~/myfile.txt | tr -dc '\n' | tr '\n' 'x')
$ echo "1-${#nl}"
1-100

Note that grep will also print invalid characters (additionally to newlines) in the locale used.

As an alternative without the problem of spaces (POSIX):

$ sed -n '$=' ~/myfile.txt

Captured in a variable:

$ nl=$(sed -n '$=' ~/myfile.txt)
$ echo "1-$nl-"
1-100-

Note that sed counts the last line even if it doesn't end in a newline character (as it should in a properly formatted text file).


Another alternative (not that I recommend using it):

$ nl=$(cat ~/myfile.txt | tr -dc '\n' | tr '\n' 'x')
$ echo "1-${#nl}"
1-100

As an alternative without the problem of spaces (POSIX):

$ sed -n '$=' ~/myfile.txt

Captured in a variable:

$ nl=$(sed -n '$=' ~/myfile.txt)
$ echo "1-$nl-"
1-100-

Note that sed counts the last line even if it doesn't end in a newline character (as it should in a properly formatted text file). More generally, sed may fail to print the number of newline characters in non-text files.


Another alternative:

$ nl=$(grep -v . ~/myfile.txt | tr '\n' 'x')
$ echo "1-${#nl}"
1-100

Note that grep will also print invalid characters (additionally to newlines) in the locale used.

2 Typos.
source | link

As an alternative without the problem of spaces (POSIX):

$ sed -n '$=' ~/myfile.txt

Captured in a variable:

$ nl=$(sed -n '$=' ~/myfile.txt)
$ echo "1-$nl-"
1-100-

Note that sed counts the last line even if it doesn't end in a newñimenewline character (as it should in a properly formatedformatted text file).


Another alternative (not that I recomendrecommend using it):

$ nl=$(cat ~/myfile.txt | tr -dc '\n' | tr '\n' 'x')
$ echo "1-${#nl}"
1-100

As an alternative without the problem of spaces (POSIX):

$ sed -n '$=' ~/myfile.txt

Captured in a variable:

$ nl=$(sed -n '$=' ~/myfile.txt)
$ echo "1-$nl-"
1-100-

Note that sed counts the last line even if it doesn't end in a newñime character (as it should in a properly formated text file).


Another alternative (not that I recomend using it):

$ nl=$(cat ~/myfile.txt | tr -dc '\n' | tr '\n' 'x')
$ echo "1-${#nl}"
1-100

As an alternative without the problem of spaces (POSIX):

$ sed -n '$=' ~/myfile.txt

Captured in a variable:

$ nl=$(sed -n '$=' ~/myfile.txt)
$ echo "1-$nl-"
1-100-

Note that sed counts the last line even if it doesn't end in a newline character (as it should in a properly formatted text file).


Another alternative (not that I recommend using it):

$ nl=$(cat ~/myfile.txt | tr -dc '\n' | tr '\n' 'x')
$ echo "1-${#nl}"
1-100
1
source | link

As an alternative without the problem of spaces (POSIX):

$ sed -n '$=' ~/myfile.txt

Captured in a variable:

$ nl=$(sed -n '$=' ~/myfile.txt)
$ echo "1-$nl-"
1-100-

Note that sed counts the last line even if it doesn't end in a newñime character (as it should in a properly formated text file).


Another alternative (not that I recomend using it):

$ nl=$(cat ~/myfile.txt | tr -dc '\n' | tr '\n' 'x')
$ echo "1-${#nl}"
1-100