Count number of match words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count number of match words
# 1  
Old 02-22-2012
Count number of match words

Input:
some random text SELECT TABLE1 some more random text
some random text SELECT TABLE2 some more random text
some random text SELECT TABLE3 some more random text
some random text SELECT TABLE1 some more random text


Output:
'SELECT TABLE1' 2
'SELECT TABLE2' 1
'SELECT TABLE3' 1

I am thinking about using
Code:
awk '/SELECT.*/ { [match]++ }'


Last edited by chitech; 02-22-2012 at 11:21 AM..
# 2  
Old 02-22-2012
Code:
cat your-file | sort | uniq -c

# 3  
Old 02-22-2012
Code:
$ awk '{a[$0]++;next}END{for (i in a){print i,a[i]}}' input.txt
SELECT TABLE1 2
SELECT TABLE2 1
SELECT TABLE3 1

---------- Post updated at 08:40 PM ---------- Previous update was at 08:39 PM ----------

Quote:
Originally Posted by Shirishlnx
Code:
cat your-file | sort | uniq -c

Don't use cat.

you can simply use,

Code:
sort filename | uniq -c

# 4  
Old 02-22-2012
Sorry I have miss something in the input file.

I don't think I can use sort and uniq

The new input:
some random text SELECT TABLE1 some more random text
some random text SELECT TABLE2 some more random text
some random text SELECT TABLE3 some more random text
some random text SELECT TABLE1 some more random text
# 5  
Old 02-22-2012
unless, you post the sample file and contents, we cannot give generic code.

Code:
$ awk '{for(i=1;i<=NF;i++){if($i~/SELECT/){a[$i" "$(i+1)]++;next}}}END{for (i in a){print i,a[i]}}' a.txt 
SELECT TABLE1 2
SELECT TABLE2 1
SELECT TABLE3 1

# 6  
Old 02-22-2012
Code:
perl -lne '(/(SELECT \w+)/)&&$x{$1}++;END{for(sort keys %x){print "$_ $x{$_}"}}' inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Count the number of files to delete doesnt match

Good evening, need your help please Need to delete certain files before octobre 1 2016, so need to know how many files im going to delete, for instance ls -lrt file_20160*.lis!wc -l but using grep -c to another file called bplist which contains the list of all files backed up doesn match... (7 Replies)
Discussion started by: alexcol
7 Replies

2. Shell Programming and Scripting

Gawk gensub, match capital words and lowercase words

Hi I have strings like these : Vengeance mitt Men Vengeance gloves Women Quatro Windstopper Etip gloves Quatro Windstopper Etip gloves Girls Thermobite hooded jacket Thermobite Triclimate snow jacket Boys Thermobite Triclimate snow jacket and I would like to get the lower case words at... (2 Replies)
Discussion started by: louisJ
2 Replies

3. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

4. Shell Programming and Scripting

problem to count number of words from file

hi every one i have written this simple shell for counting number of word that user need to find from file but i have get several error when run it. can someone tell me the problem ? echo "Enter the file name" read file echo "enter word" read word for i in \`cat $file` do if then... (1 Reply)
Discussion started by: nimafire
1 Replies

5. Shell Programming and Scripting

How to count words number for each paragraph?

Hi, I have file like this: >number1 dlejreoreltedgerere dgtrtryr >number2 dkfdjlgdjfeiflefjdfidlfjdifelfjefe fjdlfjdkfjdlfdjlfdjlfjdigeo gjelreoureofouererg >number4 dklfdjfoeueoruer fjeorueotueoirueorueorueore gjoeoueorueoreuroerueor joeuroerueorue How can i know how many words... (4 Replies)
Discussion started by: the_simpsons
4 Replies

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

7. Shell Programming and Scripting

Count the number of words in some subset of file and disregard others

Hi All, I have some 6000 text files in a directory. My files are named like 1.txt, 2.txt 3.txt and so on until 6000.txt. I want to count the "number of words" in only first 3000 of them. Any suggestions? I know wc -w can count the number of words in a text file. I am using Red Hat Linux. (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

8. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

9. Shell Programming and Scripting

Match and count the number of times

ile1 Beckham Ronaldo file2 Beckham Beckham_human Ronaldo Ronaldo_spain Ronaldo Ronaldo_brazil Beckham Beckham_manch Zidane Zidane_Fran Rooney Rooney_Eng Output shud be (1 Reply)
Discussion started by: cdfd123
1 Replies

10. UNIX for Dummies Questions & Answers

Comparing two files and count number of lines that match

Hello all, I always found help for my problems using the search option, but this time my request is too specific. I have two files that I want to compare. File1 is the index and File2 contains the data: File1: chr1 protein_coding exon 500 600 . + . gene_id "20532";... (0 Replies)
Discussion started by: DerSeb
0 Replies
Login or Register to Ask a Question