Word-counting and substitution with awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Word-counting and substitution with awk
# 1  
Old 06-24-2015
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:

Code:
 {$0 = tolower ($0)
 gsub (/[^[:alnum:]_[:blank:]]/, "", $0)
  for (i = 1; i <= NF; i++)
  freq[$1]++
  
  }
 
 END {
  for (word in freq)
  printf "%s\t%d\n", word, freq[word]

It seems work but I would like to exclude the articles (in Italiana: la, le, gli, lo), Do I have to use "gsub" another time? Like:
Code:
gsub (/la/, "", $0)

Then, I would like to substitute the overused words with synonyms which are in another file, how can I do? I have no idea.
Thank you
# 2  
Old 06-24-2015
What you call "word" might be a problematic concept:
Lasciate ogni speranza voi ch' entrate. (Dante)

How would your algorithm split that into words? Is "ch" a word as per your definition? And shouldn't it be counted as the same as "che"?

I hope this helps.

bakunin
# 3  
Old 06-24-2015
Quote:
Originally Posted by bakunin
What you call "word" might be a problematic concept:
Lasciate ogni speranza voi ch' entrate. (Dante)

How would your algorithm split that into words? Is "ch" a word as per your definition? And shouldn't it be counted as the same as "che"?

I hope this helps.

bakunin
not much
# 4  
Old 06-24-2015
Yes, use a second (or further) gsub. I think you can use the "alternate" (= or) regex operator | . Might carry some difficulties, e.g. upper case at the begin of a sentence, or trailing punctuation. Here I did this for the German words Der, der, die, das, dem, den, des (all with optional leading capital letter):
Code:
(^[dD]|[[:space:]][dD])(ie|e[mnrs]|as)[[:punct:][:space:]]

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

3. Shell Programming and Scripting

Help with word substitution

Hi all, I am trying to write a script substituting one word in a particular file with another word (sed) but I'm having trouble creating the backup file. The following are my instructions: The Unix program sed is useful for making simple substitutions throughout an entire file. But one of... (0 Replies)
Discussion started by: Hoppy56
0 Replies

4. Homework & Coursework Questions

Counting a particular word per line

1. The problem statement, all variables and given/known data: 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... (3 Replies)
Discussion started by: bigubosu
3 Replies

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

6. Shell Programming and Scripting

whole word substitution in SED

I am trying to substitute something with sed and what I want is to substitute a whole word and not part of a word. ie sed 's/class/room/g' filename will substitute both class and classes into room and roomes which is not what i want Grep for instance can use the -w option or <> grep -w... (7 Replies)
Discussion started by: gikay01
7 Replies

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

8. Linux

Last word substitution

Ok this is last question of the day from my side . I have this file and I want to replace the last letter " , " with " ) " . The input file is #cat zip.20080604.sql CONNECT TO TST103 ; SET SESSION_USER OPSDM001 ; SET CURRENT SCHEMA OPSDM001 ; CREATE VIEW OPSDM001.vw_zip SELECT ( ... (4 Replies)
Discussion started by: capri_drm
4 Replies

9. Linux

word substitution in unix

Hi I am trying to substitute 2 words on the same line with _S02 as suffix. Like this . IN "TSOPS09" INDEX IN "TSOPIX09" ; to IN "TSOPS09_S02" INDEX IN "TSOPIX09_S02" ; i used the following code to make the change , it works fine for first substitution not the second one . ... (6 Replies)
Discussion started by: capri_drm
6 Replies

10. Shell Programming and Scripting

word substitution in csh

I have data that looks something like this: term1/term2/2005-12-01 13:20:30/term4 I need to make it look like this: term1/term2/20051201132030/term4 I am using a csh script. I have tried to do it by first converting the date/time to the format in which I want it, and then replacing it... (1 Reply)
Discussion started by: oprestol
1 Replies
Login or Register to Ask a Question