How to iterate over text file having multiple-words-per-line using shell script?

后端 未结 4 1718
南旧
南旧 2021-01-24 15:16

I know how to iterate over lines of text when the text file has contents as below:

abc  
pqr  
xyz

However, what if the contents of my text fil

4条回答
  •  青春惊慌失措
    2021-01-24 15:59

    If you want to read every word in a file into a single array you can do it like this:

    arr=()
    while read -r -a _a; do
       arr+=("${a[@]}")
    done < infile
    

    Which uses -r to avoid read from interpreting backslashes in the input and -a to have it split the words (splitting on $IFS) into an array. It then appends all the elements of that array to the accumulating array while being safe for globbing and other metacharacters.

提交回复
热议问题