Extra space with counted line number?
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I count the number of lines of my file with this command on OSX:
nl=$(wc -l < ~/myfile.txt)
Say, nl
turns out to be 100. Now, I wish to use the result nl
in another command, but weirdly,
echo 1-$nl
gives me 1- 100
instead of 1-100
.
Demo
cv me$ nl=$(wc -l < ~/Desktop/cap.xlsx)
cv me$ echo $nl
104
cv me$ echo 1-$nl
1- 104
Why does this happen? How may I get 1-100
?
shell string wc
 |Â
show 2 more comments
up vote
3
down vote
favorite
I count the number of lines of my file with this command on OSX:
nl=$(wc -l < ~/myfile.txt)
Say, nl
turns out to be 100. Now, I wish to use the result nl
in another command, but weirdly,
echo 1-$nl
gives me 1- 100
instead of 1-100
.
Demo
cv me$ nl=$(wc -l < ~/Desktop/cap.xlsx)
cv me$ echo $nl
104
cv me$ echo 1-$nl
1- 104
Why does this happen? How may I get 1-100
?
shell string wc
What OS are you on? I can't reproduce this on my Debian.
â terdonâ¦
May 27 '15 at 15:47
@terdon I am on OS X. Please see the updates.
â Sibbs Gambling
May 27 '15 at 15:49
1
Weird. Make few tests to narrow the problem: 1) set value directlynl=100
; 2) try different delimiter, not-
; 3) put double quotes around expressionecho "1-$nl"
; 4) useprintf
instead ofecho
â jimmij
May 27 '15 at 16:03
@Sibbs; I either cannot reproduce it. What output do you get when quotingecho $nl
asecho "$nl"
.
â Janis
May 27 '15 at 16:03
@Janis I can't format that properly here. Please see the screenshot above.
â Sibbs Gambling
May 27 '15 at 16:12
 |Â
show 2 more comments
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I count the number of lines of my file with this command on OSX:
nl=$(wc -l < ~/myfile.txt)
Say, nl
turns out to be 100. Now, I wish to use the result nl
in another command, but weirdly,
echo 1-$nl
gives me 1- 100
instead of 1-100
.
Demo
cv me$ nl=$(wc -l < ~/Desktop/cap.xlsx)
cv me$ echo $nl
104
cv me$ echo 1-$nl
1- 104
Why does this happen? How may I get 1-100
?
shell string wc
I count the number of lines of my file with this command on OSX:
nl=$(wc -l < ~/myfile.txt)
Say, nl
turns out to be 100. Now, I wish to use the result nl
in another command, but weirdly,
echo 1-$nl
gives me 1- 100
instead of 1-100
.
Demo
cv me$ nl=$(wc -l < ~/Desktop/cap.xlsx)
cv me$ echo $nl
104
cv me$ echo 1-$nl
1- 104
Why does this happen? How may I get 1-100
?
shell string wc
shell string wc
edited May 27 '15 at 22:50
Gilles
511k12010141543
511k12010141543
asked May 27 '15 at 15:45
Sibbs Gambling
5202919
5202919
What OS are you on? I can't reproduce this on my Debian.
â terdonâ¦
May 27 '15 at 15:47
@terdon I am on OS X. Please see the updates.
â Sibbs Gambling
May 27 '15 at 15:49
1
Weird. Make few tests to narrow the problem: 1) set value directlynl=100
; 2) try different delimiter, not-
; 3) put double quotes around expressionecho "1-$nl"
; 4) useprintf
instead ofecho
â jimmij
May 27 '15 at 16:03
@Sibbs; I either cannot reproduce it. What output do you get when quotingecho $nl
asecho "$nl"
.
â Janis
May 27 '15 at 16:03
@Janis I can't format that properly here. Please see the screenshot above.
â Sibbs Gambling
May 27 '15 at 16:12
 |Â
