How do I count strings on each line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I count strings on each line?
# 1  
Old 01-27-2013
How do I count strings on each line?

Hi

Im a very inexperienced bioinformatician

I have a large DNA file with about 10000 lines of sequence and need to count the occurrence of TA for each line

for example in the file

Code:
TACGCGCGATA
TATATATA
GGCGCGTATA

I would like to get an output like:

Code:
2
4
2

I have tried various grep and awk commands but can only get total number of TA in the whole file

many thanks

Last edited by Scrutinizer; 01-27-2013 at 02:40 PM.. Reason: code tags
# 2  
Old 01-27-2013
Try
Code:
$ awk '{print gsub("TA","TA")}' file
2
4
2

If you want it a bit more flexible, use a variable like
Code:
$ awk '{print gsub(X,X)}' X="TA" file

This User Gave Thanks to RudiC For This Post:
# 3  
Old 01-27-2013
works perfectly - many thanks!
# 4  
Old 01-27-2013
Alternative:
Code:
awk -F TA '{print NF-1}' file

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

Compare file1 header count with file2 line count

What I'm trying to accomplish. I receive a Header and Detail file for daily processing. The detail file comes first which holds data, the header is a receipt of the detail file and has the detail files record count. Before processing the detail file I would like to put a wrapper around another... (4 Replies)
Discussion started by: pone2332
4 Replies

3. Shell Programming and Scripting

Count the occurences of strings

I have some text files in a folder f1 with 10 columns. The first five columns of a file are shown below. aab abb 263-455 263 455 aab abb 263-455 263 455 aab abb 263-455 263 455 bbb abb 26-455 26 455 bbb abb 26-455 26 455 bbb aka 264-266 264 266 bga bga 230-232 230 ... (10 Replies)
Discussion started by: gomez
10 Replies

4. Shell Programming and Scripting

Count the number of strings

I have 500 text files in a folder. The data of the text files are shown below. USA Germany 23-12 USA Germany 23-12 USA Germany 23-12 France Germany 15-12 France Germany 15-12 France Italy 25-50 China China 30-32 China China 30-32 I would... (1 Reply)
Discussion started by: sahith
1 Replies

5. UNIX for Dummies Questions & Answers

How to search and count strings?

Hi, Is there a command to do a sensitive/in-sensitive search for a string on a line and print how many times that string appears? For example, if I have a line of text below: dog cat rat apple banana dog lion tiger dog Is there a command to search for dog that will print out 3 as a... (7 Replies)
Discussion started by: newbie_01
7 Replies

6. Shell Programming and Scripting

How to count the number of strings?

Hi, I have a text file as shown below. I would like to count the unique number of connections of each person in the first and second column. Third column is the ID numbers of first column persons and fourth column is the ID numbers of second column persons. susan ali 156 294... (7 Replies)
Discussion started by: mohamad
7 Replies

7. UNIX for Dummies Questions & Answers

Count the number of strings in a block

Hi, I have the following text in a file: ISA*00* *00* *ZZ*ENS_EDI *ZZ*GATE0215 *110106*2244*U*00401*006224402*1*P*>~ GS*HP*ENS_EDI*GATE0215*20110106*2244*6224402*X*004010X091A1~ ST*835*00006~... (2 Replies)
Discussion started by: donisback
2 Replies

8. Shell Programming and Scripting

count identical strings print last row and count

I have a sorted file like: Apple 3 Apple 5 Apple 8 Banana 2 Banana 3 Grape 31 Orange 7 Orange 13 I'd like to search $1 and if $1 is not the same as $1 in the previous row print that row and print the number of times $1 was found. so the output would look like: Apple 8 3 Banana... (2 Replies)
Discussion started by: dcfargo
2 Replies

9. Shell Programming and Scripting

How to count unique strings

How do I count the total number of unique strings from a file using Perl? Any help is appreciated.. (6 Replies)
Discussion started by: my_Perl
6 Replies

10. Shell Programming and Scripting

Count strings on single line?

I use grep -c often, but cannot for the life of me count the number of occurences of a string on the same line (or within a file): $ cat myfile hello457903485897hello 34329048hellojsdfkljlaskdjgh182390 $ grep -c 2 $ How do I count the number of occurences of "hello" in myfile (i.e. 3)?... (6 Replies)
Discussion started by: cs03dmj
6 Replies
Login or Register to Ask a Question