if statement in for loop of a string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers if statement in for loop of a string
# 1  
Old 03-02-2006
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  
Old 03-02-2006
Code:
if [[ "$string" == *r* ]] ; then
  echo "Found r"
fi ;

# 3  
Old 03-02-2006
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  
Old 03-02-2006
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  
Old 03-02-2006
Hi ,

You are looking for this

Quote:
echo $string | awk '{
for (i=1;i<=length($0);i++)
if(substr($0,i,1) == "r")
print "1"
}'
Post back if still some problem

Gaurav
# 6  
Old 03-02-2006
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
# 7  
Old 03-02-2006
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

Loop statement within a batch script

I have to create a couple of files within a directory structure. Filename stays the same, but at the end of each file, the number appears. Example: File needs to be created within directory c:\temp File Name will stay the same (testfile), but extension will increase, starting at 01 - example... (1 Reply)
Discussion started by: HenkTrumpie
1 Replies

2. Shell Programming and Scripting

Bash - Nesting if statement in for loop

I have the basic command written in bash for element in 1 2 do if ]; then set el = "t" else set el = "p" fi done but i get the following error syntax error near unexpected token `for' ` for element in 1 2' What should i do differently? (3 Replies)
Discussion started by: ncwxpanther
3 Replies

3. Shell Programming and Scripting

While loop within if statement

Hi, I'm a rookie who is trying to learn this stuff. What I need help with is putting together a non complicated "while" loop within the below "if" statement. I also need the while loop to keep looping until the user types a key to end the loop. Please reveal the proper insertion points. Thank... (4 Replies)
Discussion started by: jefferj54
4 Replies

4. Shell Programming and Scripting

awk if statement in a for loop?

I need to match multiple values in a single column in a file: example: Source file: abd,123,one def,232,two ghi,987,six Target file: 12345,abcde,123 09876,zxvyr,566 56789,lmnop,232 Variable: var1=`grep 2 sourcefile | awk '{print$1}' essentially, echo "$var1" would read:... (2 Replies)
Discussion started by: kopfgeldjagar
2 Replies

5. Shell Programming and Scripting

If condition and for loop within sed statement

Hi, I tried to go through a lot of online material but could not find concrete solution. My issues is like this : I've got a input file like this : <a> <startDate>19700101000000</startDate> <endDate>20300101000000</endDate> </a> ... (12 Replies)
Discussion started by: Shaishav Shah
12 Replies

6. Shell Programming and Scripting

how to create a loop in an if statement

Hey guys, a=`cat abc | wc -l` b=`cat def | wc -l` if $a== $b then echo "a" else echo "b" fi I want the if condition to retry itself , untill a==b. I can't use goto statemt. Please help. Thanx in advance. Please use next time code tags for your code and data (5 Replies)
Discussion started by: jaituteja
5 Replies

7. Shell Programming and Scripting

Help With Loop in Case Statement script

I am writing a bash script that asks the user for input and I need it to repeat until the user selects quit.. I dont know how to write the loop for it I searched all over but i still do not get it.. if anyone could help with this it would be greatly apprciated here is my script so far: #!... (2 Replies)
Discussion started by: Emin_Em
2 Replies

8. Shell Programming and Scripting

Variable problem in for loop with if statement

Hi, Again a little problem. Do not understand good why an empty string is not detected. Here is the program: #!/bin/ksh APR=`date | grep Apr | awk '{print $2$3}'` MAY=`date | grep May | awk '{print $2$3}'` JUN=`date | grep Jun | awk '{print $2$3}'` echo "Variable Apr has value:... (6 Replies)
Discussion started by: ejdv
6 Replies

9. Shell Programming and Scripting

For loop statement - catch error

I'm having a question about for loops. (bash) I have the following for example: for file in `ls *.txt` do read file ... done Now when there is a file present there is no problem, now when there is no file present I get the following output in my standard mail box : "No such... (4 Replies)
Discussion started by: lumdev
4 Replies

10. UNIX for Dummies Questions & Answers

if statement in a while loop

#!/usr/bin/ksh echo Please enter while read n do echo $n >> datafile done question: How can I enject an if statement that if the users enter 0 (zero) the program will exit? this is what I have but not working #!/usr/bin/ksh echo Please enter number while read n do if $n=0 then... (2 Replies)
Discussion started by: bobo
2 Replies
Login or Register to Ask a Question