Count minimum columns in file

Hi All,

Consider the file with following lines:

1,2,3,4
1,2,3,
5,6,7,7,8,9
1

[Please note that the actual contents of file have no reln with the query]

I need to get the count of minimum columns per line. i.e. in above case, it should come out to be 1 since the last line only has 1 column.

I tried following code:

minCount = 0
wordCountPerLine = 0

while true;
do
     read myLine || break
     for (i =0 ; i < NF ; i++)
     do
         $lineCount = $wordCountPerLine +1
     done
     if ($minCount < $wordCountPerLine )
     then
         $minCount = $wordCountPerLine 
     fi
     wordCountPerLine =0
done < [File Name]

Have 2 queries:

1 > Will this work !!!! ???

2 > The reason I myself cannot answer the 1st query is for the following errors:

line 3: minCount : command not found
line 4: wordCountPerLine : command not found
line 22: syntax error near unexpected token `('
line 22: ` for (i =0 ; i < NF ; i++)'

Please help out

You can try awk :rolleyes:

awk -F, 'NF{_=(NF<_||!_)?NF:_}END{print _}' file

Thanks a ton !!! U compressed it incredibly :slight_smile: ... But can you please evaluate my code?? And why am I getting simple variable declaration errors?

This just fixes the syntax errors; it doesn't touch the logic:

minCount=0
wordCountPerLine=0

while read myLine
do
     for (( i=0 ; i < NF ; i++ ))
     do
         lineCount=$(( $wordCountPerLine + 1 ))
     done
     if [ $minCount -lt $wordCountPerLine ]
     then
         minCount=$wordCountPerLine 
     fi
     wordCountPerLine=0
done < [File Name]

bash

IFS="," read -a arr <"file"
t=${#arr[@]}
while IFS="," read -a line
do
  n=${#line[@]}
  [ "$n" -lt "$t" ] && t="$n"  
done < "file"
echo "lowest count: $t"

Thank you sir !!

The value comes out to be zero, so my logic is wrong. Do I have to specify the Field separator as well ???

In any Bourne-type shell:

IFS=,
set -f
minwordcount=0
while read line
do
  set -- $line
  [ $minwordcount -lt $# ] && minwordcount=$#
done < "$file"
echo minwordcount=$minwordcount

Can you please explain me the code ??

More comprehensibly (and more reliably):

awk -F, '
  BEGIN { n = 9999999 }  ## adjsut upwards if necessary
 NF < n { n = NF }
    END { print n }
' FILE

Sir,

I am trying to retrieve the value of n for later use in my script. But I am not able to. IN the END block, I have done the following

END { minCount = n }

But it does not work and printing minCount returns a blank.

This might be a silly question. But what might be wrong with this assignment?

minCount and n are awk variables, not shell variables.

To get the value in a shell variable, use command substitution:

minCount=$(
awk -F, '
  BEGIN { n = 9999999 }  ## adjust upwards if necessary
 NF < n { n = NF }
    END { print n }
' FILE
)
awk -F"," 'NR==1{n=NF}NF<=n{n=NF}END{print n}' file

What about if you have an empty line on your file ?

---------- Post updated at 02:03 AM ---------- Previous update was at 02:02 AM ----------

Sorry is late now, I'll follow-up tomorrow.