Find number of columns in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find number of columns in a file
# 8  
Old 05-20-2008
Again I am not sure the way you are running the command.
Make sure that you are not missing the single quotes ( ' ).
Can you send the command you are trying to run and the kind of
file on which you are running the command.

thanks
Apoorva
# 9  
Old 05-20-2008
Quote:
Originally Posted by benitdhotekar
hey you need to use the field separator if is doesnt work in normal way.

eg.

file1:
a, b, c, d, 1, 2, man, fruit

here use:

cat file1 | awk 'BEGIN{FS=","};{print NF}'

and you will get the output as:
8
Yes very correct. As my solution assumes space as default field separator.

thanks
Apoorva
# 10  
Old 05-20-2008
hi

the command wht i type is

cat file1 | awk`{print NF}'

and in file1 there is no feild separator

so what it means
# 11  
Old 05-20-2008
well it seems that you are missing one space between awk and opening single quote ('). It should be like cat file1 | awk '{ print NF }'.

thanks
Apoorva
# 12  
Old 05-20-2008
Also the first quoting character displays as a backtick in your last message. Those should be regular single quotes (ASCII 39) on both sides.

Incidentally, this is a Useless Use of Cat. Awk is perfectly capable of reading the file itself.

Code:
awk '{ print NF }' file

Try copy/paste that if you're not sure how to type the correct quotes.
# 13  
Old 05-21-2008
hiiiii
thanks a lot tht quotes was in error i guess

thanks
regards
pari
# 14  
Old 05-08-2009
Find number of columns in a file for multiple rows

Hi,

The above mentioned solutions provide the no.of columns for each row. But, wht if I want the no.of columns for the complete file instead of each row.
For E.g.. lets say I have a file with the below data

1 John Chicago
2 Pearl Washington

This expression awk -F' ' '{print NF}' would result in
3
3
But I need it for the complete file irrespective of no.of rows as just 3.

Please let me know the solution for this.

TIA
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to check for a specific number of columns in a file

Hi All I have a file which has five columns in each rows. cat file.txt a|b|c|d|e 1|2|3|4|5 a1|a2|a3|a4|a5 . . . I need to make sure that there are no less than five or more than five columns (in all the rows) by mistake. I tried this : cat file.txt | awk 'BEGIN{FS="|"};{print... (3 Replies)
Discussion started by: chatwithsaurav
3 Replies

2. Shell Programming and Scripting

Shell code for split/merge the file with certain number of columns

i have a file with some number of colums and each row have different number of fields. now my target is supposed to be, each row should have same number of columns. example: src file: 111,S3mobile,Samsu ng 112,Lu mia,Nok ia 113,brav ia,Sonyerichson tgt file: 111,S3mobile,Samsung... (8 Replies)
Discussion started by: abhilash_nakka
8 Replies

3. Shell Programming and Scripting

Number of columns in xml file

How to find the number of columns in xml file. i tried following command. #!bin/ksh cat $1 | grep -c "</mdm:attribute>" exit 0 but i am not getting accurate result which is not matching with the manual count. data format : <mdm:attribute> value </mdm:attribute> (6 Replies)
Discussion started by: victory
6 Replies

4. AIX

How to find number of columns in xml file?

How to find the number of columns in xml file. i tried following command. Code: #!bin/ksh cat $1 | grep -c "</mdm:attribute>"exit 0but i am not getting accurate result which is not matching with the manual count. data format : Code: <mdm:attribute> value </mdm:attribute> No... (1 Reply)
Discussion started by: victory
1 Replies

5. Shell Programming and Scripting

Needed value after the last delimeter in a file with varying number of delimited columns

Hi All, My file has the records as below: aaa\bbb\c\dd\ee\ff\gg zz\vv\ww pp\oo\ii\uu How can I get the value after the last delimeter. My o/p: gg ww uu Thanks in Advance, (5 Replies)
Discussion started by: HemaV
5 Replies

6. Shell Programming and Scripting

Read flat file upto certain number of columns

Hello Guys Please help me with the below issue I want to read a flat file source upto certain number of columns Say my flat file has 30 columns but I want to read upto 25 columns only How come the above issue can be addressed? Thanks a lot!!!! (1 Reply)
Discussion started by: Pratik4891
1 Replies

7. Shell Programming and Scripting

How do i find the first number in each line and insert dummy string into the missing columns?

Hi, I have one input file with the following content: MY_inpfile.txt Aname1 Cname1 Cname2 1808 5 Aname2 Cname1 1802 47 Bname1 ? 1819 22 Bname2 Cname1 1784 11 Bname3 1817 9 Zname1 Cname1 1805 59 Zname2 Cname1 Cname2 Cname3 1797 27 Every line in my input file have a 4 digit... (5 Replies)
Discussion started by: Szaffy
5 Replies

8. UNIX for Dummies Questions & Answers

Delete large number of columns rom file

Hi, I have a data file that contains 61 columns. I want to delete all the columns except columns, 3,6 and 8. The columns are tab de-limited. How would I achieve this on the terminal? Thanks (2 Replies)
Discussion started by: lost.identity
2 Replies

9. Shell Programming and Scripting

concatenate 'n' number of columns in a file

i have a file which may have 'n' number of columns 1 222 fafda 32 afdaf 4343 4343 234 43fdaf 4343 fdd fdfd fdfd fdd fdfd fdfd fdfd fdfd fdfd fdd fdfd fdfd need to concatenate the columns with... (3 Replies)
Discussion started by: mlpathir
3 Replies

10. Shell Programming and Scripting

Print columns in a file by number list in another file

This should follow with my last post but I think it's better to start a new one. Now I have a list of numbers stored in pos.txt: 2 6 7 . . . n . . . And I need to extract (2n-1, 2n) columns from matrix.txt: ind1 A B C D E F G H I J K L M N O P Q R ... ind2 B C D E F G H... (3 Replies)
Discussion started by: Zoho
3 Replies
Login or Register to Ask a Question