Searching a String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching a String
# 1  
Old 09-14-2011
Java Searching a String

Hi everyone !

suppose i'm searching for a specific string in a file so it is very easy, i use the following command
Code:
grep 'keyword' file_name

but how to search a word which is repeated maximum number of times in a file, for example in the following text i have to search a word which is repeated maximum number of times and that is john

""Hi John, how are you John, where have you been John, John is 14 years old""

what kind of technique do i have to use? is there any built in functions

Last edited by Franklin52; 09-15-2011 at 03:23 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 09-14-2011
Try:
Code:
perl -ln0e 'while (/\w+/g){$a{$&}++}@x=sort {$a{$b}<=>$a{$a}} keys %a;print $x[0]' file_name

# 3  
Old 09-14-2011
Do you mean count the words? or do you mean find John? or both?

Code:
awk '{ for (i=1;i<=NF;i++) {arr[$i]++}  }  END{ for(i in arr){print arr[i], i }} ' inputfile |
      sort -n > outputfile

output file has word counts, you can play with that. i.e.,
Code:
grep John outputfile

This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 09-14-2011
another funny idea:
Code:
 convert ", . ? !" to " "
 convert " " -> "\n"
then:
sort|uniq -c|sort|tail -n 1

# 5  
Old 09-15-2011
Java

Hi thanks but i guess you didn't understand my point

i want to search the code to find the word that is repeated maximum number of times.

for example i ask my program to find a word that is repeated maximum number of times and it bring the word that is repeated highest number of times.

---------- Post updated at 01:22 AM ---------- Previous update was at 01:13 AM ----------

Hey jim !

your code is very close to what i asked for but it has some problem it has printed "John" twice i don't know why

output file:

1 14
1 are
1 been
1 have
1 Hi
1 how
1 is
1 John
1 old
1 where
1 years
2 you
3 John

and it will be good if it only save the word which is repeated maximum number of times... thanks anyways

i'm trying on the code if i fixed it i'll post here if anyone else need it
# 6  
Old 09-15-2011
Code:
$nawk -F"[,.?\! ]" '{ for (i=1;i<=NF;i++) {arr[$i]++}  }  END{ for(i in arr){print arr[i], i }} ' input | sort -nr

This User Gave Thanks to itkamaraj For This Post:
# 7  
Old 09-15-2011
Java

Quote:
Originally Posted by itkamaraj
Code:
$nawk -F"[,.?\! ]" '{ for (i=1;i<=NF;i++) {arr[$i]++}  }  END{ for(i in arr){print arr[i], i }} ' input | sort -nr


it works fine... thanks everyone

hats off to itkamaraj
This User Gave Thanks to ourned For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching for variable string

Hi Guys, So im trying to search for the most recent occurance of a string in the most recently updated .log file in a certain directory. The string i am searching for is a value, now if this value is greater than 800 i would like an email sent out with some text (blah blah blah). This is what... (7 Replies)
Discussion started by: hello_world
7 Replies

2. Shell Programming and Scripting

searching the required string and appending string to it.

Hi all, I have some data in the form of adc|nvhs|nahssn|njadnk|nkfds in the above data i need to write a script so thet it will append "|||" to the third occurnace in the string ..... the outout should look like adc|nvhs|nahssn||||njadnk|nkfds Thanks, Firestar. (6 Replies)
Discussion started by: firestar
6 Replies

3. Shell Programming and Scripting

searching a text string for n'th :

hello, i'm a novice on bsh scripting so thanks for any help here basically i have a shell var $x that looks like this > echo $x nabc1234:!:73394:17155:Gary Mason:/home/garym:/bin/ksh and i'm trying to keep the first 8 characters and the text from the 4th : to the 5th : i've been trying... (9 Replies)
Discussion started by: sasglm
9 Replies

4. Shell Programming and Scripting

searching each file for a string

Hi Guys... I want to search for each file that contains a particular string. e.g find . -print | xargs grep -i string_name Now my issue is the files that I search in are gzipped. Will I be able to find the string, using the above commands, even if the files are gzipped? Please... (2 Replies)
Discussion started by: Phuti
2 Replies

5. UNIX for Dummies Questions & Answers

searching for a string in a file

I need to search for a specific string in a file and if this string exist I need to replace it with something else. I am not sure how I could do this, using an if statement. (2 Replies)
Discussion started by: ROOZ
2 Replies

6. UNIX for Dummies Questions & Answers

Searching for a string variable

Hi ... I have a string variable STR = "This is a test message" I have a file abc.txt that I am searching for the occurence of the string STR ... I am using the command in a script cat abc.txt | grep $STR It identifies each space as a seperator and prints word by word. How to... (2 Replies)
Discussion started by: mattrix
2 Replies

7. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

8. Shell Programming and Scripting

one more query for searching string.......

Dear friends, I have one more query, incase a file contains multiple tags of same name, then how to get the required string between the tags, in which the string begins with "O/M" i.e., file1.txt contains following text(please note that all the following tags are in single line)... (6 Replies)
Discussion started by: swamymns
6 Replies

9. UNIX for Dummies Questions & Answers

searching by string length

Hi, I'm rather new to Unix and I'm trying to write a simple script to search through a dictionary for words based on the letters a user would pass as arguments to the script. Now I have the searching part done. However, the one thig that still eludes me is that I want to only keep the... (4 Replies)
Discussion started by: GADO
4 Replies

10. UNIX for Dummies Questions & Answers

searching for a string in directory

Hi, I have a directory with a couple of thousand logs in it. The log files are created every 5 minutes. I want to search these logs grep for a specific sting and more importantly print the name of the files where that sting was found. e.g. all logs begin om20020927 what I have been... (4 Replies)
Discussion started by: warrend
4 Replies
Login or Register to Ask a Question