How can I store each separate entire line in a text file into an array?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a file called "threewords". It contains the information:
#gray speedy bee
gr-A | sp-E-d-E | b-E
#gray greedy pea
gr-A | gr-E-d-E | p-E
When I run the command:
cat threewords | grep ^# | cut -c2-
The command pulls the two lines beginning with #. It then removes the # and returns this as output:
gray speedy bee
gray greedy pea
When I run my command:
array=($(cat threewords | grep ^# | cut -c2-))
It creates the array but it separates all the words into separate array positions like this:
array[0] = gray,
array[1] = speedy,
array[2] = bee,
array[3] = gray,
array[4] = greedy,
array[5] = pea
I can figure out the code to make it put the output of each line into an array like so:
array[0] = gray speedy bee,
array[1] = gray greedy pea
grep array cut
add a comment |Â
up vote
0
down vote
favorite
I have a file called "threewords". It contains the information:
#gray speedy bee
gr-A | sp-E-d-E | b-E
#gray greedy pea
gr-A | gr-E-d-E | p-E
When I run the command:
cat threewords | grep ^# | cut -c2-
The command pulls the two lines beginning with #. It then removes the # and returns this as output:
gray speedy bee
gray greedy pea
When I run my command:
array=($(cat threewords | grep ^# | cut -c2-))
It creates the array but it separates all the words into separate array positions like this:
array[0] = gray,
array[1] = speedy,
array[2] = bee,
array[3] = gray,
array[4] = greedy,
array[5] = pea
I can figure out the code to make it put the output of each line into an array like so:
array[0] = gray speedy bee,
array[1] = gray greedy pea
grep array cut
1
this is a terrible idea.
â mikeserv
Dec 14 '14 at 1:18
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a file called "threewords". It contains the information:
#gray speedy bee
gr-A | sp-E-d-E | b-E
#gray greedy pea
gr-A | gr-E-d-E | p-E
When I run the command:
cat threewords | grep ^# | cut -c2-
The command pulls the two lines beginning with #. It then removes the # and returns this as output:
gray speedy bee
gray greedy pea
When I run my command:
array=($(cat threewords | grep ^# | cut -c2-))
It creates the array but it separates all the words into separate array positions like this:
array[0] = gray,
array[1] = speedy,
array[2] = bee,
array[3] = gray,
array[4] = greedy,
array[5] = pea
I can figure out the code to make it put the output of each line into an array like so:
array[0] = gray speedy bee,
array[1] = gray greedy pea
grep array cut
I have a file called "threewords". It contains the information:
#gray speedy bee
gr-A | sp-E-d-E | b-E
#gray greedy pea
gr-A | gr-E-d-E | p-E
When I run the command:
cat threewords | grep ^# | cut -c2-
The command pulls the two lines beginning with #. It then removes the # and returns this as output:
gray speedy bee
gray greedy pea
When I run my command:
array=($(cat threewords | grep ^# | cut -c2-))
It creates the array but it separates all the words into separate array positions like this:
array[0] = gray,
array[1] = speedy,
array[2] = bee,
array[3] = gray,
array[4] = greedy,
array[5] = pea
I can figure out the code to make it put the output of each line into an array like so:
array[0] = gray speedy bee,
array[1] = gray greedy pea
grep array cut
grep array cut
edited Aug 13 at 11:00
Jeff Schaller
32.5k849110
32.5k849110
asked Dec 13 '14 at 22:52
Ben
413
413
1
this is a terrible idea.
â mikeserv
Dec 14 '14 at 1:18
add a comment |Â
1
this is a terrible idea.
â mikeserv
Dec 14 '14 at 1:18
1
1
this is a terrible idea.
â mikeserv
Dec 14 '14 at 1:18
this is a terrible idea.
â mikeserv
Dec 14 '14 at 1:18
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
The splitting is done with IFS
as the delimiter (which contains a space, newline and tab by default). Set the IFS
to only the newline:
$ IFS=$'n' a=($(printf "1 2n2 3n"))
$ echo $a[0]
1 2
$ echo $a[1]
2 3
This will change IFS
for the shell, so best save it before and restore it:
OLD_IFS="$IFS"
IFS=$'n' array=($(grep '^#' threewords | cut -c2-))
IFS="$OLD_IFS"
And there's absolutely no reason to do:
cat threewords | grep '^#'
grep
is perfectly capable of reading files:
grep '^#' threewords
As Stephane notes, when subjecting the output of a command to further shell expansion, one should disable globbing using set -f
:
$ help set
...
-f Disable file name generation (globbing).
Otherwise:
$ cd /; a=( $(printf "*n") )
$ echo $a[@]
bin boot cdrom dev etc home ...
2
You also needset -f
.
â Stéphane Chazelas
Dec 13 '14 at 23:10
add a comment |Â
up vote
2
down vote
If you have bash
4
mapfile -t array < <(grep ^# threewords | cut -c2-)
Will populate array
, one line per element
printf "%sn" "$array[@]"
gray speedy bee
gray greedy pea
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
The splitting is done with IFS
as the delimiter (which contains a space, newline and tab by default). Set the IFS
to only the newline:
$ IFS=$'n' a=($(printf "1 2n2 3n"))
$ echo $a[0]
1 2
$ echo $a[1]
2 3
This will change IFS
for the shell, so best save it before and restore it:
OLD_IFS="$IFS"
IFS=$'n' array=($(grep '^#' threewords | cut -c2-))
IFS="$OLD_IFS"
And there's absolutely no reason to do:
cat threewords | grep '^#'
grep
is perfectly capable of reading files:
grep '^#' threewords
As Stephane notes, when subjecting the output of a command to further shell expansion, one should disable globbing using set -f
:
$ help set
...
-f Disable file name generation (globbing).
Otherwise:
$ cd /; a=( $(printf "*n") )
$ echo $a[@]
bin boot cdrom dev etc home ...
2
You also needset -f
.
â Stéphane Chazelas
Dec 13 '14 at 23:10
add a comment |Â
up vote
2
down vote
The splitting is done with IFS
as the delimiter (which contains a space, newline and tab by default). Set the IFS
to only the newline:
$ IFS=$'n' a=($(printf "1 2n2 3n"))
$ echo $a[0]
1 2
$ echo $a[1]
2 3
This will change IFS
for the shell, so best save it before and restore it:
OLD_IFS="$IFS"
IFS=$'n' array=($(grep '^#' threewords | cut -c2-))
IFS="$OLD_IFS"
And there's absolutely no reason to do:
cat threewords | grep '^#'
grep
is perfectly capable of reading files:
grep '^#' threewords
As Stephane notes, when subjecting the output of a command to further shell expansion, one should disable globbing using set -f
:
$ help set
...
-f Disable file name generation (globbing).
Otherwise:
$ cd /; a=( $(printf "*n") )
$ echo $a[@]
bin boot cdrom dev etc home ...
2
You also needset -f
.
â Stéphane Chazelas
Dec 13 '14 at 23:10
add a comment |Â
up vote
2
down vote
up vote
2
down vote
The splitting is done with IFS
as the delimiter (which contains a space, newline and tab by default). Set the IFS
to only the newline:
$ IFS=$'n' a=($(printf "1 2n2 3n"))
$ echo $a[0]
1 2
$ echo $a[1]
2 3
This will change IFS
for the shell, so best save it before and restore it:
OLD_IFS="$IFS"
IFS=$'n' array=($(grep '^#' threewords | cut -c2-))
IFS="$OLD_IFS"
And there's absolutely no reason to do:
cat threewords | grep '^#'
grep
is perfectly capable of reading files:
grep '^#' threewords
As Stephane notes, when subjecting the output of a command to further shell expansion, one should disable globbing using set -f
:
$ help set
...
-f Disable file name generation (globbing).
Otherwise:
$ cd /; a=( $(printf "*n") )
$ echo $a[@]
bin boot cdrom dev etc home ...
The splitting is done with IFS
as the delimiter (which contains a space, newline and tab by default). Set the IFS
to only the newline:
$ IFS=$'n' a=($(printf "1 2n2 3n"))
$ echo $a[0]
1 2
$ echo $a[1]
2 3
This will change IFS
for the shell, so best save it before and restore it:
OLD_IFS="$IFS"
IFS=$'n' array=($(grep '^#' threewords | cut -c2-))
IFS="$OLD_IFS"
And there's absolutely no reason to do:
cat threewords | grep '^#'
grep
is perfectly capable of reading files:
grep '^#' threewords
As Stephane notes, when subjecting the output of a command to further shell expansion, one should disable globbing using set -f
:
$ help set
...
-f Disable file name generation (globbing).
Otherwise:
$ cd /; a=( $(printf "*n") )
$ echo $a[@]
bin boot cdrom dev etc home ...
edited Apr 13 '17 at 12:36
Communityâ¦
1
1
answered Dec 13 '14 at 22:59
muru
33.6k577144
33.6k577144
2
You also needset -f
.
â Stéphane Chazelas
Dec 13 '14 at 23:10
add a comment |Â
2
You also needset -f
.
â Stéphane Chazelas
Dec 13 '14 at 23:10
2
2
You also need
set -f
.â Stéphane Chazelas
Dec 13 '14 at 23:10
You also need
set -f
.â Stéphane Chazelas
Dec 13 '14 at 23:10
add a comment |Â
up vote
2
down vote
If you have bash
4
mapfile -t array < <(grep ^# threewords | cut -c2-)
Will populate array
, one line per element
printf "%sn" "$array[@]"
gray speedy bee
gray greedy pea
add a comment |Â
up vote
2
down vote
If you have bash
4
mapfile -t array < <(grep ^# threewords | cut -c2-)
Will populate array
, one line per element
printf "%sn" "$array[@]"
gray speedy bee
gray greedy pea
add a comment |Â
up vote
2
down vote
up vote
2
down vote
If you have bash
4
mapfile -t array < <(grep ^# threewords | cut -c2-)
Will populate array
, one line per element
printf "%sn" "$array[@]"
gray speedy bee
gray greedy pea
If you have bash
4
mapfile -t array < <(grep ^# threewords | cut -c2-)
Will populate array
, one line per element
printf "%sn" "$array[@]"
gray speedy bee
gray greedy pea
answered Dec 14 '14 at 0:44
iruvar
11.6k62959
11.6k62959
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%2f174111%2fhow-can-i-store-each-separate-entire-line-in-a-text-file-into-an-array%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
1
this is a terrible idea.
â mikeserv
Dec 14 '14 at 1:18