The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
word count wc chaandana UNIX for Dummies Questions & Answers 5 05-05-2009 10:47 AM
search& count for the occurence of a word skoppana UNIX for Dummies Questions & Answers 1 11-09-2007 05:07 PM
Word count problem ssmith001 UNIX for Dummies Questions & Answers 1 01-02-2007 01:21 PM
count word ariuscy UNIX for Dummies Questions & Answers 1 10-13-2005 12:36 AM
How do I count # of char. in a word? xadamz23 Shell Programming and Scripting 9 11-12-2003 12:19 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-26-2008
bhaviknp bhaviknp is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 6
Post specified word count

hi iam trying to do a specified word count on file
called text
i have a few ideas but don't get the result i want
do any one have a idea
please help

i have this at the moment

cat text
echo "Please enter the word you are looking for:"
read string
echo "the word < $string > occurs in the following lines of file < $1 >:"
grep $string $1
echo
echo "press ENTER to continue"
read key
  #2 (permalink)  
Old 02-26-2008
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
if you are okay with Perl, check my post in this thread
  #3 (permalink)  
Old 02-27-2008
bhaviknp bhaviknp is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 6
iam to not use perl
  #4 (permalink)  
Old 02-27-2008
vino's Avatar
vino vino is online now Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Quote:
Originally Posted by bhaviknp View Post
iam to not use perl
Why ? Dont you have perl installed ?
  #5 (permalink)  
Old 02-27-2008
Dhruva's Avatar
Dhruva Dhruva is offline
Registered User
  
 

Join Date: Mar 2006
Location: India
Posts: 255
Try this.

Save this script in some file.give first argument as filename and second argument as word you want to search.

Code:
cnt=`grep "$2" $1 |awk -v CH="$2" '
          BEGIN {cnt =0}
          { while (y=index($0,CH))
            { cnt++
              y++
              $0=substr($0,y);
            }
          }
          END {print cnt }'`
echo "$2" "occurs $cnt times"
  #6 (permalink)  
Old 02-27-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
Quote:
Originally Posted by bhaviknp View Post
iam to not use perl
No homework questions on the Forums....

Code:
#!/bin/ksh
# $1 = input file $2=word to search for
tr -s ' ' '\n' < $1 | grep -c "$2"
Closed Thread

Bookmarks

Tags
awk, perl

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 12:55 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