Count the words starting with 3-


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count the words starting with 3-
# 1  
Old 10-26-2016
Count the words starting with 3-

OS : Oracle Linux 6.5
Shell : bash


I have a file whose contents look like below. I want to count the number of occurences of strings starting with 3-.
How can I do this ? I couldn't wordwrap the below line. Hence it looks long.

Code:
'3-90892405251', '3-90892911050', '3-90893144163', '3-90893078309', '3-90893078518', '3-90892991593', '3-90892799260', '3-90893142987', '3-90897712667', '3-90897799595', '3-90897854949', '3-90897855168', '3-90897979833', '3-90897809578', '3-90898043808', '3-90897876904', '3-90897675968', '3-90898094426', '3-90897361488', '3-90898143959', '3-90897726122', '3-90897967169', '3-90898100366', '3-90898157920', '3-90898123121', '3-90898017415', '3-90898017800', '3-90897736679', '3-90898239030', '3-90898203678', '3-90898264512', '3-90892204001',
.
.
.
.

---------- Post updated at 09:35 AM ---------- Previous update was at 09:15 AM ----------

The following count gave me wrong output

Code:
 
  
 $ wc -w '3-*' SomePattern.txt
wc: 3-*: No such file or directory
 23441 SomePattern.txt
 23441 total
  
 $ wc -w '3-' SomePattern.txt
wc: 3-: No such file or directory
 23441 SomePattern.txt
 23441 total

# 2  
Old 10-26-2016
Can you try like the following ?

Code:
awk -F\' '{for(i=1;i<=NF;i++) if ($i ~ "3-") a++ } END { print a+0 } file

This User Gave Thanks to greet_sed For This Post:
# 3  
Old 10-26-2016
When I tried to execute the following. I get the > character in the next lines. I think some command is incomplete.
What is the technical word for > character in this case ?
Code:
$ awk -F\' '{for(i=1;i<=NF;i++) if ($i ~ "3-") a++ } END { print a+0 } SomePattern.txt
>
>
>

# 4  
Old 10-26-2016
Missing single quotes Red

Code:
$ awk -F\' ' '{for(i=1;i<=NF;i++) if ($i ~ "3-") a++ } END { print a+0 } ' SomePattern.txt


Last edited by Scrutinizer; 10-27-2016 at 01:53 AM..
This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 10-26-2016
Hello John K,

Could you please try following and let me know if this helps you.
Code:
awk '{num=gsub(/3-/,X,$0);print num}'   Input_file


Hello greet_sed,

Solution could be changed to as following.
Code:
awk -F"'" '{for(i=1;i<=NF;i++) if ($i ~ /3-/) a++ } END { print a+0 }'  Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 10-26-2016
Thank You very much Ravinder
Thank you greet_sed, JimMcnamara

---------- Post updated at 10:48 AM ---------- Previous update was at 10:41 AM ----------

One more question:
Is there a way I could skip duplicates (strings starting with 3-) while doing the count ?
# 7  
Old 10-26-2016
Code:
sed 's/ *, */\n/g' infile | grep "'3-" | sort -u | wc -l

This User Gave Thanks to rdrtx1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search for words starting and ending with

im trying to search for a WORD in a file which begins with a number followed by a hypen follwed multiple words and end with a dot "." and pront the entire line which matches the above. Please note that there is a space at the begining of each line i/p file 19458 00000-CONTROL-PARA.... (5 Replies)
Discussion started by: anijan
5 Replies

2. Shell Programming and Scripting

Count words from file

hi all how to count words from a text aaa bbb ccc ddd 123 aaa 123 aaa aaa ddd 123 i need to cout hoe many time the words "aaa" and "123" each appears the output should be 4 3 or 4 3 or aaa 4 123 3 thanks (10 Replies)
Discussion started by: sharong
10 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

Print words starting at particular positions

I have three words say abc def and ghi. I want to write them in a text file each starting (not ending) at particular positions say 1, 42 and 73. It is guaranteed that they will not overwrite. Length of the three variable may vary. Any help? (5 Replies)
Discussion started by: Soham
5 Replies

5. Shell Programming and Scripting

Extract words starting with a pattern from a file

Hi Guys.. I have a file and i want to extract all words that starts with a pattern 'ABC_' or 'ADF_' For example, ABC.txt ---- INSERT INTO ABC_DLKFJAL_FJKLD SELECT DISTINCT S,B,C FROM ADF_DKF_KDFJ_IERU8 A, ABC_LKDJFREUE9_FJKDF B WHERE A.FI=B.EI; COMMIT; Output : ABS_DLKFJAL_FJKLD,... (5 Replies)
Discussion started by: Pramod_009
5 Replies

6. UNIX for Dummies Questions & Answers

Count of rows starting with 01 in a file

Hi I am very new to unix and please help me in solving the below problem I have a file with 50000+ rows. Each row(line) start with 01 or 02 or 03. Now i have to get the count of rows starting with 01 only, Thanks in advance (3 Replies)
Discussion started by: nmakkena
3 Replies

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

8. Shell Programming and Scripting

Count words

Hi, does anyone know the command to count words by its length. I need to Count the number of five letter words that I have in a file with thousand of words. thanks (3 Replies)
Discussion started by: fabioamaury
3 Replies

9. Shell Programming and Scripting

How to remove all words starting from a matching word in a line

Hi Guys, I have a file like this: wwwe 1 ioie ewew yyy uuu 88 erehrlk 4 ihoiwhe lkjhassad lkhsad yyy mmm 45 jhash lhasdhs lkhsdkjsn ouiyrshroi oihoihswodnw oiyhewe yyy ggg 77 I want to remove everything after "yyy" and including "yyy" from each line in the file. So I want:... (2 Replies)
Discussion started by: npatwardhan
2 Replies

10. Shell Programming and Scripting

count no of words in a line

hi i have a string like str=abc def ghi jkl now i want to count the no of words in the string please help (7 Replies)
Discussion started by: satish@123
7 Replies
Login or Register to Ask a Question