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
Nov 17 at 16:30
Thanks. Updated.
– Tim
Nov 17 at 16:32
1
@JeffSchaller "What happened when you tried?"
– Tim
Nov 17 at 16:39
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 Nov 17 at 16:45
asked Nov 17 at 16:15
Tim
25.1k72243444
25.1k72243444
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
Nov 17 at 16:30
Thanks. Updated.
– Tim
Nov 17 at 16:32
1
@JeffSchaller "What happened when you tried?"
– Tim
Nov 17 at 16:39
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
Nov 17 at 16:30
Thanks. Updated.
– Tim
Nov 17 at 16:32
1
@JeffSchaller "What happened when you tried?"
– Tim
Nov 17 at 16:39
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
Nov 17 at 16:30
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
Nov 17 at 16:30
Thanks. Updated.
– Tim
Nov 17 at 16:32
Thanks. Updated.
– Tim
Nov 17 at 16:32
1
1
@JeffSchaller "What happened when you tried?"
– Tim
Nov 17 at 16:39
@JeffSchaller "What happened when you tried?"
– Tim
Nov 17 at 16:39
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
Nov 17 at 16:49
@Tim you can add-t
if you want to "Remove a trailing DELIM" - exactly as you would for the default (newline) delimiter
– steeldriver
Nov 17 at 16:55
In1 2 3
,2
isn't trailing however. What does "trailing" mean?
– Tim
Nov 17 at 17:07
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
Nov 17 at 17:36
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
Nov 17 at 17:44
|
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
Nov 17 at 16:49
@Tim you can add-t
if you want to "Remove a trailing DELIM" - exactly as you would for the default (newline) delimiter
– steeldriver
Nov 17 at 16:55
In1 2 3
,2
isn't trailing however. What does "trailing" mean?
– Tim
Nov 17 at 17:07
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
Nov 17 at 17:36
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
Nov 17 at 17:44
|
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
Nov 17 at 16:49
@Tim you can add-t
if you want to "Remove a trailing DELIM" - exactly as you would for the default (newline) delimiter
– steeldriver
Nov 17 at 16:55
In1 2 3
,2
isn't trailing however. What does "trailing" mean?
– Tim
Nov 17 at 17:07
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
Nov 17 at 17:36
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
Nov 17 at 17:44
|
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 Nov 17 at 16:43
steeldriver
33.7k34983
33.7k34983
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
Nov 17 at 16:49
@Tim you can add-t
if you want to "Remove a trailing DELIM" - exactly as you would for the default (newline) delimiter
– steeldriver
Nov 17 at 16:55
In1 2 3
,2
isn't trailing however. What does "trailing" mean?
– Tim
Nov 17 at 17:07
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
Nov 17 at 17:36
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
Nov 17 at 17:44
|
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
Nov 17 at 16:49
@Tim you can add-t
if you want to "Remove a trailing DELIM" - exactly as you would for the default (newline) delimiter
– steeldriver
Nov 17 at 16:55
In1 2 3
,2
isn't trailing however. What does "trailing" mean?
– Tim
Nov 17 at 17:07
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
Nov 17 at 17:36
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
Nov 17 at 17:44
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
Nov 17 at 16:49
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
Nov 17 at 16:49
@Tim you can add
-t
if you want to "Remove a trailing DELIM" - exactly as you would for the default (newline) delimiter– steeldriver
Nov 17 at 16:55
@Tim you can add
-t
if you want to "Remove a trailing DELIM" - exactly as you would for the default (newline) delimiter– steeldriver
Nov 17 at 16:55
In
1 2 3
, 2
isn't trailing however. What does "trailing" mean?– Tim
Nov 17 at 17:07
In
1 2 3
, 2
isn't trailing however. What does "trailing" mean?– Tim
Nov 17 at 17:07
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
Nov 17 at 17:36
@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
Nov 17 at 17:36
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
Nov 17 at 17:44
@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
Nov 17 at 17:44
|
show 1 more comment
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
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
Nov 17 at 16:30
Thanks. Updated.
– Tim
Nov 17 at 16:32
1
@JeffSchaller "What happened when you tried?"
– Tim
Nov 17 at 16:39