Does readarray allow to specify line delimiter?
up vote
1
down vote
favorite
In bash read
builtin has -d
for us to specify line delimiter other than newline
Does readarray
provide some way to specify line delimiter?
- Is it correct that it has no an option for that purpose?
- Is there a shell default variable for that purpose, similar to
IFS
for field delimiter?
Thanks.
After I saw steeldriver's comment,
$ bash --version
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ help readarray
readarray: readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
Read lines from a file into an array variable.
A synonym for `mapfile'.
but later I discovered:
$ help mapfile
mapfile: mapfile [-d delim] [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
Read lines from the standard input into an indexed array variable.
Read lines from the standard input into the indexed array variable ARRAY, or
from file descriptor FD if the -u option is supplied. The variable MAPFILE
is the default ARRAY.
Options:
-d delim Use DELIM to terminate lines, instead of newline
...
bash array
add a comment |
up vote
1
down vote
favorite
In bash read
builtin has -d
for us to specify line delimiter other than newline
Does readarray
provide some way to specify line delimiter?
- Is it correct that it has no an option for that purpose?
- Is there a shell default variable for that purpose, similar to
IFS
for field delimiter?
Thanks.
After I saw steeldriver's comment,
$ bash --version
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ help readarray
readarray: readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
Read lines from a file into an array variable.
A synonym for `mapfile'.
but later I discovered:
$ help mapfile
mapfile: mapfile [-d delim] [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
Read lines from the standard input into an indexed array variable.
Read lines from the standard input into the indexed array variable ARRAY, or
from file descriptor FD if the -u option is supplied. The variable MAPFILE
is the default ARRAY.
Options:
-d delim Use DELIM to terminate lines, instead of newline
...
bash array
1
It depends how recent a version of bash (it was introduced in 4.4 IIRC) - a simple perusal of the manpage should indicate whether yours does (-d delim
)
– steeldriver
2 days ago
Thanks. Updated.
– Tim
2 days ago
1
@JeffSchaller "What happened when you tried?"
– Tim
2 days ago
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
In bash read
builtin has -d
for us to specify line delimiter other than newline
Does readarray
provide some way to specify line delimiter?
- Is it correct that it has no an option for that purpose?
- Is there a shell default variable for that purpose, similar to
IFS
for field delimiter?
Thanks.
After I saw steeldriver's comment,
$ bash --version
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ help readarray
readarray: readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
Read lines from a file into an array variable.
A synonym for `mapfile'.
but later I discovered:
$ help mapfile
mapfile: mapfile [-d delim] [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
Read lines from the standard input into an indexed array variable.
Read lines from the standard input into the indexed array variable ARRAY, or
from file descriptor FD if the -u option is supplied. The variable MAPFILE
is the default ARRAY.
Options:
-d delim Use DELIM to terminate lines, instead of newline
...
bash array
In bash read
builtin has -d
for us to specify line delimiter other than newline
Does readarray
provide some way to specify line delimiter?
- Is it correct that it has no an option for that purpose?
- Is there a shell default variable for that purpose, similar to
IFS
for field delimiter?
Thanks.
After I saw steeldriver's comment,
$ bash --version
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ help readarray
readarray: readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
Read lines from a file into an array variable.
A synonym for `mapfile'.
but later I discovered:
$ help mapfile
mapfile: mapfile [-d delim] [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
Read lines from the standard input into an indexed array variable.
Read lines from the standard input into the indexed array variable ARRAY, or
from file descriptor FD if the -u option is supplied. The variable MAPFILE
is the default ARRAY.
Options:
-d delim Use DELIM to terminate lines, instead of newline
...
bash array
bash array
edited 2 days ago
asked 2 days ago
Tim
1
1
1
It depends how recent a version of bash (it was introduced in 4.4 IIRC) - a simple perusal of the manpage should indicate whether yours does (-d delim
)
– steeldriver
2 days ago
Thanks. Updated.
– Tim
2 days ago
1
@JeffSchaller "What happened when you tried?"
– Tim
2 days ago
add a comment |
1
It depends how recent a version of bash (it was introduced in 4.4 IIRC) - a simple perusal of the manpage should indicate whether yours does (-d delim
)
– steeldriver
2 days ago
Thanks. Updated.
– Tim
2 days ago
1
@JeffSchaller "What happened when you tried?"
– Tim
2 days ago
1
1
It depends how recent a version of bash (it was introduced in 4.4 IIRC) - a simple perusal of the manpage should indicate whether yours does (
-d delim
)– steeldriver
2 days ago
It depends how recent a version of bash (it was introduced in 4.4 IIRC) - a simple perusal of the manpage should indicate whether yours does (
-d delim
)– steeldriver
2 days ago
Thanks. Updated.
– Tim
2 days ago
Thanks. Updated.
– Tim
2 days ago
1
1
@JeffSchaller "What happened when you tried?"
– Tim
2 days ago
@JeffSchaller "What happened when you tried?"
– Tim
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Since bash version 4.4, yes it does. From the release announcement Bash-4.4 Release Available:
The most notable new features are mapfile's ability to use an arbitrary
record delimiter;
(readarray
being a synonym for mapfile
). The description in man bash
is
-d The first character of delim is used to terminate each
input line, rather than newline.
Thanks. Does delimiter mean the same as a separator and thus is to be removed? I have a file whose content is1 2 3
. andreadarray -d "2" myarr < f1
andecho "${myarr[0]}"
outputs1 2
andecho "${myarr[1]}"
outputs3
.
– Tim
2 days ago
@Tim you can add-t
if you want to "Remove a trailing DELIM" - exactly as you would for the default (newline) delimiter
– steeldriver
2 days ago
In1 2 3
,2
isn't trailing however. What does "trailing" mean?
– Tim
2 days ago
1
@Tim To remove the trailing delimiter means to remove the delimiter from each element of the array. In your case, that would result in the two elements1
and3
(the1
would have a space after it and the3
would be preceded by a space and probably followed by a newline).
– Kusalananda
2 days ago
2
@Tim There is no mentioning of "separator". The delimiter (newline, or the character specified with-d
) is included in the data unless-t
is also used. See also When to use the terms "delimiter," "terminator," and "separator"
– Kusalananda
2 days ago
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Since bash version 4.4, yes it does. From the release announcement Bash-4.4 Release Available:
The most notable new features are mapfile's ability to use an arbitrary
record delimiter;
(readarray
being a synonym for mapfile
). The description in man bash
is
-d The first character of delim is used to terminate each
input line, rather than newline.
Thanks. Does delimiter mean the same as a separator and thus is to be removed? I have a file whose content is1 2 3
. andreadarray -d "2" myarr < f1
andecho "${myarr[0]}"
outputs1 2
andecho "${myarr[1]}"
outputs3
.
– Tim
2 days ago
@Tim you can add-t
if you want to "Remove a trailing DELIM" - exactly as you would for the default (newline) delimiter
– steeldriver
2 days ago
In1 2 3
,2
isn't trailing however. What does "trailing" mean?
– Tim
2 days ago
1
@Tim To remove the trailing delimiter means to remove the delimiter from each element of the array. In your case, that would result in the two elements1
and3
(the1
would have a space after it and the3
would be preceded by a space and probably followed by a newline).
– Kusalananda
2 days ago
2
@Tim There is no mentioning of "separator". The delimiter (newline, or the character specified with-d
) is included in the data unless-t
is also used. See also When to use the terms "delimiter," "terminator," and "separator"
– Kusalananda
2 days ago
|
show 1 more comment
up vote
3
down vote
accepted
Since bash version 4.4, yes it does. From the release announcement Bash-4.4 Release Available:
The most notable new features are mapfile's ability to use an arbitrary
record delimiter;
(readarray
being a synonym for mapfile
). The description in man bash
is
-d The first character of delim is used to terminate each
input line, rather than newline.
Thanks. Does delimiter mean the same as a separator and thus is to be removed? I have a file whose content is1 2 3
. andreadarray -d "2" myarr < f1
andecho "${myarr[0]}"
outputs1 2
andecho "${myarr[1]}"
outputs3
.
– Tim
2 days ago
@Tim you can add-t
if you want to "Remove a trailing DELIM" - exactly as you would for the default (newline) delimiter
– steeldriver
2 days ago
In1 2 3
,2
isn't trailing however. What does "trailing" mean?
– Tim
2 days ago
1
@Tim To remove the trailing delimiter means to remove the delimiter from each element of the array. In your case, that would result in the two elements1
and3
(the1
would have a space after it and the3
would be preceded by a space and probably followed by a newline).
– Kusalananda
2 days ago
2
@Tim There is no mentioning of "separator". The delimiter (newline, or the character specified with-d
) is included in the data unless-t
is also used. See also When to use the terms "delimiter," "terminator," and "separator"
– Kusalananda
2 days ago
|
show 1 more comment
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Since bash version 4.4, yes it does. From the release announcement Bash-4.4 Release Available:
The most notable new features are mapfile's ability to use an arbitrary
record delimiter;
(readarray
being a synonym for mapfile
). The description in man bash
is
-d The first character of delim is used to terminate each
input line, rather than newline.
Since bash version 4.4, yes it does. From the release announcement Bash-4.4 Release Available:
The most notable new features are mapfile's ability to use an arbitrary
record delimiter;
(readarray
being a synonym for mapfile
). The description in man bash
is
-d The first character of delim is used to terminate each
input line, rather than newline.
answered 2 days ago
steeldriver
33.6k34982
33.6k34982
Thanks. Does delimiter mean the same as a separator and thus is to be removed? I have a file whose content is1 2 3
. andreadarray -d "2" myarr < f1
andecho "${myarr[0]}"
outputs1 2
andecho "${myarr[1]}"
outputs3
.
– Tim
2 days ago
@Tim you can add-t
if you want to "Remove a trailing DELIM" - exactly as you would for the default (newline) delimiter
– steeldriver
2 days ago
In1 2 3
,2
isn't trailing however. What does "trailing" mean?
– Tim
2 days ago
1
@Tim To remove the trailing delimiter means to remove the delimiter from each element of the array. In your case, that would result in the two elements1
and3
(the1
would have a space after it and the3
would be preceded by a space and probably followed by a newline).
– Kusalananda
2 days ago
2
@Tim There is no mentioning of "separator". The delimiter (newline, or the character specified with-d
) is included in the data unless-t
is also used. See also When to use the terms "delimiter," "terminator," and "separator"
– Kusalananda
2 days ago
|
show 1 more comment
Thanks. Does delimiter mean the same as a separator and thus is to be removed? I have a file whose content is1 2 3
. andreadarray -d "2" myarr < f1
andecho "${myarr[0]}"
outputs1 2
andecho "${myarr[1]}"
outputs3
.
– Tim
2 days ago
@Tim you can add-t
if you want to "Remove a trailing DELIM" - exactly as you would for the default (newline) delimiter
– steeldriver
2 days ago
In1 2 3
,2
isn't trailing however. What does "trailing" mean?
– Tim
2 days ago
1
@Tim To remove the trailing delimiter means to remove the delimiter from each element of the array. In your case, that would result in the two elements1
and3
(the1
would have a space after it and the3
would be preceded by a space and probably followed by a newline).
– Kusalananda
2 days ago
2
@Tim There is no mentioning of "separator". The delimiter (newline, or the character specified with-d
) is included in the data unless-t
is also used. See also When to use the terms "delimiter," "terminator," and "separator"
– Kusalananda
2 days ago
Thanks. Does delimiter mean the same as a separator and thus is to be removed? I have a file whose content is
1 2 3
. and readarray -d "2" myarr < f1
and echo "${myarr[0]}"
outputs 1 2
and echo "${myarr[1]}"
outputs 3
.– Tim
2 days ago
Thanks. Does delimiter mean the same as a separator and thus is to be removed? I have a file whose content is
1 2 3
. and readarray -d "2" myarr < f1
and echo "${myarr[0]}"
outputs 1 2
and echo "${myarr[1]}"
outputs 3
.– Tim
2 days ago
@Tim you can add
-t
if you want to "Remove a trailing DELIM" - exactly as you would for the default (newline) delimiter– steeldriver
2 days ago
@Tim you can add
-t
if you want to "Remove a trailing DELIM" - exactly as you would for the default (newline) delimiter– steeldriver
2 days ago
In
1 2 3
, 2
isn't trailing however. What does "trailing" mean?– Tim
2 days ago
In
1 2 3
, 2
isn't trailing however. What does "trailing" mean?– Tim
2 days ago
1
1
@Tim To remove the trailing delimiter means to remove the delimiter from each element of the array. In your case, that would result in the two elements
1
and 3
(the 1
would have a space after it and the 3
would be preceded by a space and probably followed by a newline).– Kusalananda
2 days ago
@Tim To remove the trailing delimiter means to remove the delimiter from each element of the array. In your case, that would result in the two elements
1
and 3
(the 1
would have a space after it and the 3
would be preceded by a space and probably followed by a newline).– Kusalananda
2 days ago
2
2
@Tim There is no mentioning of "separator". The delimiter (newline, or the character specified with
-d
) is included in the data unless -t
is also used. See also When to use the terms "delimiter," "terminator," and "separator"– Kusalananda
2 days ago
@Tim There is no mentioning of "separator". The delimiter (newline, or the character specified with
-d
) is included in the data unless -t
is also used. See also When to use the terms "delimiter," "terminator," and "separator"– Kusalananda
2 days ago
|
show 1 more 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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482358%2fdoes-readarray-allow-to-specify-line-delimiter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
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
1
It depends how recent a version of bash (it was introduced in 4.4 IIRC) - a simple perusal of the manpage should indicate whether yours does (
-d delim
)– steeldriver
2 days ago
Thanks. Updated.
– Tim
2 days ago
1
@JeffSchaller "What happened when you tried?"
– Tim
2 days ago