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

北战南征 提交于 2020-01-30 08:30:25

问题


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 file are as below,

abc xyz  
cdf pqr  
lmn rst

and I need to get values "abc" stored to one variable and"xyz" stored to another variable. How would I do that?


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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")'


来源:https://stackoverflow.com/questions/36747688/how-to-iterate-over-text-file-having-multiple-words-per-line-using-shell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!