0

I am trying to write a shell script which reads a file line by line, does some operations on each line, and write output to a new file. The file name is passed as an argument.

Since the file is passed at run time, I must calculate the number of columns at run time.

I calculated the number of columns with awk and construct a string using a loop, as below:

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  

But it is not working, throwing below error read: col1 col2 col3 col4: invalid variable name

I have to read huge files having more than 15 columns

Can anyone please help.

Also is there any more efficient way to achieve this?

  • I have a hard time trying to understand what you mean. Fix formatting in your post and show the whole script you use. – Arkadiusz Drabczyk Apr 28 '20 at 21:00
  • 2
    Also related: [Why is using a shell loop to process text considered bad practice?](https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice?noredirect=1&lq=1) – Kusalananda Apr 28 '20 at 21:10
  • I'm not entirely clear on the situation, but would reading into an array (instead of separate variables) work? Something like `read -a cols`, and then refer to `"${cols[0]}"` etc. – Gordon Davisson Apr 29 '20 at 03:06
  • I have also tried reading line by line from file and splitting it on delimiter, reading the fields into array and iterating array and then applying myfunc on each value. but it is taking lot of time to execute. Not sure if doing this way reduces any execution time as more or less iam still doing the same process. – BHARATH RAJ Apr 29 '20 at 10:04
  • Using an array (instead of separate variables) shouldn't affect the time it takes (unless something *really* weird is going on). – Gordon Davisson Apr 30 '20 at 15:49

0 Answers0