how can I capture in an array the text of a line from a txt file? in the shell

1

I want to capture the text of each line of a text file example text

Finally, I indicate that if we invoke the command without counting options (count lines, words, etc), the command will directly return four columns: the count of words, lines and total bytes of the file.

I would like to capture each line in an array or a variable in bash

    
asked by Yeko Garcia 02.08.2018 в 21:20
source

1 answer

1

I can think of something like that.

$ declare -a array_lineas
$ i=0
$ while read linea
> do
> array_lineas[$i]="$linea"
> ((i++))
> done <<< "$(echo -e 'linea1\nlinea2\nlinea3')"
$ echo "${array_lineas[@]}"
linea1 linea2 linea3

There I used an here-string, but with a file it should be something like that.

$ declare -a array_lineas  # declaro array_lineas como una variable de tipo array
$ i=0  # inicializo el contador
$ while read linea  # read puede leer línea por línea, i.e., según el delimitador de salto de línea
do
array_lineas[$i]="$linea"
((i++))
done < archivo.txt  # El bucle while se alimenta (su standard input) de archivo.txt
$ echo "${array_lineas[@]}"
linea1 linea2 linea3
    
answered by 03.08.2018 / 00:41
source