LinuxQuestions.org
Help answer threads with 0 replies.
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 11-30-2009, 03:57 PM   #1
cliffyao
LQ Newbie
 
Registered: Oct 2009
Posts: 27

Rep: Reputation: 15
How to check the total number of columns in a text file


Hi, all

I have a .txt file which has many many tab-delimited columns. I wonder if there is any command like "wc -l" that can tell me the total number of columns in this file.

All your help will be greatly appreciated.

-Cliff
 
Old 11-30-2009, 04:14 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
awk -F\t 'BEGIN{print NF}' file.txt

should do it.
 
Old 11-30-2009, 04:15 PM   #3
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
This could be more cleanly done in perl, but this method has pretty low overhead.

Code:
head -n1 test |  sed 's/\t/\n/g' | wc -l
Could also be done using a single awk/sed line, but I don't have time to test at the moment.
 
Old 11-30-2009, 04:27 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Could not find something in less time than it took to write it:

linenum = the number of the line to test

Code:
sed -n "${linenum}s/\t/\n/gp" | wc -l <filename >newfilename
subtract 1 to get the number of tabs

<<EDIT: Other answers here are better>>

Last edited by pixellany; 11-30-2009 at 04:31 PM.
 
Old 11-30-2009, 06:33 PM   #5
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
My using awk:
Code:
awk '{print split($0,a,"\t"); exit}' testfile
 
Old 11-30-2009, 06:38 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
with awk,
Code:
awk -F"\t" '{print NF;exit}' file
or the shell
Code:
$ exec 4<"file"
$ read -r line <&4
$ IFS=$'\t'
$ set -- $line
$ echo "Number of columns: $#"
$ exec >&4-

Last edited by ghostdog74; 11-30-2009 at 08:01 PM.
 
Old 11-30-2009, 06:47 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by acid_kewpie View Post
awk -F\t 'BEGIN{print NF}' file.txt

should do it.
NF is not yet initialized in BEGIN block
 
Old 12-01-2009, 12:30 AM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
No, of course it's not. My bad. I just didn't want to print it for every line, thought it'd just take the first, but sure, exiting after the first line is clearly the way to do that.
 
Old 12-01-2009, 04:42 PM   #9
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
Quote:
Originally Posted by ghostdog74 View Post
with awk,
Code:
awk -F"\t" '{print NF;exit}' file
The number of fields does not reflect the fact you can have null values in a column. Suppose in the first row one or more values are empty and you have two consecutive tabs: awk and many other commands/languages ignore them and count the actual number of fields. In this way you don't retrieve the actual number of columns.

On the other hand, if the file has not empty (null) values and all columns are filled up, we can simply use:
Code:
head -1 testfile | wc -w
 
Old 12-01-2009, 04:49 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
That's a possibility, but only if the cols are single valued ... if not you have to count the tab chars.
 
  


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
sorting text file on different columns in different orders arshadul Linux - Newbie 1 10-19-2009 02:15 PM
how to make several columns in a text file of different lengths the same length Mike_V Programming 3 04-23-2009 09:17 PM
delete columns 15 to 27 of a text file powah Programming 1 01-31-2007 02:03 AM
How many total TCP sockets can my linux box have? how can I check the number used? abefroman Linux - Networking 2 11-09-2005 03:11 PM
How can I see the total number of file locks being currently used? abefroman Linux - Software 1 11-08-2005 06:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:13 AM.

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