access each character in a string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers access each character in a string
# 1  
Old 09-08-2008
access each character in a string

Hi All,
I'm writing a script where one of the parameter is a string like:
0011001100

Then I want to do a while loop, according to the value of each character.

Could tell how I can access the value of each character in the string?

Thank you,
Gino
# 2  
Old 09-08-2008
bash has special builtins for this, but on any unix box:
Code:
count=1
string="00100011"
while [[ $count -le ${#string}
do
     tmp=$(expr substr $string $count 1)
     echo $tmp
done

# 3  
Old 09-08-2008
Thank you for the reply.

I get a sytax error like this:
although i' m using bash it seems that substr

expr: syntax error
# 4  
Old 09-08-2008
${string:7:1}

Returns at the seventh character position, one character -- in bash.
# 5  
Old 09-08-2008
thank you again.

I had to do it like this to make it work.
The is string is in $3

blah=$3
flag=${blah:ID:1}

flag=${$3:ID:1}
is not accepted.
# 6  
Old 09-08-2008
Code:
#!/usr/local/bin/bash

str="00100011"
cnt=${#str}

for ((i=0; i < cnt; i++))
do
     echo ${str:$i:1}
done

# 7  
Old 09-08-2008
Code:
#!/bin/sh
str=$1
while test "$str"; do
  tail=${str#?}
  echo ${str%$tail}
  str=$tail
done

For fixing the syntax error you had before, you want flag=${3:ID:1}
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

2. Shell Programming and Scripting

How can I match the particular character in the string?

Hi, I want to check out a word in the text file and generate a clear report for me to see... The text file content: Content: ............ 20120608: 20120608: ............ 20120608: .......... 2012031201: , hime] End of the file My expected output is: Full TXT: manatsu TXT:... (3 Replies)
Discussion started by: meroko
3 Replies

3. UNIX for Advanced & Expert Users

How the user process can access the character device loaded by my module

I am trying to load into the kernel a system-call dynamically (without restarting the kernel and compailing it) in an attempt to (once in kernel mode) write to user process's memory. (I know there is a way to do this with the ptrace interface but it is not an option.) I know the only way to... (1 Reply)
Discussion started by: hopelessProgram
1 Replies

4. Shell Programming and Scripting

Perl to access character from string

This it to pring 'i' character. #!/usr/bin/perl $str="This is perl"; print $str; Must i split the string to access a character from string? How can i print characters individually using a for loop? (2 Replies)
Discussion started by: cola
2 Replies

5. Shell Programming and Scripting

Extracting the last character of a string

How to extract the last character of a string in bash? ---------- Post updated at 03:56 PM ---------- Previous update was at 03:55 PM ---------- Suppose "abcde" is a string. i want to extract the last character i.e. "e". (1 Reply)
Discussion started by: proactiveaditya
1 Replies

6. Shell Programming and Scripting

Korn: How to loop through a string character by character

If I have a string defined as: MyString=abcde echo $MyString How can I loop through it character by character? I haven't been able to find a way to index the string so that I loop through it. shew01 (10 Replies)
Discussion started by: shew01
10 Replies

7. Shell Programming and Scripting

how to display only last character of a string?

hi all, if i do an: "echo $string |" what should be after the pipe to display ONLY the last char of the output? tia, DN2 (9 Replies)
Discussion started by: DukeNuke2
9 Replies

8. Programming

converting character string to hex string

HI Hi I have a character string which contains some special characters and I need it to display as a hex string. For example, the sample i/p string: ×¥ïA Å gïÛý and the o/p should be : D7A5EF4100C5010067EFDBFD Any pointers or sample code pls. (5 Replies)
Discussion started by: axes
5 Replies

9. Shell Programming and Scripting

Testing the last character in a string

Hi In the shell scripted I'm trying to write! I would like to test the last character in a string. The string is a path/directory and I want to see if the last character is a '/'. The string (path/directory) is inputted by a user. If the '/' character isn't present then I want to be able to... (11 Replies)
Discussion started by: dbrundrett
11 Replies

10. UNIX for Dummies Questions & Answers

Delete first 2 character from string

Hi ! All, I have to delete first 2 characters from string. How its possible? Like string value is "2001". I required output as "01" Plaese help me. Nitin (4 Replies)
Discussion started by: nitinshinde
4 Replies
Login or Register to Ask a Question