How to search and count strings?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to search and count strings?
# 1  
Old 10-16-2013
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 result? I tried grep but grep searches by line.

Feedback much appreciated. Thanks in advance.
# 2  
Old 10-16-2013
This will show the sum of hits on all lines in the input file:
Code:
$ cat infile
dog cat rat apple banana dog lion tiger dog
cat rat apple banana dog lion tiger dog
lion tiger dog
$ awk '{for(c=0;c<=NF;c++){if($c == "dog"){h++}}} END{print h}' RS= infile
6

This would show it per line:
Code:
$ awk '{for(c=0;c<=NF;c++){if(c==0){h=0}; if($c == "dog"){h++}}} {print h}' infile
3
2
1

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 10-16-2013
How about:-
Code:
tr " " "^J" < infile | grep -c dog

The ^J character is achieved by the key sequence CNTL-V then CNTL-J

The tr splits it into separate lines where it meets a space, then grep -c counts them.


I hope that this helps.

Robin
Liverpool/Blackburn
UK
# 4  
Old 10-17-2013
Quote:
Originally Posted by zaxxon
This will show the sum of hits on all lines in the input file:
Code:
$ cat infile
dog cat rat apple banana dog lion tiger dog
cat rat apple banana dog lion tiger dog
lion tiger dog
$ awk '{for(c=0;c<=NF;c++){if($c == "dog"){h++}}} END{print h}' RS= infile
6

This would show it per line:
Code:
$ awk '{for(c=0;c<=NF;c++){if(c==0){h=0}; if($c == "dog"){h++}}} {print h}' infile
3
2
1


Hi,

Thanks for your reply.

Testing it out gives below:

Code:
$: cat infile
dog cat rat apple banana dog lion tiger dog
cat rat apple banana dog lion tiger dog
lion tiger DOG
$: awk '{for(c=0;c<=NF;c++){if(c==0){h=0}; if(tolower($c) == "dog"){h++}}} {print h}' infile
3
2
0

Using nawk instead of awk gives 1 instead of 0. Shouldn't tolower work as well with awk?

Also, testing it with the oracle tnsping command.

Code:
$: tnsping testp1

TNS Ping Utility for Solaris: Version 11.2.0.2.0 - Production on 16-OCT-2013 16:18:06

Copyright (c) 1997, 2010, Oracle.  All rights reserved.

Used parameter files:

/db/testp1/dba/sqlnet/sqlnet.ora


Used TNSNAMES adapter to resolve the alias

Attempting to contact (DESCRIPTION =(LOAD_BALANCE=off)(FAILOVER=on)(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3) (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (Host = testp1prim.mnl.ph.com) (Port = 10666))) (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (Host = testp1stdby.mnl.ph.com) (Port = 10666))) (CONNECT_DATA = (SERVICE_NAME=testp1_app.mnl.ph.com)))
OK (10 msec)

$: tnsping testp1 | grep "Attempting"
Attempting to contact (DESCRIPTION =(LOAD_BALANCE=off)(FAILOVER=on)(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3) (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (Host = testp1prim.mnl.ph.com) (Port = 10666))) (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (Host = testp1stdby.mnl.ph.com) (Port = 10666))) (CONNECT_DATA = (SERVICE_NAME=testp1_app.mnl.ph.com)))

- Then I tried to grep for Host and expecting to get 2 but got 0 (zero) instead. I tried using nawk, it gives 0 (zero) as well. Any ideas?

Code:
$: tnsping testp1 | grep "Attempting" | awk '{for(c=0;c<=NF;c++){if(c==0){h=0}; if($c == "Host"){h++}}} {print h}'
0

- BTW, FYI kinda hoping to be able to parse the tnsping output and be able to reference each value as a shell variable, i.e. for example, echo $load_balance to give off, that's for another post I guess. For the moment, just want to be able to count for a string occurremce.

- Thanks again for your reply.
# 5  
Old 10-17-2013
Right, I forgot you needed it to be case insensitive.

I know, no big help but it worked for me:
Code:
$ cat infile
dog cat rat apple banana dog lion tiger dog
cat rat apple banana dog lion tiger dog
lion tiger DOG
$ awk '{for(c=0;c<=NF;c++){if(c==0){h=0}; if(tolower($c) == "dog"){h++}}} {print h}' infile
3
2
1

For parsing the other output with "Host", you should use something like this, because the word doesn't stand alone separated by awk's field separator:
Code:
$ awk '{for(c=0;c<=NF;c++){if(c==0){h=0}; if( tolower($c) ~ /host/ ){h++}}} {print h}' infile


Last edited by zaxxon; 10-17-2013 at 05:09 AM..
# 6  
Old 10-17-2013
Case insensitive version of my one-liner:-
Code:
tr " " "^J" < infile | grep -ic dog


Robin
# 7  
Old 10-17-2013
Another alternative (if you have GNU grep):
Code:
grep -oi dog infile | wc -l

This User Gave Thanks to CarloM For This Post:
 
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 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

2. 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

3. Shell Programming and Scripting

Search between two search strings and print the value

Based on the forums i have tried with grep command but i am unable to get the required output. search this value /*------ If that is found then search for temp_vul and print and also search until /*------- and print new_vul Input file contains: ... (5 Replies)
Discussion started by: onesuri
5 Replies

4. 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

5. Shell Programming and Scripting

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 TACGCGCGATA TATATATA GGCGCGTATA I would like to get an output like: 2 4 2 I have tried... (3 Replies)
Discussion started by: Manchesterpaul
3 Replies

6. Shell Programming and Scripting

Count no of occurrence of the strings based on column value

Can anyone help me to count number of occurrence of the strings based on column value. Say i have 300 files with 1000 record length from which i need to count the number of occurrence string which is existing from 213 to 219. Some may be unique and some may be repeated. (8 Replies)
Discussion started by: zooby
8 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