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

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

提交回复
热议问题