Count column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Count column
# 1  
Old 06-18-2014
Count column

how do i count column in a file.

test1,test2,test3,,, = 5 columns
# 2  
Old 06-18-2014
Quote:
Originally Posted by lawsongeek
test1,test2,test3,,, = 5 columns
If i am not mistaken having 5 separators in a line gives not 5 fields but 6:

Code:
field1 , field2 , field3 , field4 , field5 , field6

Anyway, you can count the numbers of fields:

Code:
echo "field1 , field2 , field3 , field4 , field5 , field6" | awk '{FS=",";print NF}'

I hope this helps.

bakunin
# 3  
Old 06-18-2014
my goal is see output of # of columns such as wc -lc.
# 4  
Old 06-18-2014
Quote:
Originally Posted by lawsongeek
my goal is see output of # of columns such as wc -lc.
I have understood that. I suggest you first try the command i wrote above, then modify the input (the string behind "echo") and see how that changes the output. Especially the line:

Code:
echo ",,,,," | awk '{FS=",";print NF}'

might tell you something.

I hope this helps.

bakunin
# 5  
Old 06-18-2014
thank you. worked perfectly.
# 6  
Old 06-18-2014
I'm not sure the FS is getting set in the right place. I get the following:-
Code:
# echo "field1 , field2 , field3 , field4 , field5 , field6" | awk '{FS=",";print NF}'
11
# echo "field1,field2,field3,field4,field5,field6" | awk '{FS=",";print NF}'          
1
#

The only difference is that I've removed the spaces. The FS="," seemingly has no effect:-
Code:
# echo "field1 , field2 , field3 , field4 , field5 , field6" | awk '{print NF}'       
11
# echo "field1,field2,field3,field4,field5,field6" | awk '{print NF}'          
1
#

Not being too experienced in awk, I did get more sucess with these:-
Code:
# echo "field1,field2,field3,field4,field5,field6" | awk -F, '{print NF}'
6
# echo "field1 , field2 , field3 , field4 , field5 , field6" | awk -F, '{print NF}'
6
#

Have I missed something important in your solution? Smilie I'm using RHEL 6


Kind regards,
Robin

Last edited by rbatte1; 06-18-2014 at 01:04 PM.. Reason: Added OS version
# 7  
Old 06-18-2014
Quote:
Originally Posted by rbatte1
I'm not sure the FS is getting set in the right place. I get the following:-
Code:
# echo "field1 , field2 , field3 , field4 , field5 , field6" | awk '{FS=",";print NF}'
11
# echo "field1,field2,field3,field4,field5,field6" | awk '{FS=",";print NF}'          
1

If this is your result your awk is definitely working different than mine. I used AIX (7.1.3 SP3) awk.

Quote:
Originally Posted by rbatte1
The only difference is that I've removed the spaces.
This is in line with my documentation. Per default the field separator (internal variable "FS") is set to a blank (which is translated to any whitespace in the current locale by awk). You got 11 fields because it treated each word and each comma as a separate field. You got 1 on the second try, because with blanks used as field separators there is only one field once you remove all the blanks. So it seems your awk just ignored the FS-declaration somehow and fell back to its defaults.


Quote:
Originally Posted by rbatte1
I did get more sucess with these:-
Code:
awk -F,

According to my documentation the "-F" parameter sets the value for the internal FS variable the same way i did and i suppose that means the two ways ought to produce the same result.

Addendum: i just checked with a Ubuntu system and the awk there works the same way you described it. The version is:

Code:
# awk -W version
mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan

Its documentation says that the variable FS can only be set to a ERE, so i tried
Code:
# echo "f1,f2,f3,f4" | awk 'BEGIN {FS="[,]";} {print NF;}'
4

which finally did the trick. Seems like mawk is more picky about where FS is set and only allows EREs whereas AIX's awk allows it to be set anywhere and allows single characters, blanks or EREs.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Column 2 string count in Column 3

My I/p is Col1|Col2|Col3 2116209997932|POSIX INC|POSIX 2116209997933|POSIX INC|POSIX 2116210089479|POSIX INC|POSIX 2116210180502|POSIX INC|POSIX 2116210512279|POSIX INC|Aero 2116210516838|POSIX INC|POSIX 2116210534342|POSIX INC|postal 2116210534345|POSIX INC|postal ... (6 Replies)
Discussion started by: nikhil jain
6 Replies

2. Shell Programming and Scripting

Read first column and count lines in second column using awk

