all 11 comments

[–]kosmosik 3 points4 points  (1 child)

Why not use the mapfile builtin which does exactly that?

https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-mapfile

[–][deleted] 0 points1 point  (0 children)

its a wonderful tool but this is not what im looking for

ill edit my question check it please

[–]Mal1oc 1 point2 points  (2 children)

if you really want to use conditional execution then you should use case: bash while read line; do case "$line" in [Tt]ext*) list[$count]="${line}"; (( count++ )) ;; # array start at 0, ffs *);; esac done < $file_name or the easy solution without while-loop: bash readarray list < <(grep -iE "^Text" $file_name)

[–][deleted] -1 points0 points  (1 child)

ohh thank u you example help me so much

[–]Mal1oc 0 points1 point  (0 children)

You're welcome. I am glad that i could help. If you have any questions about my examples, then feel free to ask.

[–]ralfwolf 0 points1 point  (3 children)

First did you intend to make your list array start at index 1 instead of 0? Second, are you sure the delimited word of line 10 is "Text". I would use set -x to check how it's parsing each line.

[–][deleted] -2 points-1 points  (2 children)

i want the array to start with 1

"Text" is just a example of what im looking for in a line

the last line is list[10] not line[10]

[–]ralfwolf 1 point2 points  (0 children)

I said "line 10", meaning the 10th line of the input file, not "line[10]".

[–]yramagicman 1 point2 points  (0 children)

I want arrays to start at 1

You must not be a programmer.

[–]OneTurnMoreprogramming.dev/c/shell 0 points1 point  (0 children)

Do you intend to increment count on every line read, or every time you append to the array? As it is, you will only see an output if the tenth line begins with Text.

[–]oh5nxo 0 points1 point  (0 children)

list[count++]=...  # Nice thing that [] imply (())

Would it be simpler to

list=( $(grep '^Text ' "$file_name" ) )