![Bash의 변수로 구성된 while 루프를 실행합니다.](https://linux55.com/image/172071/Bash%EC%9D%98%20%EB%B3%80%EC%88%98%EB%A1%9C%20%EA%B5%AC%EC%84%B1%EB%90%9C%20while%20%EB%A3%A8%ED%94%84%EB%A5%BC%20%EC%8B%A4%ED%96%89%ED%95%A9%EB%8B%88%EB%8B%A4..png)
파일을 한 줄씩 읽고, 각 줄에서 일부 작업을 수행하고, 출력을 새 파일에 쓰는 쉘 스크립트를 작성하려고 합니다. 파일 이름이 매개변수로 전달됩니다.
런타임에 파일이 전달되므로 런타임에 열 수를 계산해야 합니다.
열 수를 계산했습니다. 앗 그리고 루프를 사용하여 다음과 같은 문자열을 구성합니다.
file="$1
del=$2
columns=`head -1 $file| awk -F'$del' '{print NF}'``
cols=''
for ((z=1; z<=$columns; z++ ))
do
cols=$cols" ""col""$z"
done
#if file has 4 columns, value of cols will be "col1 col2 col3 col4"
While read $cols
do
#iterating through each column and apply my function - myfunc to do required conversions on each value. variable- $outvarmyfunc is derived in myfunc
outline=""
for ((s=1; s<=$columns; s++ ))
do
eval col=\${col$s}
myfunc $col
#below if condition is to avoid delimter before first word in each line
if[ $s -eq 1 ]
then
outline="$outvarmyfunc"
else
outline="outline""$del""$outvarmyfunc"
fi
done
echo "$outline" >> $file"_modified"
done < $file
하지만 다음 오류가 발생하면서 작동하지 않습니다. col1 col2 col3 col4: 잘못된 변수 이름
15개 이상의 열이 포함된 대용량 파일을 읽어야 합니다.
누구든지 도와줄 수 있나요?
이를 달성하는 더 효율적인 방법이 있습니까?