show 2 more comments
What OS are you on? I can't reproduce this on my Debian.
â terdonâ¦
May 27 '15 at 15:47
@terdon I am on OS X. Please see the updates.
â Sibbs Gambling
May 27 '15 at 15:49
1
Weird. Make few tests to narrow the problem: 1) set value directlynl=100
; 2) try different delimiter, not-
; 3) put double quotes around expressionecho "1-$nl"
; 4) useprintf
instead ofecho
â jimmij
May 27 '15 at 16:03
@Sibbs; I either cannot reproduce it. What output do you get when quotingecho $nl
asecho "$nl"
.
â Janis
May 27 '15 at 16:03
@Janis I can't format that properly here. Please see the screenshot above.
â Sibbs Gambling
May 27 '15 at 16:12
What OS are you on? I can't reproduce this on my Debian.
â terdonâ¦
May 27 '15 at 15:47
What OS are you on? I can't reproduce this on my Debian.
â terdonâ¦
May 27 '15 at 15:47
@terdon I am on OS X. Please see the updates.
â Sibbs Gambling
May 27 '15 at 15:49
@terdon I am on OS X. Please see the updates.
â Sibbs Gambling
May 27 '15 at 15:49
1
1
Weird. Make few tests to narrow the problem: 1) set value directly
nl=100
; 2) try different delimiter, not -
; 3) put double quotes around expression echo "1-$nl"
; 4) use printf
instead of echo
â jimmij
May 27 '15 at 16:03
Weird. Make few tests to narrow the problem: 1) set value directly
nl=100
; 2) try different delimiter, not -
; 3) put double quotes around expression echo "1-$nl"
; 4) use printf
instead of echo
â jimmij
May 27 '15 at 16:03
@Sibbs; I either cannot reproduce it. What output do you get when quoting
echo $nl
as echo "$nl"
.â Janis
May 27 '15 at 16:03
@Sibbs; I either cannot reproduce it. What output do you get when quoting
echo $nl
as echo "$nl"
.â Janis
May 27 '15 at 16:03
@Janis I can't format that properly here. Please see the screenshot above.
â Sibbs Gambling
May 27 '15 at 16:12
@Janis I can't format that properly here. Please see the screenshot above.
â Sibbs Gambling
May 27 '15 at 16:12
 |Â
