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?
read -a cols
, and then refer to"${cols[0]}"
etc. – Gordon Davisson Apr 29 '20 at 03:06