How to loop through every char in a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to loop through every char in a string
# 1  
Old 07-23-2009
How to loop through every char in a string

for example this string: gLZMQp8i

Loop become easy if we add space between each char, How to do it?
or other solutions are welcome.
# 2  
Old 07-23-2009
What you want to do in the loop?
# 3  
Old 07-23-2009
try..
Code:
echo 'gLZMQp8i' | sed 's|.|&\n|g'

# 4  
Old 07-23-2009
I have found this code....

Code:
var="example text";var1=$(echo $var | wc -c);for((i=$var1;i>=0;i--));do echo $var | cut -c $i;done

# 5  
Old 07-23-2009
Quote:
Originally Posted by ryandegreat25
try..
Code:
echo 'gLZMQp8i' | sed 's|.|&\n|g'

it worked great, Could you explain the usage of &
I found it
/regexp(3,n)/replacement/
Attempt to match regexp(3,n) against the pattern space. If success-
ful, replace that portion matched with replacement. The
replacement may contain the special character & to refer to that
portion of the pattern space which matched, and the special
escapes \1 through \9 to refer to the corresponding matching
sub-expressions in(1,8) the regexp(3,n).


---------- Post updated at 09:34 PM ---------- Previous update was at 09:28 PM ----------

Quote:
Originally Posted by radoulov
What you want to do in the loop?
I want to count the totoal number of each class a-z, A-Z and 0-9.

Is there better way than loop?

Last edited by honglus; 07-23-2009 at 09:09 AM..
# 6  
Old 07-23-2009
A possible solution :
Code:
$ cat count.sh
$ cat c.sh
eval $( echo "$*" | \
        awk  '
          { l += gsub(/[a-z]/, "");
            u += gsub(/[A-Z]/, "");
            d += gsub(/[0-9]/, ""); }
          END {
            printf("lower_cnt=%d;upper_cnt=%d;digit_cnt=%d\n",
                   l, u, d) } '
       )
echo lower=$lower_cnt
echo upper=$upper_cnt
echo digit=$digit_cnt
$ count.sh Happy New Year 2010
lower=9
upper=3
digit=4
$

Jean-Pierre.
# 7  
Old 07-23-2009
I have tried this so...

Code:
echo 'gLZMQp8i' | sed 's/\(.\)/\1 /g'

result:
g L Z M Q p 8 i

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C++ Using open on a string instead of char*

I am using ifstream to open a file using std::fstream::open void open ( const char * filename, ios_base::openmode mode = ios_base::in ); However I want to use a string instead of a char* as follows but having a problem on how to do this string val_ifmodl = “fred.modl” ifstream ifs_modl;... (2 Replies)
Discussion started by: kristinu
2 Replies

2. Programming

Cast from String to char

Hello, This is my code: i'd like to like to add getenv("MYLIB") in the first case of my buffer inside of '1' , should i do the cast ? and how please ? Thank you. (1 Reply)
Discussion started by: chercheur857
1 Replies

3. Programming

how to use hex escape char with string in C?

I want it to ouput "abcd", but it dosen't. 1 #include<stdio.h> 2 int main() 3 { 4 printf("a\x62cd"); 5 } 6 gcc alarm.c -o alarm alarm.c: In function 'main': alarm.c:4:9: warning: hex escape sequence out of range It seems that the complier joint "cd" as part of... (8 Replies)
Discussion started by: vistastar
8 Replies

4. Programming

PERL \c char in the string

Hi guys, I am stuck up in a situation. I have a SUN box with certain logs which I need to parse to draw a report using Perl. Now, when I load the text file using a perl degugger to see how the text looks like when the first line of the log file is read in a variable. below is the snapshot of... (2 Replies)
Discussion started by: Asteroid
2 Replies

5. Shell Programming and Scripting

Parsing char string

I am stumped! I need to parse an input parameter to a script that has the form '-Ort'. I basically need 'O', 'r' and 't', i.e. the individual characters in the string parsed. Since there are no delimiters, I don't know how awk could do this. Can someone tell how to do this, this should be a... (5 Replies)
Discussion started by: ALTRUNVRSOFLN
5 Replies

6. Shell Programming and Scripting

how to get number char from a string

for example: i hav a string like : /rmsprd/arch01/rmsprd/rmsprdarch72736.log how I can extract my_num=72736? I know I can echo "/rmsprd/arch01/rmsprd/rmsprdarch72736.log" | tr "/" " " | awk '{ print $4 }' to get rmsprdarch72736.log (4 Replies)
Discussion started by: netbanker
4 Replies

7. Shell Programming and Scripting

last char from a string

i have a script that reads a plain text file. (its a ksh, and i can use bash also) each line of the file is a fullpath of a file. that makes the list huge. i need to add a functionalitie to that script, i have to be able to add /usr/* or /usr/ and with that reference all the files and folders... (6 Replies)
Discussion started by: broli
6 Replies

8. Programming

replacing char with string

how we can replace char with a string example char *a="a.s" so finally what i ant to do raplace a with ant and s sree so in my array a i want to store the value as "ant.sree" thank u in advance (1 Reply)
Discussion started by: phani_sree
1 Replies

9. UNIX for Dummies Questions & Answers

string of 7 char length always...

Hi, I know, particular value in the variable should always be of lenth 7 , but the value that is present in thevariable might be of any no.of characters less than or equal to 7... if the no.of characters in the variable is less than 7, I want to add, zeroes at the starting of the field.. How can... (3 Replies)
Discussion started by: thanuman
3 Replies

10. Programming

Compare Char to String

This is actually a c++ question... Basically I am creating a program that asks for five characters. I have a dictionary file containing tons of words no long than five letters long, on a seperate line. I want to be able to take the five inputted letters and compare them to the words in the file... (3 Replies)
Discussion started by: Phobos
3 Replies
Login or Register to Ask a Question