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 > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Very urgent :- How to compare string using if statement jisha Shell Programming and Scripting 1 01-14-2008 03:41 AM
while read loop w/ a nested if statement - doesn't treat each entry individually littlefrog Shell Programming and Scripting 7 12-11-2007 09:49 PM
For loop statement - catch error lumdev Shell Programming and Scripting 4 09-20-2007 08:50 AM
if statement in a while loop bobo UNIX for Dummies Questions & Answers 2 11-07-2006 12:38 PM
Help with if loop (string comparison) psynaps3 Shell Programming and Scripting 4 07-07-2006 03:36 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 03-02-2006
Sniper Pixie Sniper Pixie is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 25
if statement in for loop of a string

I am attempting to pass a string into awk and loop through it, and then for every occurrance of a certain character perform an action. In this case, for example, echo 1 for each time character r is found in the string. Except I can't get it to work. Could someone please tell me why?


echo $string | awk '{
for (i=1;i<=length($0);i++)
do
if (substr($0,i,1) == "r")
echo "1"
fi
done
}'
  #2 (permalink)  
Old 03-02-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Code:
if [[ "$string" == *r* ]] ; then
  echo "Found r"
fi ;
  #3 (permalink)  
Old 03-02-2006
Sniper Pixie Sniper Pixie is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 25
Thanks for your reply, but I want it to loop through the string and then perform an action for each occurrance of r, not just once. eg. "rover" would return 11 in this example because there're two r's in it.

I've tried adapting your code so it looks something like this:

echo $string | awk '{
for (i=1;i<=length($0);i++)
do
if [[ (substr($0,i,1) == *r*) ]] ; then
echo "1"
fi ;
done
}'


but get parse errors all over the place:

awk: cmd. line:4: if [[ (substr($0,i,1) == *r*) ]] ; then
awk: cmd. line:4: ^ parse error
awk: cmd. line:4: if [[ (substr($0,i,1) == *r*) ]] ; then
awk: cmd. line:4: ^ parse error
awk: cmd. line:4: if [[ (substr($0,i,1) == *r*) ]] ; then
awk: cmd. line:4: ^ parse error

Any more ideas?
  #4 (permalink)  
Old 03-02-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
What do you want to do ?

Find the number of occurences of a certain character in a string, or, are you looking for the all the positions where the character appears ?

Last edited by vino; 03-02-2006 at 07:25 AM..
  #5 (permalink)  
Old 03-02-2006
gauravgoel gauravgoel is offline
Registered User
  
 

Join Date: Dec 2005
Location: India
Posts: 218
Quote:
Originally Posted by vino
What do you want to do ?

Find the number of occurences of a certain character in a string, or, are you looking for the all the positions where the character appears ?

For number of occurences you can do

Code:
echo "$string" | grep -c "r"
Hi Vino,

With due regards, I would like to say that the above may not solve the problem, OP is having. He is looking for no. of occurence of a character in a string. And the above command will give always give output as 1 if there is one or more than one occurence of "r"
Kindly correct me if I am wrong

Gaurav
  #6 (permalink)  
Old 03-02-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Quote:
Originally Posted by gauravgoel
Hi Vino,

With due regards, I would like to say that the above may not solve the problem, OP is having. He is looking for no. of occurence of a character in a string. And the above command will give always give output as 1 if there is one or more than one occurence of "r"
Kindly correct me if I am wrong

Gaurav
You are right. It gives 1. -c gives a count of the number of lines where the character occurs.

Good catch.
  #7 (permalink)  
Old 03-02-2006
Sniper Pixie Sniper Pixie is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 25
Thanks gauravgoel, that was just what I was looking for. Thanks for your effort as well, vino.
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 10:31 PM.


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