Split string by space in ZSH
Clash Royale CLAN TAG#URR8PPP
Give this file.txt
:
first line
second line
third line
This works in bash
:
while IFS=' ' read -a args; do
echo "$args[0]"
done < file.txt
To produce
first
second
third
That is to say, we were able to read the file line by line, and on each one we split the line further into an array using space as a delimiter. But in zsh
, the result is an error: read: bad option: -a
.
How can we achieve in zsh
the same as in bash
? I’ve tried several solutions, but I was never able to split a string into an array using spaces as the delimiter.
bash zsh array read
add a comment |
Give this file.txt
:
first line
second line
third line
This works in bash
:
while IFS=' ' read -a args; do
echo "$args[0]"
done < file.txt
To produce
first
second
third
That is to say, we were able to read the file line by line, and on each one we split the line further into an array using space as a delimiter. But in zsh
, the result is an error: read: bad option: -a
.
How can we achieve in zsh
the same as in bash
? I’ve tried several solutions, but I was never able to split a string into an array using spaces as the delimiter.
bash zsh array read
Can you use thecut
binary? It is not a shell built-in, but it will do what you want viacut -d ' ' -f 1 file.txt
.
– Jeremy Dover
Feb 14 at 5:10
@JeremyDover It would work for this case, but I want something more robust, hence asking for an array.
– user137369
Feb 14 at 5:17
add a comment |
Give this file.txt
:
first line
second line
third line
This works in bash
:
while IFS=' ' read -a args; do
echo "$args[0]"
done < file.txt
To produce
first
second
third
That is to say, we were able to read the file line by line, and on each one we split the line further into an array using space as a delimiter. But in zsh
, the result is an error: read: bad option: -a
.
How can we achieve in zsh
the same as in bash
? I’ve tried several solutions, but I was never able to split a string into an array using spaces as the delimiter.
bash zsh array read
Give this file.txt
:
first line
second line
third line
This works in bash
:
while IFS=' ' read -a args; do
echo "$args[0]"
done < file.txt
To produce
first
second
third
That is to say, we were able to read the file line by line, and on each one we split the line further into an array using space as a delimiter. But in zsh
, the result is an error: read: bad option: -a
.
How can we achieve in zsh
the same as in bash
? I’ve tried several solutions, but I was never able to split a string into an array using spaces as the delimiter.
bash zsh array read
bash zsh array read
edited Feb 14 at 5:07
user137369
asked Feb 14 at 4:49
user137369user137369
17718
17718
Can you use thecut
binary? It is not a shell built-in, but it will do what you want viacut -d ' ' -f 1 file.txt
.
– Jeremy Dover
Feb 14 at 5:10
@JeremyDover It would work for this case, but I want something more robust, hence asking for an array.
– user137369
Feb 14 at 5:17
add a comment |
Can you use thecut
binary? It is not a shell built-in, but it will do what you want viacut -d ' ' -f 1 file.txt
.
– Jeremy Dover
Feb 14 at 5:10
@JeremyDover It would work for this case, but I want something more robust, hence asking for an array.
– user137369
Feb 14 at 5:17
Can you use the
cut
binary? It is not a shell built-in, but it will do what you want via cut -d ' ' -f 1 file.txt
.– Jeremy Dover
Feb 14 at 5:10
Can you use the
cut
binary? It is not a shell built-in, but it will do what you want via cut -d ' ' -f 1 file.txt
.– Jeremy Dover
Feb 14 at 5:10
@JeremyDover It would work for this case, but I want something more robust, hence asking for an array.
– user137369
Feb 14 at 5:17
@JeremyDover It would work for this case, but I want something more robust, hence asking for an array.
– user137369
Feb 14 at 5:17
add a comment |
1 Answer
1
active
oldest
votes
From man zshbuiltins
, zsh's read uses -A
instead.
read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
[ -u n ] [ name[?prompt] ] [ name ... ]
...
-A The first name is taken as the name of an array
and all words are assigned to it.
Hence the command is
while IFS=' ' read -A args; do
echo "$args[1]"
done < file.txt
N.B. by default, zsh array numbering begins with 1
, whereas bash's begins with 0
.
$ man zshparam
...
Array Subscripts
...
The elements are numbered beginning with 1, unless the
KSH_ARRAYS option is set in which case they are numbered from zero.
1
You are correct. I had tried-A
but it did not seem to be working. It was a combination of the 1-based index plus the differences in slicing that were tripping me up.
– user137369
Feb 14 at 5:29
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f500537%2fsplit-string-by-space-in-zsh%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
From man zshbuiltins
, zsh's read uses -A
instead.
read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
[ -u n ] [ name[?prompt] ] [ name ... ]
...
-A The first name is taken as the name of an array
and all words are assigned to it.
Hence the command is
while IFS=' ' read -A args; do
echo "$args[1]"
done < file.txt
N.B. by default, zsh array numbering begins with 1
, whereas bash's begins with 0
.
$ man zshparam
...
Array Subscripts
...
The elements are numbered beginning with 1, unless the
KSH_ARRAYS option is set in which case they are numbered from zero.
1
You are correct. I had tried-A
but it did not seem to be working. It was a combination of the 1-based index plus the differences in slicing that were tripping me up.
– user137369
Feb 14 at 5:29
add a comment |
From man zshbuiltins
, zsh's read uses -A
instead.
read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
[ -u n ] [ name[?prompt] ] [ name ... ]
...
-A The first name is taken as the name of an array
and all words are assigned to it.
Hence the command is
while IFS=' ' read -A args; do
echo "$args[1]"
done < file.txt
N.B. by default, zsh array numbering begins with 1
, whereas bash's begins with 0
.
$ man zshparam
...
Array Subscripts
...
The elements are numbered beginning with 1, unless the
KSH_ARRAYS option is set in which case they are numbered from zero.
1
You are correct. I had tried-A
but it did not seem to be working. It was a combination of the 1-based index plus the differences in slicing that were tripping me up.
– user137369
Feb 14 at 5:29
add a comment |
From man zshbuiltins
, zsh's read uses -A
instead.
read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
[ -u n ] [ name[?prompt] ] [ name ... ]
...
-A The first name is taken as the name of an array
and all words are assigned to it.
Hence the command is
while IFS=' ' read -A args; do
echo "$args[1]"
done < file.txt
N.B. by default, zsh array numbering begins with 1
, whereas bash's begins with 0
.
$ man zshparam
...
Array Subscripts
...
The elements are numbered beginning with 1, unless the
KSH_ARRAYS option is set in which case they are numbered from zero.
From man zshbuiltins
, zsh's read uses -A
instead.
read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
[ -u n ] [ name[?prompt] ] [ name ... ]
...
-A The first name is taken as the name of an array
and all words are assigned to it.
Hence the command is
while IFS=' ' read -A args; do
echo "$args[1]"
done < file.txt
N.B. by default, zsh array numbering begins with 1
, whereas bash's begins with 0
.
$ man zshparam
...
Array Subscripts
...
The elements are numbered beginning with 1, unless the
KSH_ARRAYS option is set in which case they are numbered from zero.
edited Feb 14 at 5:18
answered Feb 14 at 5:10
SparhawkSparhawk
10.2k74398
10.2k74398
1
You are correct. I had tried-A
but it did not seem to be working. It was a combination of the 1-based index plus the differences in slicing that were tripping me up.
– user137369
Feb 14 at 5:29
add a comment |
1
You are correct. I had tried-A
but it did not seem to be working. It was a combination of the 1-based index plus the differences in slicing that were tripping me up.
– user137369
Feb 14 at 5:29
1
1
You are correct. I had tried
-A
but it did not seem to be working. It was a combination of the 1-based index plus the differences in slicing that were tripping me up.– user137369
Feb 14 at 5:29
You are correct. I had tried
-A
but it did not seem to be working. It was a combination of the 1-based index plus the differences in slicing that were tripping me up.– user137369
Feb 14 at 5:29
add a 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.
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%2f500537%2fsplit-string-by-space-in-zsh%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
Can you use the
cut
binary? It is not a shell built-in, but it will do what you want viacut -d ' ' -f 1 file.txt
.– Jeremy Dover
Feb 14 at 5:10
@JeremyDover It would work for this case, but I want something more robust, hence asking for an array.
– user137369
Feb 14 at 5:17