Counting a particular word per line

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Counting a particular word per line
# 1  
Old 04-07-2010
Counting a particular word per line

1. The problem statement, all variables and given/known data:
Code:
It was the best of times, it was the worst of times,
It was the age of wisdom, it was the age of foolishness,
It was the epoch of belief, it was the epoch of incredulity,
It was the season of Light, it was the season of Darkness,
It was the spring of hope, it was the winter of despair,
we had everything before us, we had nothing before us,
/*we were all going direct to heaven, we were all going direct the other  way in short, the poeriod was so far like the present period,*/
that some of its noisiest authorities insisted on its being received,
for good or for evil, in the superlative degree of comparison only.

Find the line that contains the word 'the' 3 times.

2. Relevant commands, code, scripts, algorithms:
can only use tee, tr, grep, head, tail, sort, uniq, cut, paste, join and cat


3. The attempts at a solution (include all code and scripts):
I've got a general idea of how to do it but i don't know how to put it all into a command. The marked /**/ line above is the line i should be printing out as it contains 3 "the".

This is not the first question in my lab but its the 6th. The previous question don't ask to analyse lines, rather count words with 10 letters, turn upper case to lower case. list unique words only once.

So my main problem is knowing how to check each line for the word 'the' occurring 3 times.


4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
FIT3042 Monash, Australia, Victoria, Computer Science, John Hurst.

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Last edited by bakunin; 04-07-2010 at 12:50 PM.. Reason: provided some code-tags for free
# 2  
Old 04-07-2010
well, if it's a matter of exactly 3 occurs of the string " the " and not to include the "other" string:

Code:
grep -n " the " file_test.txt |while read LINE
do
print "\n$LINE\n"
typeset -i count=0
echo $LINE |tr " " "\n" |grep -v "other" |grep -c 'the'
count=$(( $count+1 ))
done

otherwise, if it's a matter of at least 3 occurs:

Code:
grep -n " the " file_test.txt |while read LINE
do
print "\n$LINE\n"
typeset -i count=0
echo $LINE |tr " " "\n" |grep -c 'the'
count=$(( $count+1 ))
done

# 3  
Old 04-07-2010
Sorry i should've said that im only supposed to do this over 1 line or just as a command. I'm fairly new to using unix tools please forgive me.

so for example listing hte unique words in the file would be

Code:
cat <filename> | tr " " "\n" + sort + uniq

for future replies

Last edited by bakunin; 04-07-2010 at 12:51 PM.. Reason: same here: please use code-tags
# 4  
Old 04-07-2010
as a one-liner...

Code:
grep -n " the " file_test.txt |while read LINE ;do count=0 ;print "$(echo $LINE)--> $(echo $LINE |tr " " "\n" |grep -v "other" |grep -c 'the' ;count=$(( $count+1 )) ;done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Counting Word Appearance

How do you write a script that counts the number of times a word appears in a file and output it? Original: ID1 SMARCB1;Adil;Jon ID2 Jon;Annie;Mei ID3 Adil;Spaghetti;NBA ID4 Raptors;wethenorth;SMARCB1 ID5 SMARCB1;wethenorth Objective: SMARCB1: 3 Adil: 2 Jon: 2... (5 Replies)
Discussion started by: Lipidil
5 Replies

2. UNIX for Dummies Questions & Answers

Word-counting and substitution with awk

Hi!! I am trying to write a program which allows me to count how many times I used the same word in a text: {$0 = tolower ($0) gsub (/_]/, "", $0) for (i = 1; i <= NF; i++) freq++ } END { for (word in freq) printf "%s\t%d\n", word, freq It seems work but... (3 Replies)
Discussion started by: ettore8888
3 Replies

3. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

4. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

5. Shell Programming and Scripting

Word counting perl script

Hi friends i need a help on Perl Script In My Home directory, i have some other directories and inside those directories i have some subdirectories and all the directories contains files. Now i want to count a word in all files and i want the output like below wordcount in which file(name... (5 Replies)
Discussion started by: siva kumar
5 Replies

6. UNIX for Dummies Questions & Answers

BASH - Counting word occurrences in a Web Page

Hi all, I have to do a script bash (for university) that counts all word occurrences in a specific web page. anyone can help me?. Thanks :) (1 Reply)
Discussion started by: piacentero
1 Replies

7. Shell Programming and Scripting

counting word xx referred to a time period, like minute or hour

Hello, I try to insert a post because I've got a trouble to perform a unix job. But I didn't found which steps (procedure) I should follow. Could you help me? I got a log by my Application box, like following: gbosmam037:test >view Log_Server.csv ... (2 Replies)
Discussion started by: maluca68
2 Replies

8. Shell Programming and Scripting

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 Replies

9. UNIX for Dummies Questions & Answers

counting the occurence of a word

In a file I have to count a particular word. like i need apache how many times. I tried this $ tr "\011" "\012\012"<foo1 | tr -cd "" |sort\uniq -c but I got result like this 32 apache 18 dns 12 doctor Please sugest me (4 Replies)
Discussion started by: pranabrana
4 Replies

10. Linux

option of grep for counting exact word ??

Hi All, I have a quary regarding grep command in linux. I have a file which contains 56677 56677 +56677 +56677 56677 56677 56677 I want to extract total count of "56677" When I hit the following command #cat filename | grep -w -c '56677' the result comes 7. Its counting... (3 Replies)
Discussion started by: maddy
3 Replies
Login or Register to Ask a Question