Count exact matched words

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Count exact matched words
# 1  
Old 05-18-2017
Count exact matched words

hi ,
i have a file test.dat which contains following data.

test.dat
Code:
XY|abc@xyz.com
XY|abc@xyz.com
ST|abc@xyz.com
ST|abc@xyz.com
ST|XYZ@abc.com
FK|abc@xyz.com
FK|STG@xyz.com
FK|abc@xyz.com
FK|FKG@xyz.com

i want to know the count of XY,ST,FK.
i.e XY = 2 , ST = 3 , FK = 4

I am trying with the below commands but didnt get the right count.

Code:
grep -o -w 'XY' test.dat|wc -w
grep -o -w 'ST' test.dat|wc -w
grep -o -w 'FK' test.dat|wc -w

need your advise.
thanks
# 2  
Old 05-18-2017
How about
Code:
grep -Eow "XY|ST|FK" file | sort | uniq -c
      4 FK
      3 ST
      2 XY

This User Gave Thanks to RudiC For This Post:
# 3  
Old 05-18-2017
Hello itzkashi,

Could you please try following and let me know if this helps you.

Solution 1st: In case you are NOT bothering about your Input_file's sequence.
Code:
awk -F"|" '{A[$1]++} END{for(i in A){print A[i],i}}'   Input_file

Solution 2nd: If you need output same as Input_file's sequence.
Code:
awk -F"|" '{if(!A[$1]){++i};B[i]=++A[$1] OFS $1} END{for(j=1;j<=i;j++){print B[j]}}'   Input_file
OR
awk -F"|" '!A[$1]{++i} {B[i]=++A[$1] OFS $1} END{for(j=1;j<=i;j++){print B[j]}}'   Input_file

Also in case you need to sort the output, then you could append | sort -k2 after Input_file, in any of the above codes.

Thanks,
R. Singh

Last edited by RavinderSingh13; 05-18-2017 at 02:44 PM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 05-18-2017
Code:
for i in XY ST FK; do echo "$i=$(grep -wc "$i" test.dat)"; done

More efficient is awk, like in the previous solution.
The following is a more principal solution of the "keep the order"
Code:
awk -F'|' 'C[$1]++==0 { A[++amax]=$1 } END { for (a=1; a<=amax; a++) print A[a]"="C[A[a]] }' test.dat


Last edited by MadeInGermany; 05-18-2017 at 03:32 PM.. Reason: corrected awk version
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 05-18-2017
Thanks Ravinder , solution 1st worked perfectly.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with sed command does not replace exact string matched

I have a file change.sed more change.sed I fire the below command inorder to replace "190.169.11.15" with "10.4.112.240" in proxy.logsed -f change.sed proxy.log proxy.log has the below entry more proxy.log The command replaces both 190.169.11.15 & 190.169.11.155 as below: I am expecting... (17 Replies)
Discussion started by: mohtashims
17 Replies

2. Shell Programming and Scripting

Extract between two Exact matched strings.

more data.txt i need this exacted from data.txt This is the command i tried sed -n "/Start_of_DISK_info:\/u/,/End_of_DISK_info:\/u/p" data.txtBut, unfortunately it does not do an exact match. Instead, it prints text between both these strings /u & /u/tmp like below. i need this... (6 Replies)
Discussion started by: mohtashims
6 Replies

3. Shell Programming and Scripting

Search and retreive after matched words

I have following source file format {"driver":{"first_name":"xxxx","last_name":"yyyy"},"confirmation_id":"US285204420","vendor":{"id":"1234","name":"BUDGET"}} I need to extract the data from above and provide in below desired output is xxxx,yyyy,US285204420,1234,BUDGET Can you please... (11 Replies)
Discussion started by: nqp200
11 Replies

4. Shell Programming and Scripting

Regex: print matched line and exact pattern match

Hi experts, I have a file with regexes which is used for automatic searches on several files (40+ GB). To do some postprocessing with the grep result I need the matching line as well as the match itself. I know that the latter could be achieved with grep's -o option. But I'm not aware of a... (2 Replies)
Discussion started by: stresing
2 Replies

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

6. Shell Programming and Scripting

Grep two words with exact match

HI Input : Counters Counter Int Ints Counters Counters Ints Ints I want to grep Counter|Int Output : Counter (1 Reply)
Discussion started by: pareshkp
1 Replies

7. Shell Programming and Scripting

Egrep with exact words

Hi, I have the following data in a file bob bobby sam sammy ed eddie I need to grep these exact words: "bob", "sam", "ed". I don't want to see "bobby", "sammy", "eddie". egrep 'bob|sam|ed' gives me everything because egrep interprets these as a pattern. How can I specify an exact... (8 Replies)
Discussion started by: almonds
8 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

Print two matched words from the same line

Hi experts I need to pick 2 matched words from the same line..... I have given below an example file eg: O14757 hsa04110 hsa04115 2 P38398 hsa04120 1 O15111 hsa04010 hsa04210 hsa04920 hsa04620 hsa04660 hsa04662 hsa05200 hsa05212 hsa05221 hsa05220 hsa05215 hsa05222 hsa05120 13 O14920... (4 Replies)
Discussion started by: binnybio
4 Replies

10. Shell Programming and Scripting

SED: delete and print the only exact matched pattern

I am really need help with the regular expression in SED. From input file, I need to extract lines that have the port number (sport or dport) as defined. The input file is something like this time=1209515280-1209515340 dst=192.168.133.202 src=208.70.8.23 bytes=2472 proto=6 sport=80 dport=1447... (6 Replies)
Discussion started by: new_buddy
6 Replies
Login or Register to Ask a Question