Count minimum columns in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count minimum columns in file
# 1  
Old 11-19-2009
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:

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

Last edited by sh_kk; 11-19-2009 at 12:04 AM.. Reason: Incorrect Title
# 2  
Old 11-19-2009
You can try awk Smilie
Code:
awk -F, 'NF{_=(NF<_||!_)?NF:_}END{print _}' file

# 3  
Old 11-19-2009
Quote:
Originally Posted by danmero
You can try awk Smilie
Code:
awk -F, 'NF{_=(NF<_||!_)?NF:_}END{print _}' file


Thanks a ton !!! U compressed it incredibly Smilie ... But can you please evaluate my code?? And why am I getting simple variable declaration errors?
# 4  
Old 11-19-2009

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

Code:
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]

# 5  
Old 11-19-2009
bash
Code:
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"

# 6  
Old 11-19-2009
Quote:
Originally Posted by cfajohnson

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

Code:
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]

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 ???
# 7  
Old 11-19-2009

In any Bourne-type shell:

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Count columns that are non-empty per line

The file is similar to the attached. Thousands of columns, delimiter is tab, with many columns containing free text and space separated. I want to get the count of columns with non-empty entries. eg.Col1=10, Col6=5, Col8=1 awk preferred (7 Replies)
Discussion started by: genehunter
7 Replies

2. UNIX for Beginners Questions & Answers

Copy columns from one file into another and get sum of column values and row count

I have a file abc.csv, from which I need column 24(PurchaseOrder_TotalCost) to get the sum_of_amounts with date and row count into another file say output.csv abc.csv- UTF-8,,,,,,,,,,,,,,,,,,,,,,,,, ... (6 Replies)
Discussion started by: Tahir_M
6 Replies

3. UNIX for Beginners Questions & Answers

Count multiple columns and print original file

Hello, I have two tab files with headers File1: with 4 columns header1 header2 header3 header4 44 a bb 1 57 c ab 4 64 d d 5 File2: with 26 columns header1.. header5 header6 header7 ... header 22...header26 id1 44 a bb id2 57 ... (6 Replies)
Discussion started by: nans
6 Replies

4. Shell Programming and Scripting

In a row, replace negative sign and find minimum value among four columns

Hi Friends, I have an input file like this chr1 100 200 1 2 3 4 chr1 150 200 4 5 6 7 chr2 300 400 9 6 7 1 chr2 300 410 -10 21 -11 13 chr3 700 900 -21 -22 130 165 Now, my output file is chr1 100 200 1 chr1 150 200 4 chr2 300 400 1 chr2 300 410 10 chr3 700 900 21 Remove... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

5. Shell Programming and Scripting

minimum and maximum from columns

Hi Friends, my input file is this way chr1 100 120 abc chr1 100 121 def chr1 100 122 ghi chr2 240 263 kil chr2 240 276 ghj chr2 255 290 hjh my output chr1 100 122 abc chr2 240 276 kil chr2 255 290 hjh Basically, I want to match on first and second column and then print the... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

6. UNIX for Dummies Questions & Answers

Getting the minimum of each column in a file

Hi, I have a file like: 0.000000 124.085533 124.085533 124.085533 124.085533 124.085533 124.085533 124.085533 124.085533 124.085533 33.097845 33.363764 0.000000 266.483441 262.519130 266.380993 274.989622 289.594799 309.523518 336.124848 372.386124 413.522043 429.984825 421.621810... (6 Replies)
Discussion started by: cosmologist
6 Replies

7. Shell Programming and Scripting

Difference between two columns and count

hi I am very new to shell scripting. i wanted to achieve this.. Col1 Col2 Col3 Col4 aa 23 bb 32 aa 34 bb 12 aa 45 bb 345 kk 20 ss 50 kk 30 ss 50 tt 10 vv 50 Desired output is Col1 Col2 Col3 Col4 Difference Count aa 23 bb ... (1 Reply)
Discussion started by: empyrean
1 Replies

8. Shell Programming and Scripting

Count all columns in a given file

Hi, I have a file where i have dump of a DB. it has all the columns and rows inside that. I wanted to extract the columns and number of columns from that dump. Any suggestions will be helpful. Thanks (1 Reply)
Discussion started by: namishtiwari
1 Replies

9. UNIX for Dummies Questions & Answers

Extract minimum values among 3 columns

Hi. I would like to ask for helps on extracting a minimum values among three columns using gawk in tab separator. input file: as1 10 20 30 as2 22 21 23 as3 300 391 567 as4 19 20 15 Output file: as1 10 as2 21 as3 300 as4 15 I am extremely appreciate your helps and comments.... (2 Replies)
Discussion started by: Amanda Low
2 Replies

10. Shell Programming and Scripting

Columns count

I'm writting a script that will get the logical volumes from a volume group and check for mirroring. I'm using a while read line command. I need to be able to count the columns for the output and if it is <= 3 save the logical volume to a file and if it is > 3 continue. How do I capture the... (1 Reply)
Discussion started by: daveisme
1 Replies
Login or Register to Ask a Question