show 2 more comments
4 Answers
4
active
oldest
votes
up vote
4
down vote
As POSIX defined, the output of wc
shall contain an entry for each input file of the form:
"%d %d %d %sn", <newlines>, <words>, <bytes>, <file>
But the output file format pseudo printf()
string differs from the System V version of wc
:
"%7d%7d%7d %sn"
POSIX didn't require leading spaces to be added, so it's free for implementation to do what it want. There are different implementations of wc
, at least with OSX and wc
from heirloom tools chest, it added leading spaces to output.
$ /usr/5bin/wc -l /tmp/file
3 /tmp/file
GNU wc also add leading spaces when reading from standard in and without any options:
$ cat file | wc
5 5 65
To remove all leading spaces, in POSIX shell:
set -f
set -- $nl
nl=$1
set +f
Note that this approach assume that variable only contain leading or trailing spaces, no spaces in the middle, like a b.
add a comment |Â
up vote
4
down vote
wc
implementations may produce leading spaces; this may also be depending on whether (and how many) options are used (GNU wc
, for example, won't produce spaces if, as in your case, exactly one option is used). Those spaces are retained in the assignment nl=$(...)
. To remove the spaces, as a workaround, you could use either of:
nl=$(wc -l < ~/myfile.txt | awk 'print $1')
or
nl=$(awk 'ENDprint NR' ~/myfile.txt)
or
nl=$(wc -l < ~/Desktop/cap.xlsx)
nl=$(echo $nl)
or (but see @cuonglm's comment below for heirloom's version of wc
)
nl=$(wc -l < ~/Desktop/cap.xlsx)
nl=$nl##*
@cuonglm; Thanks for spotting that; I changed the shell regexp now fromnl=$nl## *
tonl=$nl##*
.
â Janis
May 27 '15 at 16:40
It still fail with heirloomwc
, it produce trailing spaces, too. So you still remove the entire variable.
â cuonglm
May 27 '15 at 16:42
The heirloomwc
produces trailing spaces? - Okay. I'll leave that variant still as one option. With your comment here the folks now know that in heirloom context they should apply one of the other suggestions.
â Janis
May 27 '15 at 16:46
add a comment |Â
up vote
1
down vote
Simpliest workaround (in bash or any other POSIX shell):
nl=$(wc -l < ~/myfile.txt)
nl=$(($nl))
$((...))
in standard sh
is for arithmetic expansion, so the result will always be only the number itself. Note that if wc -l
produces no output (like when the file can't be read), that will yield 0
instead of an empty string.
$((nl))
would also work in bash
but is not guaranteed to by POSIX as $nl
doesn't contain a numerical constant, and in practice doesn't work in yash
for instance.
You can also do it all at once with:
nl=$(($(wc -l < ~/myfile.txt)))
Which has the benefit of preserving the exit status of wc
.
add a comment |Â
up vote
1
down vote
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.
@StéphaneChazelas Why is sed counts the last line even if it doesn't end in a newline character not enough for you? (hmmm, for reasonably clean text files)
â Isaac
Sep 11 at 7:33
Sorry, I had overlooked that part.
â Stéphane Chazelas
Sep 11 at 8:12
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
As POSIX defined, the output of wc
shall contain an entry for each input file of the form:
"%d %d %d %sn", <newlines>, <words>, <bytes>, <file>
But the output file format pseudo printf()
string differs from the System V version of wc
:
"%7d%7d%7d %sn"
POSIX didn't require leading spaces to be added, so it's free for implementation to do what it want. There are different implementations of wc
, at least with OSX and wc
from heirloom tools chest, it added leading spaces to output.
$ /usr/5bin/wc -l /tmp/file
3 /tmp/file
GNU wc also add leading spaces when reading from standard in and without any options:
$ cat file | wc
5 5 65
To remove all leading spaces, in POSIX shell:
set -f
set -- $nl
nl=$1
set +f
Note that this approach assume that variable only contain leading or trailing spaces, no spaces in the middle, like a b.
add a comment |Â
up vote
4
down vote
As POSIX defined, the output of wc
shall contain an entry for each input file of the form:
"%d %d %d %sn", <newlines>, <words>, <bytes>, <file>
But the output file format pseudo printf()
string differs from the System V version of wc
:
"%7d%7d%7d %sn"
POSIX didn't require leading spaces to be added, so it's free for implementation to do what it want. There are different implementations of wc
, at least with OSX and wc
from heirloom tools chest, it added leading spaces to output.
$ /usr/5bin/wc -l /tmp/file
3 /tmp/file
GNU wc also add leading spaces when reading from standard in and without any options:
$ cat file | wc
5 5 65
To remove all leading spaces, in POSIX shell:
set -f
set -- $nl
nl=$1
set +f
Note that this approach assume that variable only contain leading or trailing spaces, no spaces in the middle, like a b.
add a comment |Â
up vote
4
down vote
up vote
4
down vote
As POSIX defined, the output of wc
shall contain an entry for each input file of the form:
"%d %d %d %sn", <newlines>, <words>, <bytes>, <file>
But the output file format pseudo printf()
string differs from the System V version of wc
:
"%7d%7d%7d %sn"
POSIX didn't require leading spaces to be added, so it's free for implementation to do what it want. There are different implementations of wc
, at least with OSX and wc
from heirloom tools chest, it added leading spaces to output.
$ /usr/5bin/wc -l /tmp/file
3 /tmp/file
GNU wc also add leading spaces when reading from standard in and without any options:
$ cat file | wc
5 5 65
To remove all leading spaces, in POSIX shell:
set -f
set -- $nl
nl=$1
set +f
Note that this approach assume that variable only contain leading or trailing spaces, no spaces in the middle, like a b.
As POSIX defined, the output of wc
shall contain an entry for each input file of the form:
"%d %d %d %sn", <newlines>, <words>, <bytes>, <file>
But the output file format pseudo printf()
string differs from the System V version of wc
:
"%7d%7d%7d %sn"
POSIX didn't require leading spaces to be added, so it's free for implementation to do what it want. There are different implementations of wc
, at least with OSX and wc
from heirloom tools chest, it added leading spaces to output.
$ /usr/5bin/wc -l /tmp/file
3 /tmp/file
GNU wc also add leading spaces when reading from standard in and without any options:
$ cat file | wc
5 5 65
To remove all leading spaces, in POSIX shell:
set -f
set -- $nl
nl=$1
set +f
Note that this approach assume that variable only contain leading or trailing spaces, no spaces in the middle, like a b.
edited May 27 '15 at 16:45
answered May 27 '15 at 16:11
cuonglm
98.6k21188285
98.6k21188285
add a comment |Â
add a comment |Â
up vote
4
down vote
wc
implementations may produce leading spaces; this may also be depending on whether (and how many) options are used (GNU wc
, for example, won't produce spaces if, as in your case, exactly one option is used). Those spaces are retained in the assignment nl=$(...)
. To remove the spaces, as a workaround, you could use either of:
nl=$(wc -l < ~/myfile.txt | awk 'print $1')
or
nl=$(awk 'ENDprint NR' ~/myfile.txt)
or
nl=$(wc -l < ~/Desktop/cap.xlsx)
nl=$(echo $nl)
or (but see @cuonglm's comment below for heirloom's version of wc
)
nl=$(wc -l < ~/Desktop/cap.xlsx)
nl=$nl##*
@cuonglm; Thanks for spotting that; I changed the shell regexp now fromnl=$nl## *
tonl=$nl##*
.
â Janis
May 27 '15 at 16:40
It still fail with heirloomwc
, it produce trailing spaces, too. So you still remove the entire variable.
â cuonglm
May 27 '15 at 16:42
The heirloomwc
produces trailing spaces? - Okay. I'll leave that variant still as one option. With your comment here the folks now know that in heirloom context they should apply one of the other suggestions.
â Janis
May 27 '15 at 16:46
add a comment |Â
up vote
4
down vote
wc
implementations may produce leading spaces; this may also be depending on whether (and how many) options are used (GNU wc
, for example, won't produce spaces if, as in your case, exactly one option is used). Those spaces are retained in the assignment nl=$(...)
. To remove the spaces, as a workaround, you could use either of:
nl=$(wc -l < ~/myfile.txt | awk 'print $1')
or
nl=$(awk 'ENDprint NR' ~/myfile.txt)
or
nl=$(wc -l < ~/Desktop/cap.xlsx)
nl=$(echo $nl)
or (but see @cuonglm's comment below for heirloom's version of wc
)
nl=$(wc -l < ~/Desktop/cap.xlsx)
nl=$nl##*
@cuonglm; Thanks for spotting that; I changed the shell regexp now fromnl=$nl## *
tonl=$nl##*
.
â Janis
May 27 '15 at 16:40
It still fail with heirloomwc
, it produce trailing spaces, too. So you still remove the entire variable.
â cuonglm
May 27 '15 at 16:42
The heirloomwc
produces trailing spaces? - Okay. I'll leave that variant still as one option. With your comment here the folks now know that in heirloom context they should apply one of the other suggestions.
â Janis
May 27 '15 at 16:46
add a comment |Â
up vote
4
down vote
up vote
4
down vote
wc
implementations may produce leading spaces; this may also be depending on whether (and how many) options are used (GNU wc
, for example, won't produce spaces if, as in your case, exactly one option is used). Those spaces are retained in the assignment nl=$(...)
. To remove the spaces, as a workaround, you could use either of:
nl=$(wc -l < ~/myfile.txt | awk 'print $1')
or
nl=$(awk 'ENDprint NR' ~/myfile.txt)
or
nl=$(wc -l < ~/Desktop/cap.xlsx)
nl=$(echo $nl)
or (but see @cuonglm's comment below for heirloom's version of wc
)
nl=$(wc -l < ~/Desktop/cap.xlsx)
nl=$nl##*
wc
implementations may produce leading spaces; this may also be depending on whether (and how many) options are used (GNU wc
, for example, won't produce spaces if, as in your case, exactly one option is used). Those spaces are retained in the assignment nl=$(...)
. To remove the spaces, as a workaround, you could use either of:
nl=$(wc -l < ~/myfile.txt | awk 'print $1')
or
nl=$(awk 'ENDprint NR' ~/myfile.txt)
or
nl=$(wc -l < ~/Desktop/cap.xlsx)
nl=$(echo $nl)
or (but see @cuonglm's comment below for heirloom's version of wc
)
nl=$(wc -l < ~/Desktop/cap.xlsx)
nl=$nl##*
edited May 27 '15 at 16:54
answered May 27 '15 at 16:05
Janis
9,79011236
9,79011236
@cuonglm; Thanks for spotting that; I changed the shell regexp now fromnl=$nl## *
tonl=$nl##*
.
â Janis
May 27 '15 at 16:40
It still fail with heirloomwc
, it produce trailing spaces, too. So you still remove the entire variable.
â cuonglm
May 27 '15 at 16:42
The heirloomwc
produces trailing spaces? - Okay. I'll leave that variant still as one option. With your comment here the folks now know that in heirloom context they should apply one of the other suggestions.
â Janis
May 27 '15 at 16:46
add a comment |Â
@cuonglm; Thanks for spotting that; I changed the shell regexp now fromnl=$nl## *
tonl=$nl##*
.
â Janis
May 27 '15 at 16:40
It still fail with heirloomwc
, it produce trailing spaces, too. So you still remove the entire variable.
â cuonglm
May 27 '15 at 16:42
The heirloomwc
produces trailing spaces? - Okay. I'll leave that variant still as one option. With your comment here the folks now know that in heirloom context they should apply one of the other suggestions.
â Janis
May 27 '15 at 16:46
@cuonglm; Thanks for spotting that; I changed the shell regexp now from
nl=$nl## *
to nl=$nl##*
.â Janis
May 27 '15 at 16:40
@cuonglm; Thanks for spotting that; I changed the shell regexp now from
nl=$nl## *
to nl=$nl##*
.â Janis
May 27 '15 at 16:40
It still fail with heirloom
wc
, it produce trailing spaces, too. So you still remove the entire variable.â cuonglm
May 27 '15 at 16:42
It still fail with heirloom
wc
, it produce trailing spaces, too. So you still remove the entire variable.â cuonglm
May 27 '15 at 16:42
The heirloom
wc
produces trailing spaces? - Okay. I'll leave that variant still as one option. With your comment here the folks now know that in heirloom context they should apply one of the other suggestions.â Janis
May 27 '15 at 16:46
The heirloom
wc
produces trailing spaces? - Okay. I'll leave that variant still as one option. With your comment here the folks now know that in heirloom context they should apply one of the other suggestions.â Janis
May 27 '15 at 16:46
add a comment |Â
up vote
1
down vote
Simpliest workaround (in bash or any other POSIX shell):
nl=$(wc -l < ~/myfile.txt)
nl=$(($nl))
$((...))
in standard sh
is for arithmetic expansion, so the result will always be only the number itself. Note that if wc -l
produces no output (like when the file can't be read), that will yield 0
instead of an empty string.
$((nl))
would also work in bash
but is not guaranteed to by POSIX as $nl
doesn't contain a numerical constant, and in practice doesn't work in yash
for instance.
You can also do it all at once with:
nl=$(($(wc -l < ~/myfile.txt)))
Which has the benefit of preserving the exit status of wc
.
add a comment |Â
up vote
1
down vote
Simpliest workaround (in bash or any other POSIX shell):
nl=$(wc -l < ~/myfile.txt)
nl=$(($nl))
$((...))
in standard sh
is for arithmetic expansion, so the result will always be only the number itself. Note that if wc -l
produces no output (like when the file can't be read), that will yield 0
instead of an empty string.
$((nl))
would also work in bash
but is not guaranteed to by POSIX as $nl
doesn't contain a numerical constant, and in practice doesn't work in yash
for instance.
You can also do it all at once with:
nl=$(($(wc -l < ~/myfile.txt)))
Which has the benefit of preserving the exit status of wc
.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Simpliest workaround (in bash or any other POSIX shell):
nl=$(wc -l < ~/myfile.txt)
nl=$(($nl))
$((...))
in standard sh
is for arithmetic expansion, so the result will always be only the number itself. Note that if wc -l
produces no output (like when the file can't be read), that will yield 0
instead of an empty string.
$((nl))
would also work in bash
but is not guaranteed to by POSIX as $nl
doesn't contain a numerical constant, and in practice doesn't work in yash
for instance.
You can also do it all at once with:
nl=$(($(wc -l < ~/myfile.txt)))
Which has the benefit of preserving the exit status of wc
.
Simpliest workaround (in bash or any other POSIX shell):
nl=$(wc -l < ~/myfile.txt)
nl=$(($nl))
$((...))
in standard sh
is for arithmetic expansion, so the result will always be only the number itself. Note that if wc -l
produces no output (like when the file can't be read), that will yield 0
instead of an empty string.
$((nl))
would also work in bash
but is not guaranteed to by POSIX as $nl
doesn't contain a numerical constant, and in practice doesn't work in yash
for instance.
You can also do it all at once with:
nl=$(($(wc -l < ~/myfile.txt)))
Which has the benefit of preserving the exit status of wc
.
edited Sep 10 at 13:02
Stéphane Chazelas
286k53528867
286k53528867
answered Sep 9 at 16:14
Clément Moulin - SimpleRezo
91
91
add a comment |Â
add a comment |Â
up vote
1
down vote
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.
@StéphaneChazelas Why is sed counts the last line even if it doesn't end in a newline character not enough for you? (hmmm, for reasonably clean text files)
â Isaac
Sep 11 at 7:33
Sorry, I had overlooked that part.
â Stéphane Chazelas
Sep 11 at 8:12
add a comment |Â
up vote
1
down vote
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.
@StéphaneChazelas Why is sed counts the last line even if it doesn't end in a newline character not enough for you? (hmmm, for reasonably clean text files)
â Isaac
Sep 11 at 7:33
Sorry, I had overlooked that part.
â Stéphane Chazelas
Sep 11 at 8:12
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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.
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.
edited Sep 11 at 4:57
answered Sep 10 at 1:52
Isaac
7,28911035
7,28911035
@StéphaneChazelas Why is sed counts the last line even if it doesn't end in a newline character not enough for you? (hmmm, for reasonably clean text files)
â Isaac
Sep 11 at 7:33
Sorry, I had overlooked that part.
â Stéphane Chazelas
Sep 11 at 8:12
add a comment |Â
@StéphaneChazelas Why is sed counts the last line even if it doesn't end in a newline character not enough for you? (hmmm, for reasonably clean text files)
â Isaac
Sep 11 at 7:33
Sorry, I had overlooked that part.
â Stéphane Chazelas
Sep 11 at 8:12
@StéphaneChazelas Why is sed counts the last line even if it doesn't end in a newline character not enough for you? (hmmm, for reasonably clean text files)
â Isaac
Sep 11 at 7:33
@StéphaneChazelas Why is sed counts the last line even if it doesn't end in a newline character not enough for you? (hmmm, for reasonably clean text files)
â Isaac
Sep 11 at 7:33
Sorry, I had overlooked that part.
â Stéphane Chazelas
Sep 11 at 8:12
Sorry, I had overlooked that part.
â Stéphane Chazelas
Sep 11 at 8:12
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f205906%2fextra-space-with-counted-line-number%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
What OS are you on? I can't reproduce this on my Debian.
â terdonâ¦
May 27 '15 at 15:47
@terdon I am on OS X. Please see the updates.
â Sibbs Gambling
May 27 '15 at 15:49
1
Weird. Make few tests to narrow the problem: 1) set value directly
nl=100
; 2) try different delimiter, not-
; 3) put double quotes around expressionecho "1-$nl"
; 4) useprintf
instead ofecho
â jimmij
May 27 '15 at 16:03
@Sibbs; I either cannot reproduce it. What output do you get when quoting
echo $nl
asecho "$nl"
.â Janis
May 27 '15 at 16:03
@Janis I can't format that properly here. Please see the screenshot above.
â Sibbs Gambling
May 27 '15 at 16:12