Finding occurrance of a value in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding occurrance of a value in a variable
# 1  
Old 11-12-2005
Finding occurrance of a value in a variable

Hi,

I was given a question like this:

Write a program which reads a set of arguments from the standard input. Note
that there is no limit for the number of arguments. The first argument is the file
name and the other arguments are searched in the given file. For each argument, it displays the lines of the file which include the argument and it displays the number of times the argument exists in the file. This process is repated for the other arguments. You need to consider error messages for the case of non-existing file, non-existing argument(s) and missing argument.

I store the parameters in an array and decided the process the file line by line. Using head and tail, I get each line, but I can't come to a conclusion that if the word has an occurrance in that line.

For example, our file is:

Yesterday I played basketball.
I love basketball.
Basketball is good.

And we take the first line "Yesterday I played basketball." Assuming that we are looking for word "basketball", how can I state that "basketball" exists in that line? (and how many times)

Regards,
ambitious

Last edited by ambitious; 11-12-2005 at 06:39 AM..
# 2  
Old 11-12-2005
You might want to review the rules of these forums, and in particular

(6) Do not post classroom or homework problems.
# 3  
Old 11-12-2005
No, I don't want the problem to be solved. I just want to find occurrances of a word in a variable.
# 4  
Old 11-12-2005
Wouldn't that be the same as solving the problem? Anyways, you can use grep to solve the problem. Look up the man page for grep on your unix system.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error in finding the PID by grep and assigning to a variable

Hello All, I am facing difficulty in getting the PID value & then assigning it to a variable, kindly help me in resolving the issue. Thanks a lot in advance. The custom utility used inside the Test2.sh will process the file in a batch of 10 lines at once and for efficient memory management,... (3 Replies)
Discussion started by: duddukuri
3 Replies

2. Shell Programming and Scripting

Finding position of space in a variable

HI All, am trying to find the position of space in a variable, it is working for other characters other than space ulab="ulab1|ulab2" find_pos=`expr index $ulab '|'` echo $find_pos above code worked fine but below one says syntax error ulab="ulab ulab2" find_pos=`expr index $ulab ' '`... (2 Replies)
Discussion started by: ulab
2 Replies

3. UNIX for Dummies Questions & Answers

Finding WHERE an environment variable is defined.

HI. I had an environment variable defined in my .bash_profile file and I deleted it. I opened a new terminal and looked inside .bash_profile and it's not there but when I type printenv MYCONFIG it reutrns what the value used to be in my .bash_profile still!! I did a grep for MYCONFIG and... (6 Replies)
Discussion started by: dissectcode
6 Replies

4. Shell Programming and Scripting

Finding Variable Value defined in a Script

Hi , I had a script in which there is a variable $LOG defined and I want to check where that variable is getting value from, is there a way I can check for that variable and it's value if it is defined in some other script function or script . I checked using command env and also export... (1 Reply)
Discussion started by: somu_june
1 Replies

5. Shell Programming and Scripting

Finding PID of a process using variable substituition

Hi All, Am copying mulitple files in a directory in names File0,File1,File2 etc. I need to print separately the PID of these copies using File names. for((i=0;i<5;i++)) do mypid=`ps aux | awk '/File$i/ && !/awk/ { print $2 }'` echo PID is $mypid done It printed nothing. Thinking... (6 Replies)
Discussion started by: amio
6 Replies

6. UNIX for Dummies Questions & Answers

Finding files that midify the PS1 variable.

Any help developing a command to find all files on the system named ".bashrc" that modify the PS1 variable. I wanna list the full file name, including the full path, and protection. Nothing else. (2 Replies)
Discussion started by: raidkridley
2 Replies

7. Shell Programming and Scripting

RegExp: From first occurrance to last (at line start)

So I have a log that contains something like this: What I want is to get the first occurrence of "^localhost0" (at line start) and then everything else up to the last occurrence of "^localhost0" (at line start) Ideally I don't even care about the 2 "localhost0" lines either. All I really... (6 Replies)
Discussion started by: jjinno
6 Replies

8. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies

9. Shell Programming and Scripting

Finding Occurence of comma in a Variable ( KORN Shell)

All I want to find the occurence of comma in a variable in KORN shell. For example : var = test,test2,test3 .... finding occurence of comma in this variable. Result = 3 now. Please help me to write the code. Thanks in advance. Regards Deepak (2 Replies)
Discussion started by: DeepakXavier
2 Replies

10. Shell Programming and Scripting

Finding out the length of a string held within a variable

:confused: Does anyone know which command I can use to find out the length of a string held within a variable? (5 Replies)
Discussion started by: dbrundrett
5 Replies
Login or Register to Ask a Question