Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 03-06-2010, 11:35 PM   #1
LQ Newbie
Registered: Mar 2010
Posts: 8

Rep: Reputation: 0
Question Help Needed - How to count an item in a tab-delimited txt file?

Hi, I got a question and need some help:

in a tab-delimited file, such as:
1	11	a	I	ab
2	22	b	II	bc
3	33	c	III	cd
4	33	d	IV	de
5	55	e	V	ef
6	66	f	VI	fg
7	77	g	VII	gh
8	88	h	VIII	hi
how can I count the number of occurrence of an item, such as 33 (twice)?

Thank you very much for your help!
Old 03-07-2010, 12:05 AM   #2
LQ Newbie
Registered: Mar 2010
Posts: 3

Rep: Reputation: 0
Hi Jason,

you may not be very familiar with cool linux command line stuff.

I think the following will do the trick

cat fileName | awk '{if ($2 == 33) print $2};' | wc -l

What this does is first print the file, cat fileName
pipe the result of that file to awk, a command line programming language

awk '{if ($2 == 33) print $2};'

checks if the second column is 33, the value u want, if it is, print it. (otherwise ignore)
similarily, $1 is first column, $4 is fourth column, $0 is entire line.

finally, use wc -l to check the number of lines, the number of occurrence of the number you want.

hope it helps

Last edited by simitra; 03-07-2010 at 12:07 AM.
Old 03-07-2010, 06:36 AM   #3
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184

Rep: Reputation: 41
grep -o "occurrence" sourcefile|wc -l
Old 03-07-2010, 09:20 AM   #4
LQ Newbie
Registered: Mar 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Thank you sooooo much guys!

I am really a newbie in linux, just started learning for couple of days. Hope I can pick it up without too much struggles.


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
searching pattern in tab delimited file using grep vaibhavs17 Programming 5 03-05-2010 01:02 AM
Help needed in removing intermediate segments from a pipe delimited segment file naren_0101bits Programming 12 12-03-2007 10:47 AM
Working with Tab-Delimited files shiroh_1982 Linux - Newbie 5 06-02-2006 04:08 AM
Parsing a tab delimited text file jajanes Programming 9 08-08-2003 10:34 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:58 AM.

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