How to create an array from the lines of a command's output

kolonel

I have a file called failedfiles.txt with the following content:

failed1
failed2
failed3

I need to use grep to return the content on each line in that file, and save the output in a list to be accessed. So I want something like this:

temp_list=$(grep "[a-z]" failedfiles.txt)

However, the problem with this is that when I type

echo ${temp_list[0]}

I get the following output:

failed1 failed2 failed3

But what I want is when I do:

echo ${temp_list[0]}

to print

failed1

and when I do:

echo ${temp_list[1]}

to print

failed2

Thanks.

devnull

You did not create an array. What you did was Command Substitution which would simply put the output of a command into a variable.

In order to create an array, say:

temp_list=( $(grep "[a-z]" failedfiles.txt) )

You might also want to refer to Guide on Arrays.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create an array from the lines of a command's output

From Dev

How to limit the number of lines a command's output has available in bash?

From Dev

How to insert tabs before output lines from a executed command

From Dev

bash - count and output lines from command

From Dev

Is there any way to read lines from command output?

From Dev

How to separate command output to individual lines

From Dev

How to prepend lines to git command output

From Dev

How to initialize an array with elements with whitespace from a command output

From Dev

Bash: How to get a value from an array to create command from it

From Dev

bash: $(...) trims trailing empty lines. How to properly assign to variable a command's output?

From Dev

how to assign terminal variable from previous command's output

From Dev

How to create array of functions from command line in bash?

From Dev

Storing command output lines into array based on new line character

From Dev

Create header to table from the output of the command "paste"

From Dev

Check for zero lines output from command over SSH

From Dev

Read specific floating point values from lines of the command output

From Dev

Is there a way to find out the number of lines from a command output?

From Dev

Capture output from command into variable retaining new lines

From Dev

grep lines starting with - or plus from yum command output

From Dev

Parsing a string to an array output from command line

From Dev

Generate an associative array from command output

From Dev

How to create an environment variable that is the output of a command

From Dev

How can I create array of lines in this case?

From Dev

How to filter out lines of a command output that occur in a text file?

From Dev

How to store multiple lines of command output to a variable using JSch

From Dev

how to read certain lines of a file that is the output of fc command with a batch script

From Dev

How to use Ruby's command line in-place-edit mode to remove lines from a text file

From Dev

Array output to multiple lines

From Dev

How can I create a Bash conditional script, based on output from a command?

Related Related

  1. 1

    How to create an array from the lines of a command's output

  2. 2

    How to limit the number of lines a command's output has available in bash?

  3. 3

    How to insert tabs before output lines from a executed command

  4. 4

    bash - count and output lines from command

  5. 5

    Is there any way to read lines from command output?

  6. 6

    How to separate command output to individual lines

  7. 7

    How to prepend lines to git command output

  8. 8

    How to initialize an array with elements with whitespace from a command output

  9. 9

    Bash: How to get a value from an array to create command from it

  10. 10

    bash: $(...) trims trailing empty lines. How to properly assign to variable a command's output?

  11. 11

    how to assign terminal variable from previous command's output

  12. 12

    How to create array of functions from command line in bash?

  13. 13

    Storing command output lines into array based on new line character

  14. 14

    Create header to table from the output of the command "paste"

  15. 15

    Check for zero lines output from command over SSH

  16. 16

    Read specific floating point values from lines of the command output

  17. 17

    Is there a way to find out the number of lines from a command output?

  18. 18

    Capture output from command into variable retaining new lines

  19. 19

    grep lines starting with - or plus from yum command output

  20. 20

    Parsing a string to an array output from command line

  21. 21

    Generate an associative array from command output

  22. 22

    How to create an environment variable that is the output of a command

  23. 23

    How can I create array of lines in this case?

  24. 24

    How to filter out lines of a command output that occur in a text file?

  25. 25

    How to store multiple lines of command output to a variable using JSch

  26. 26

    how to read certain lines of a file that is the output of fc command with a batch script

  27. 27

    How to use Ruby's command line in-place-edit mode to remove lines from a text file

  28. 28

    Array output to multiple lines

  29. 29

    How can I create a Bash conditional script, based on output from a command?

HotTag

Archive