The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
RegExp: From first occurrance to last (at line start) jjinno Shell Programming and Scripting 6 01-08-2008 04:04 PM
finding duplicate files by size and finding pattern matching and its count jerome Sukumar Shell Programming and Scripting 2 12-01-2006 04:20 AM
Finding Occurence of comma in a Variable ( KORN Shell) DeepakXavier Shell Programming and Scripting 2 11-14-2005 04:44 AM
ksh: A part of variable A's name is inside of variable B, how to update A? pa3be Shell Programming and Scripting 4 03-30-2005 12:29 PM
Finding out the length of a string held within a variable dbrundrett Shell Programming and Scripting 5 02-19-2002 08:02 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-12-2005
ambitious ambitious is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 2
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 (permalink)  
Old 11-12-2005
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,245
You might want to review the rules of these forums, and in particular

(6) Do not post classroom or homework problems.
  #3 (permalink)  
Old 11-12-2005
ambitious ambitious is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 2
No, I don't want the problem to be solved. I just want to find occurrances of a word in a variable.
  #4 (permalink)  
Old 11-12-2005
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0