Hello all, I would like to ask your help here: I've a huge file that has 2 columns. A part of it is: sorted.txt: kss23 rml.67lkj kss23 zhh.6gf kss23 nhd.09.fdd kss23 hp.767.88.89 fl67 nmdsfs.56.df.67 fl67 kk.fgf.98.56.n fl67 bgdgdfg.hjj.879.d fl66 kl..hfh.76.ghg fl66... (5 Replies)
Discussion started by: Padavan
5 Replies

3. Shell Programming and Scripting

Uniq count second column

Hello How can I get a number of occurrence count for this file; ERR315389.1000156 CTTGAAGAAGAATTGAAAACTGTGACGAACAACTTGAAGTCACTGGAGGCTCAGGCTGAGAAGTACTCGCAGAAGGAAGACAGATATGAGGAAGAG ERR315389.1000281 ... (3 Replies)
Discussion started by: Wan Fahmi
3 Replies

4. Shell Programming and Scripting

Count Column and Print at last

HI Guys, Count base on column A Input:- U00393 6 000100000 U00393 7 000100001 U00393 8 000100002 U00393 9 000100003 U00393 10 000100004 U00393 11 000100005 U00928 6 000100000 U00928 7 000100001 U00964 6 000100000 U00964 7 000100001 U00964 8 000100002 U00972 6 000100000 U00972... (3 Replies)
Discussion started by: asavaliya
3 Replies

5. UNIX for Dummies Questions & Answers

How to count the frequncey of a certain value in one column?

Hi all, I have file like this: FID IID MISS_PHENO N_MISS N_GENO F_MISS AU4103 AU4103201 Y 15473 66858 0.2314 AU4142 AU4142303 Y 15464 66858 0.2313 AU4128 AU4128304 Y 15458 66858 0.2312 AU4129 AU4129202 Y 15451 66858 0.2311 AU3934 AU3934201 Y 15441 66858 0.231 AU3934 AU3934304 Y 15448 66858... (2 Replies)
Discussion started by: luoruicd
2 Replies

6. Shell Programming and Scripting

Need Count of Column

Need help on count AL06 AL06_71 A=1 it=1 90 AL06 AL06_72 A=1 it=1 60 AL06 AL06_73 A=1 it=1 80 AL06 AL06_7 A=2 it=1 0 AL06 AL06_7 A=2 it=1 0 AL06 AL06_7 A=2 it=1 0 AL07 AL07_71 A=1 it=1 40 AL07 AL07_72 A=1 it=1 40 AL07 AL07_73 A=1 it=1 70 AL08 AL08_7 A=2 it=1 0 1 AL08 AL08_7 A=2 it=1... (2 Replies)
Discussion started by: asavaliya
2 Replies

7. Shell Programming and Scripting

Count column data

Hi Guys, B07 U51C A1 44 B1 44 Yes B07 L64U A2 44 B1 44 Yes B07 L62U A2 44 B1 44 Yes B07 L11C A4 32 B1 44 NO B05 L12Z A1 12 B1 44 NO B01 651Z A2 44 B1 44 NO B04 A51Z A2 12 B1 44 NO L07 B08D A4 12 B1 44 NO B07 RU8D A4 44 B1 44 Yes B07 L58D A4 15 B1 44 No B07 LA8D A4 44 B1 44 Yes B07... (6 Replies)
Discussion started by: asavaliya
6 Replies

8. Shell Programming and Scripting

Column value and it's count

Hi, i have data like a b a a b c d ... I have to output info of each distinct value that appears in the column and the count of it a-3,b-2,c-1,d-1 Is there a single line command using awk or sed to accomplish this? Thanks, -srinivas yelamanchili (7 Replies)
Discussion started by: ysrini
7 Replies

9. UNIX for Advanced & Expert Users

column count

I am making a script in bash shell and need to find out how many columns are in each row. I tried using awk '{print NF}' which will give me the number of columns for each row of the file all at once which is not what i want. I am trying to read each line individual from a file and want to know... (6 Replies)
Discussion started by: HackerSeabass
6 Replies

10. Shell Programming and Scripting

How to get the count only if two column matches?

Hi, I have a file with the contents as below, 10:23:10 GOOD 10.30.50.60 10:23:11 GOOD 10.30.50.62 10:23:12 Hello 10.30.50.60 10:23:12 BAD 10.30.50.60 10:23:13 GOOD 10.30.50.66 10:23:14 BAD 10.30.50.62... (3 Replies)
Discussion started by: gobinath
3 Replies
Login or Register to Ask a Question