What does cut return if the specified field does not exist?
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I have a string:
6.40.4
and a second one:
6.40
I am using var=$(echo $versionfull | cut -d'.' -f3)
to get the third digit from the first string in bash. What does this command return for the second one? It looks empty but either [ -z $var ]
or [ $var == "" ]
does not work. I want to give it a value of 0
in case of a second string.
text-processing cut
add a comment |Â
up vote
3
down vote
favorite
I have a string:
6.40.4
and a second one:
6.40
I am using var=$(echo $versionfull | cut -d'.' -f3)
to get the third digit from the first string in bash. What does this command return for the second one? It looks empty but either [ -z $var ]
or [ $var == "" ]
does not work. I want to give it a value of 0
in case of a second string.
text-processing cut
2
Pipe it tood
to see what it returns...
â don_crissti
Aug 29 at 19:28
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I have a string:
6.40.4
and a second one:
6.40
I am using var=$(echo $versionfull | cut -d'.' -f3)
to get the third digit from the first string in bash. What does this command return for the second one? It looks empty but either [ -z $var ]
or [ $var == "" ]
does not work. I want to give it a value of 0
in case of a second string.
text-processing cut
I have a string:
6.40.4
and a second one:
6.40
I am using var=$(echo $versionfull | cut -d'.' -f3)
to get the third digit from the first string in bash. What does this command return for the second one? It looks empty but either [ -z $var ]
or [ $var == "" ]
does not work. I want to give it a value of 0
in case of a second string.
text-processing cut
edited Aug 29 at 20:21
Jeff Schaller
32.1k849109
32.1k849109
asked Aug 29 at 19:17
Kliwer
182
182
2
Pipe it tood
to see what it returns...
â don_crissti
Aug 29 at 19:28
add a comment |Â
2
Pipe it tood
to see what it returns...
â don_crissti
Aug 29 at 19:28
2
2
Pipe it to
od
to see what it returns...â don_crissti
Aug 29 at 19:28
Pipe it to
od
to see what it returns...â don_crissti
Aug 29 at 19:28
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
8
down vote
accepted
You could use var=$(echo "$versionfull.0" | cut -d'.' -f3)
.
In the first case, versionfull
will contain 6.40.4.0, ignoring the padding and returning 4 as needed. In the second case, the .0
will be padded and returned.
Thanks. This is simple and works exactly like I want it to.
â Kliwer
Aug 29 at 19:39
3
It's sad that this is marked as the solution because it doesn't even answer the question, which is what everyone will expect to find when they come here in the future with the same question. Maybe the question should be changed to reflect that OP doesn't actually care about what it returns.
â pipe
Aug 30 at 9:28
You are right, it does not answers the question, but it solves OP problem. Is tailored for this only case, but that's all OP wants - solve his problem.
â ThoriumBR
Aug 30 at 11:43
add a comment |Â
up vote
10
down vote
To answer your direct question, cut
returns nothing except a trailing newline, as per the spec (thanks to don_crissti for that reference):
echo "$versionfull" | cut -d. -f3 | od -c
0000000 n
0000001
If you have a shell that supports here-strings, you could do the following:
IFS=. read a b c <<< "$versionfull"
Notice the quoting for $versionfull
, in case it ever had whitespace (anything from $IFS
).
If you think that c
might be empty, then ask to set it to zero:
c=$c:-0
2
Note that the need to quote inIFS=. read a b c <<< "$versionfull"
was only for older versions ofbash
; other shells and newer versions ofbash
don't have the problem.
â Stéphane Chazelas
Aug 29 at 19:43
Note that if the input has only one field, cut returns that field.
â Stéphane Chazelas
Aug 29 at 20:05
add a comment |Â
up vote
6
down vote
cut
has an odd API.
cut -f n
will output the nth field of each line that has at least one delimiter (empty if the line has fewer than n-1 delimiters (fewer than n fields)), and returns the full line (so the first field) for those that don't have any delimiter:
$ echo a.b.c | cut -d. -f3
c
$ echo a.b | cut -d. -f3
$ echo a | cut -d. -f3
a
So the answer to What does cut return if the specified field does not exist? is either the first field or an empty field depending on whether the input line has one field or more.
You can add the -s
option to remove the lines that don't have any delimiter to avoid that weird last case above, but that's generally not what you want (you'd generally want to consider that input line to have an empty 3rd field instead of skipping it altogether), and that's worth if you want the first field:
$ echo a | cut -sd. -f2
$ echo a | cut -sd. -f1
$
(you asked for the first field, the input has only one field, but you don't get any output because the input has no delimiter).
So @ThoriumBR's suggestion to add a .0
is a very good one. Without it:
versionfull=5
echo "$versionfull" | cut -d. -f3
would actually output 5. By adding .0
, we make sure the input has at least one delimiter. I would go further and use:
echo "$versionfull.0.0" | cut -d. -f3
To make sure the input has at least 3 fields.
The probability of version 7 in Mikrotik is low ;) Anyway, thanks - I'll do that.
â Kliwer
Aug 29 at 20:11
How does this relate to his second case? The input has a delimiter.
â Barmar
Aug 29 at 22:59
add a comment |Â
up vote
1
down vote
It sets it to an empty string. You should quote the variable when using it with the [
command.
if [ "$var" == "" ]
then echo "Var is empty"
fi
Without the quotes, the first line expands to:
if [ == "" ]
which is invalid syntax because ==
requires a parameter to the left of it.
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
You could use var=$(echo "$versionfull.0" | cut -d'.' -f3)
.
In the first case, versionfull
will contain 6.40.4.0, ignoring the padding and returning 4 as needed. In the second case, the .0
will be padded and returned.
Thanks. This is simple and works exactly like I want it to.
â Kliwer
Aug 29 at 19:39
3
It's sad that this is marked as the solution because it doesn't even answer the question, which is what everyone will expect to find when they come here in the future with the same question. Maybe the question should be changed to reflect that OP doesn't actually care about what it returns.
â pipe
Aug 30 at 9:28
You are right, it does not answers the question, but it solves OP problem. Is tailored for this only case, but that's all OP wants - solve his problem.
â ThoriumBR
Aug 30 at 11:43
add a comment |Â
up vote
8
down vote
accepted
You could use var=$(echo "$versionfull.0" | cut -d'.' -f3)
.
In the first case, versionfull
will contain 6.40.4.0, ignoring the padding and returning 4 as needed. In the second case, the .0
will be padded and returned.
Thanks. This is simple and works exactly like I want it to.
â Kliwer
Aug 29 at 19:39
3
It's sad that this is marked as the solution because it doesn't even answer the question, which is what everyone will expect to find when they come here in the future with the same question. Maybe the question should be changed to reflect that OP doesn't actually care about what it returns.
â pipe
Aug 30 at 9:28
You are right, it does not answers the question, but it solves OP problem. Is tailored for this only case, but that's all OP wants - solve his problem.
â ThoriumBR
Aug 30 at 11:43
add a comment |Â
up vote
8
down vote
accepted
up vote
8
down vote
accepted
You could use var=$(echo "$versionfull.0" | cut -d'.' -f3)
.
In the first case, versionfull
will contain 6.40.4.0, ignoring the padding and returning 4 as needed. In the second case, the .0
will be padded and returned.
You could use var=$(echo "$versionfull.0" | cut -d'.' -f3)
.
In the first case, versionfull
will contain 6.40.4.0, ignoring the padding and returning 4 as needed. In the second case, the .0
will be padded and returned.
edited Aug 29 at 20:09
Stéphane Chazelas
283k53521856
283k53521856
answered Aug 29 at 19:26
ThoriumBR
1984
1984
Thanks. This is simple and works exactly like I want it to.
â Kliwer
Aug 29 at 19:39
3
It's sad that this is marked as the solution because it doesn't even answer the question, which is what everyone will expect to find when they come here in the future with the same question. Maybe the question should be changed to reflect that OP doesn't actually care about what it returns.
â pipe
Aug 30 at 9:28
You are right, it does not answers the question, but it solves OP problem. Is tailored for this only case, but that's all OP wants - solve his problem.
â ThoriumBR
Aug 30 at 11:43
add a comment |Â
Thanks. This is simple and works exactly like I want it to.
â Kliwer
Aug 29 at 19:39
3
It's sad that this is marked as the solution because it doesn't even answer the question, which is what everyone will expect to find when they come here in the future with the same question. Maybe the question should be changed to reflect that OP doesn't actually care about what it returns.
â pipe
Aug 30 at 9:28
You are right, it does not answers the question, but it solves OP problem. Is tailored for this only case, but that's all OP wants - solve his problem.
â ThoriumBR
Aug 30 at 11:43
Thanks. This is simple and works exactly like I want it to.
â Kliwer
Aug 29 at 19:39
Thanks. This is simple and works exactly like I want it to.
â Kliwer
Aug 29 at 19:39
3
3
It's sad that this is marked as the solution because it doesn't even answer the question, which is what everyone will expect to find when they come here in the future with the same question. Maybe the question should be changed to reflect that OP doesn't actually care about what it returns.
â pipe
Aug 30 at 9:28
It's sad that this is marked as the solution because it doesn't even answer the question, which is what everyone will expect to find when they come here in the future with the same question. Maybe the question should be changed to reflect that OP doesn't actually care about what it returns.
â pipe
Aug 30 at 9:28
You are right, it does not answers the question, but it solves OP problem. Is tailored for this only case, but that's all OP wants - solve his problem.
â ThoriumBR
Aug 30 at 11:43
You are right, it does not answers the question, but it solves OP problem. Is tailored for this only case, but that's all OP wants - solve his problem.
â ThoriumBR
Aug 30 at 11:43
add a comment |Â
up vote
10
down vote
To answer your direct question, cut
returns nothing except a trailing newline, as per the spec (thanks to don_crissti for that reference):
echo "$versionfull" | cut -d. -f3 | od -c
0000000 n
0000001
If you have a shell that supports here-strings, you could do the following:
IFS=. read a b c <<< "$versionfull"
Notice the quoting for $versionfull
, in case it ever had whitespace (anything from $IFS
).
If you think that c
might be empty, then ask to set it to zero:
c=$c:-0
2
Note that the need to quote inIFS=. read a b c <<< "$versionfull"
was only for older versions ofbash
; other shells and newer versions ofbash
don't have the problem.
â Stéphane Chazelas
Aug 29 at 19:43
Note that if the input has only one field, cut returns that field.
â Stéphane Chazelas
Aug 29 at 20:05
add a comment |Â
up vote
10
down vote
To answer your direct question, cut
returns nothing except a trailing newline, as per the spec (thanks to don_crissti for that reference):
echo "$versionfull" | cut -d. -f3 | od -c
0000000 n
0000001
If you have a shell that supports here-strings, you could do the following:
IFS=. read a b c <<< "$versionfull"
Notice the quoting for $versionfull
, in case it ever had whitespace (anything from $IFS
).
If you think that c
might be empty, then ask to set it to zero:
c=$c:-0
2
Note that the need to quote inIFS=. read a b c <<< "$versionfull"
was only for older versions ofbash
; other shells and newer versions ofbash
don't have the problem.
â Stéphane Chazelas
Aug 29 at 19:43
Note that if the input has only one field, cut returns that field.
â Stéphane Chazelas
Aug 29 at 20:05
add a comment |Â
up vote
10
down vote
up vote
10
down vote
To answer your direct question, cut
returns nothing except a trailing newline, as per the spec (thanks to don_crissti for that reference):
echo "$versionfull" | cut -d. -f3 | od -c
0000000 n
0000001
If you have a shell that supports here-strings, you could do the following:
IFS=. read a b c <<< "$versionfull"
Notice the quoting for $versionfull
, in case it ever had whitespace (anything from $IFS
).
If you think that c
might be empty, then ask to set it to zero:
c=$c:-0
To answer your direct question, cut
returns nothing except a trailing newline, as per the spec (thanks to don_crissti for that reference):
echo "$versionfull" | cut -d. -f3 | od -c
0000000 n
0000001
If you have a shell that supports here-strings, you could do the following:
IFS=. read a b c <<< "$versionfull"
Notice the quoting for $versionfull
, in case it ever had whitespace (anything from $IFS
).
If you think that c
might be empty, then ask to set it to zero:
c=$c:-0
edited Aug 29 at 19:49
answered Aug 29 at 19:27
Jeff Schaller
32.1k849109
32.1k849109
2
Note that the need to quote inIFS=. read a b c <<< "$versionfull"
was only for older versions ofbash
; other shells and newer versions ofbash
don't have the problem.
â Stéphane Chazelas
Aug 29 at 19:43
Note that if the input has only one field, cut returns that field.
â Stéphane Chazelas
Aug 29 at 20:05
add a comment |Â
2
Note that the need to quote inIFS=. read a b c <<< "$versionfull"
was only for older versions ofbash
; other shells and newer versions ofbash
don't have the problem.
â Stéphane Chazelas
Aug 29 at 19:43
Note that if the input has only one field, cut returns that field.
â Stéphane Chazelas
Aug 29 at 20:05
2
2
Note that the need to quote in
IFS=. read a b c <<< "$versionfull"
was only for older versions of bash
; other shells and newer versions of bash
don't have the problem.â Stéphane Chazelas
Aug 29 at 19:43
Note that the need to quote in
IFS=. read a b c <<< "$versionfull"
was only for older versions of bash
; other shells and newer versions of bash
don't have the problem.â Stéphane Chazelas
Aug 29 at 19:43
Note that if the input has only one field, cut returns that field.
â Stéphane Chazelas
Aug 29 at 20:05
Note that if the input has only one field, cut returns that field.
â Stéphane Chazelas
Aug 29 at 20:05
add a comment |Â
up vote
6
down vote
cut
has an odd API.
cut -f n
will output the nth field of each line that has at least one delimiter (empty if the line has fewer than n-1 delimiters (fewer than n fields)), and returns the full line (so the first field) for those that don't have any delimiter:
$ echo a.b.c | cut -d. -f3
c
$ echo a.b | cut -d. -f3
$ echo a | cut -d. -f3
a
So the answer to What does cut return if the specified field does not exist? is either the first field or an empty field depending on whether the input line has one field or more.
You can add the -s
option to remove the lines that don't have any delimiter to avoid that weird last case above, but that's generally not what you want (you'd generally want to consider that input line to have an empty 3rd field instead of skipping it altogether), and that's worth if you want the first field:
$ echo a | cut -sd. -f2
$ echo a | cut -sd. -f1
$
(you asked for the first field, the input has only one field, but you don't get any output because the input has no delimiter).
So @ThoriumBR's suggestion to add a .0
is a very good one. Without it:
versionfull=5
echo "$versionfull" | cut -d. -f3
would actually output 5. By adding .0
, we make sure the input has at least one delimiter. I would go further and use:
echo "$versionfull.0.0" | cut -d. -f3
To make sure the input has at least 3 fields.
The probability of version 7 in Mikrotik is low ;) Anyway, thanks - I'll do that.
â Kliwer
Aug 29 at 20:11
How does this relate to his second case? The input has a delimiter.
â Barmar
Aug 29 at 22:59
add a comment |Â
up vote
6
down vote
cut
has an odd API.
cut -f n
will output the nth field of each line that has at least one delimiter (empty if the line has fewer than n-1 delimiters (fewer than n fields)), and returns the full line (so the first field) for those that don't have any delimiter:
$ echo a.b.c | cut -d. -f3
c
$ echo a.b | cut -d. -f3
$ echo a | cut -d. -f3
a
So the answer to What does cut return if the specified field does not exist? is either the first field or an empty field depending on whether the input line has one field or more.
You can add the -s
option to remove the lines that don't have any delimiter to avoid that weird last case above, but that's generally not what you want (you'd generally want to consider that input line to have an empty 3rd field instead of skipping it altogether), and that's worth if you want the first field:
$ echo a | cut -sd. -f2
$ echo a | cut -sd. -f1
$
(you asked for the first field, the input has only one field, but you don't get any output because the input has no delimiter).
So @ThoriumBR's suggestion to add a .0
is a very good one. Without it:
versionfull=5
echo "$versionfull" | cut -d. -f3
would actually output 5. By adding .0
, we make sure the input has at least one delimiter. I would go further and use:
echo "$versionfull.0.0" | cut -d. -f3
To make sure the input has at least 3 fields.
The probability of version 7 in Mikrotik is low ;) Anyway, thanks - I'll do that.
â Kliwer
Aug 29 at 20:11
How does this relate to his second case? The input has a delimiter.
â Barmar
Aug 29 at 22:59
add a comment |Â
up vote
6
down vote
up vote
6
down vote
cut
has an odd API.
cut -f n
will output the nth field of each line that has at least one delimiter (empty if the line has fewer than n-1 delimiters (fewer than n fields)), and returns the full line (so the first field) for those that don't have any delimiter:
$ echo a.b.c | cut -d. -f3
c
$ echo a.b | cut -d. -f3
$ echo a | cut -d. -f3
a
So the answer to What does cut return if the specified field does not exist? is either the first field or an empty field depending on whether the input line has one field or more.
You can add the -s
option to remove the lines that don't have any delimiter to avoid that weird last case above, but that's generally not what you want (you'd generally want to consider that input line to have an empty 3rd field instead of skipping it altogether), and that's worth if you want the first field:
$ echo a | cut -sd. -f2
$ echo a | cut -sd. -f1
$
(you asked for the first field, the input has only one field, but you don't get any output because the input has no delimiter).
So @ThoriumBR's suggestion to add a .0
is a very good one. Without it:
versionfull=5
echo "$versionfull" | cut -d. -f3
would actually output 5. By adding .0
, we make sure the input has at least one delimiter. I would go further and use:
echo "$versionfull.0.0" | cut -d. -f3
To make sure the input has at least 3 fields.
cut
has an odd API.
cut -f n
will output the nth field of each line that has at least one delimiter (empty if the line has fewer than n-1 delimiters (fewer than n fields)), and returns the full line (so the first field) for those that don't have any delimiter:
$ echo a.b.c | cut -d. -f3
c
$ echo a.b | cut -d. -f3
$ echo a | cut -d. -f3
a
So the answer to What does cut return if the specified field does not exist? is either the first field or an empty field depending on whether the input line has one field or more.
You can add the -s
option to remove the lines that don't have any delimiter to avoid that weird last case above, but that's generally not what you want (you'd generally want to consider that input line to have an empty 3rd field instead of skipping it altogether), and that's worth if you want the first field:
$ echo a | cut -sd. -f2
$ echo a | cut -sd. -f1
$
(you asked for the first field, the input has only one field, but you don't get any output because the input has no delimiter).
So @ThoriumBR's suggestion to add a .0
is a very good one. Without it:
versionfull=5
echo "$versionfull" | cut -d. -f3
would actually output 5. By adding .0
, we make sure the input has at least one delimiter. I would go further and use:
echo "$versionfull.0.0" | cut -d. -f3
To make sure the input has at least 3 fields.
edited Aug 30 at 6:40
answered Aug 29 at 20:02
Stéphane Chazelas
283k53521856
283k53521856
The probability of version 7 in Mikrotik is low ;) Anyway, thanks - I'll do that.
â Kliwer
Aug 29 at 20:11
How does this relate to his second case? The input has a delimiter.
â Barmar
Aug 29 at 22:59
add a comment |Â
The probability of version 7 in Mikrotik is low ;) Anyway, thanks - I'll do that.
â Kliwer
Aug 29 at 20:11
How does this relate to his second case? The input has a delimiter.
â Barmar
Aug 29 at 22:59
The probability of version 7 in Mikrotik is low ;) Anyway, thanks - I'll do that.
â Kliwer
Aug 29 at 20:11
The probability of version 7 in Mikrotik is low ;) Anyway, thanks - I'll do that.
â Kliwer
Aug 29 at 20:11
How does this relate to his second case? The input has a delimiter.
â Barmar
Aug 29 at 22:59
How does this relate to his second case? The input has a delimiter.
â Barmar
Aug 29 at 22:59
add a comment |Â
up vote
1
down vote
It sets it to an empty string. You should quote the variable when using it with the [
command.
if [ "$var" == "" ]
then echo "Var is empty"
fi
Without the quotes, the first line expands to:
if [ == "" ]
which is invalid syntax because ==
requires a parameter to the left of it.
add a comment |Â
up vote
1
down vote
It sets it to an empty string. You should quote the variable when using it with the [
command.
if [ "$var" == "" ]
then echo "Var is empty"
fi
Without the quotes, the first line expands to:
if [ == "" ]
which is invalid syntax because ==
requires a parameter to the left of it.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
It sets it to an empty string. You should quote the variable when using it with the [
command.
if [ "$var" == "" ]
then echo "Var is empty"
fi
Without the quotes, the first line expands to:
if [ == "" ]
which is invalid syntax because ==
requires a parameter to the left of it.
It sets it to an empty string. You should quote the variable when using it with the [
command.
if [ "$var" == "" ]
then echo "Var is empty"
fi
Without the quotes, the first line expands to:
if [ == "" ]
which is invalid syntax because ==
requires a parameter to the left of it.
answered Aug 29 at 23:03
Barmar
6,7201122
6,7201122
add a comment |Â
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%2f465583%2fwhat-does-cut-return-if-the-specified-field-does-not-exist%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
2
Pipe it to
od
to see what it returns...â don_crissti
Aug 29 at 19:28