LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-03-2008, 07:23 AM   #1
Swapna173
LQ Newbie
 
Registered: Dec 2007
Posts: 25

Rep: Reputation: 15
Find Number of column in a file


Hi Guys,

I need help in finding the Number of column in a file with comma as the delimiter.

Regards,
Swapna
 
Old 06-03-2008, 07:46 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Using awk you can try
Code:
awk -F, 'END{print NF}' file
provided that each line has the same number of columns. This command print out the number of fields (delimited by comma) of the last row of the file.
 
Old 06-03-2008, 07:56 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
sed 's/[^,]//g' filename | wc -c

this gives the total # of commas PLUS the number of "newlines". But--it's a start.

Do you really want the total number of columns, or the number of columns per line?

Is this homework?
 
Old 06-04-2008, 01:29 AM   #4
Swapna173
LQ Newbie
 
Registered: Dec 2007
Posts: 25

Original Poster
Rep: Reputation: 15
Smile Thanks

Thanks a lot friends. it worked with awk command. Reply Posted by : colucix.

Actually I wanted the number of fields in the file which are comma separated.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to append columns form a column file in another file adam_blackice Programming 4 09-17-2007 11:33 PM
How can I find the AG number of a certain file?(XFS) hxsrmeng Linux - Newbie 0 09-14-2007 12:21 PM
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 07:07 AM
How to find out the number of open file descriptors? skie_knite007 Programming 2 12-11-2005 10:23 PM
help on finding the number that has max occurrences in a column [sql query] zeppelin Programming 6 06-15-2004 01:41 PM


All times are GMT -5. The time now is 03:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration