Count minimum columns in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count minimum columns in file
# 8  
Old 11-19-2009
Quote:
Originally Posted by danmero
You can try awk Smilie
Code:
awk -F, 'NF{_=(NF<_||!_)?NF:_}END{print _}' file


Can you please explain me the code ??
# 9  
Old 11-19-2009
Quote:
Originally Posted by sh_kk
Can you please explain me the code ??

More comprehensibly (and more reliably):

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

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

More comprehensibly (and more reliably):

Code:
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?
# 11  
Old 11-19-2009
Quote:
Originally Posted by sh_kk
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:

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

# 12  
Old 11-19-2009
Code:
awk -F"," 'NR==1{n=NF}NF<=n{n=NF}END{print n}' file

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