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
...









share|improve this question




















  • 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

















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
...









share|improve this question




















  • 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















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
...









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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.





share|improve this answer





















  • 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










  • In 1 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 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




    @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













Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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.





share|improve this answer





















  • 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










  • In 1 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 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




    @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

















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.





share|improve this answer





















  • 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










  • In 1 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 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




    @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















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.





share|improve this answer












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.






share|improve this answer












share|improve this answer



share|improve this answer










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 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










  • In 1 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 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




    @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












  • @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








  • 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






  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

AnyDesk - Fatal Program Failure

How to calibrate 16:9 built-in touch-screen to a 4:3 resolution?

QoS: MAC-Priority for clients behind a repeater