Bash의 변수로 구성된 while 루프를 실행합니다.

Bash의 변수로 구성된 while 루프를 실행합니다.

파일을 한 줄씩 읽고, 각 줄에서 일부 작업을 수행하고, 출력을 새 파일에 쓰는 쉘 스크립트를 작성하려고 합니다. 파일 이름이 매개변수로 전달됩니다.

런타임에 파일이 전달되므로 런타임에 열 수를 계산해야 합니다.

열 수를 계산했습니다. 그리고 루프를 사용하여 다음과 같은 문자열을 구성합니다.

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개 이상의 열이 포함된 대용량 파일을 읽어야 합니다.

누구든지 도와줄 수 있나요?

이를 달성하는 더 효율적인 방법이 있습니까?

관련 정보