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

后端 未结 4 1618
南旧
南旧 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:47

    If the delimiter is a space then you can do:

    #!/bin/bash
    
    ALLVALUES=()
    while read line
    do
        ALLVALUES+=( $line )
    done < "/path/to/your/file"
    

    So after, you can just reference an element by ${ALLVALUES[0]} or ${ALLVALUES[1]} etc

    0 讨论(0)
  • 2021-01-24 15:57

    read splits the line by $IFS as many times as you pass variables to it:

    while read var1 var2 ; do
        echo "var1: ${var1} var2: ${var2}"
    done
    

    You see, if you pass var1 and var2 both columns go to separate variables. But note that if the line would contain more columns var2 would contain the whole remaining line, not just column2.

    Type help read for more info.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-24 16:00

    This awk command reads the input word by word:

    awk -v RS='[[:space:]]+' '1' file
    
    abc
    xyz
    cdf
    pqr
    lmn
    rst
    

    To populate a shell array use awk command in process substitution:

    arr=()
    while read -r w; do
       arr+=("$w")
    done < <(awk -v RS='[[:space:]]+' '1' file)
    

    And print the array content:

    declare -p arr
    declare -a arr='([0]="abc" [1]="xyz" [2]="cdf" [3]="pqr" [4]="lmn" [5]="rst")'
    
    0 讨论(0)
提交回复
热议问题