Count number of occurrence at each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count number of occurrence at each line
# 1  
Old 05-28-2012
Question Count number of occurrence at each line

Hi

I have the following file
Code:
ENST001 ENST002 4 4 4 88 9 9
ENST004 3 3 3 99 8 8
ENST009 ENST010 ENST006 8 8 8 77 8 8

Basically I want to count how many times ENST* is repeated in each line so the expected results is
Code:
2
1
3

Any suggestion please ?

Last edited by Scrutinizer; 05-29-2012 at 06:23 AM.. Reason: Please use code tags for data and code samples, thank you - mod: also data file
# 2  
Old 05-28-2012
Hi


Code:
$ awk '{for(i=1;i<=NF;i++)if($i ~ /^ENST/)x++;print x;x=0}' file
2
1
3

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 05-28-2012
Code:
perl -lne 'print s/ENST//g' file

This User Gave Thanks to balajesuri For This Post:
# 4  
Old 05-29-2012
awk

Code:
awk '{n=gsub(/ENST/,'ENST',$0);print n}' file


Last edited by Scrutinizer; 05-29-2012 at 06:24 AM.. Reason: code tags
# 5  
Old 05-29-2012
Code:
awk -FENST '{print NF-1}' file

Code:
awk '{print gsub(/ENST/,x)}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count the pipes "|" in line and delete line if count greter then number.

Hello, I have been working on Awk/sed one liner which counts the number of occurrences of '|' in pipe separated lines of file and delete the line from files if count exceeds "17". i.e need to get records having exact 17 pipe separated fields(no more or less) currently i have below : awk... (1 Reply)
Discussion started by: ketanraut
1 Replies

2. Shell Programming and Scripting

Want to count the number of lines after the first line

hi, How can i count the number of lines after the first line in a flat file in unix? Say i have a flat file with a header like: Student Name Student ID .... Tnx (7 Replies)
Discussion started by: reignangel2003
7 Replies

3. Shell Programming and Scripting

How to count the occurrence of a number?

Hi, I have a file which contained a set of numbers like Col1 col2 col3 col4 1 sa 13 0 2 sb 14 0 3 sc 15 9 4 sd 16 -9 5 sd 20 -2 6 sd 20 4 Here in last column I need to count the zeros, positive values and negative values, please help me to do that. (2 Replies)
Discussion started by: Shenbaga.d
2 Replies

4. Shell Programming and Scripting

Count number of occurrence of a string in file

if there's a file containing: money king money queen money cat money also money king all those strings are on one line in the file. how can i find out how many times "money king" shows up in the line? egrep -c "money king" wont work. (7 Replies)
Discussion started by: SkySmart
7 Replies

5. Shell Programming and Scripting

How to count the number of occurrence of words from multiple files?

File 1 aaa bbb ccc File 2 aaa xxx zzz bbb File 3 aaa bbb xxx Output: (4 Replies)
Discussion started by: Misa-Misa
4 Replies

6. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

7. Shell Programming and Scripting

Shell script to count number of ~ from each line and compare with next line

Hi, I have created one shell script in which it will count number of "~" tilda charactors from each line of the file.But the problem is that i need to count each line count individually, that means. if line one contains 14 "~"s and line two contains 15 "~"s then it should give an error msg.each... (3 Replies)
Discussion started by: Ganesh Khandare
3 Replies

8. UNIX for Dummies Questions & Answers

line number of the i-th occurrence of a pattern

Hi all, is there a simple way to obtain the line number of the i-th occurrence of a pattern? I have OCCURRENCE=`grep -io "${STRING_NAME}" ${1}-${8}${EXT}.out_bis| wc -l` which tells me how many occurency I have. I would like to go through them and determine the line number and assign... (6 Replies)
Discussion started by: f_o_555
6 Replies

9. UNIX for Dummies Questions & Answers

Line number of an occurrence using grep

Hi All, is there a way to extract the line number of an occurrence using grep? I know that with the -n option it prints out the line number as well. I would like to assign the line number to a variable. Thanks, Sarah (5 Replies)
Discussion started by: f_o_555
5 Replies

10. Shell Programming and Scripting

Count the number of occurrences of a pattern between each occurrence of a different pattern

I need to count the number of occurrences of a pattern, say 'key', between each occurrence of a different pattern, say 'lu'. Here's a portion of the text I'm trying to parse: lu S1234L_149_m1_vg.6, part-att 1, vdp-att 1 p-reserver IID 0xdb registrations: key 4156 4353 0000 0000 ... (3 Replies)
Discussion started by: slipstream
3 Replies
Login or Register to Ask a Question