Print one by one character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print one by one character
# 1  
Old 07-22-2011
Print one by one character

Hi all,

Can any one tell the script to print a line with one bye one character or word..

Ex: Hi every one. How are you all ....

I want to print like...

First it has to print H then sleep some time and i and again sleep and e ... etc

OR

Hi wait some time and every ..... etc


Thanks in advance...
# 2  
Old 07-22-2011
Code:
s='Hi every one. How are you all ....'

# char by char
while [ ${#s} -gt 0 ]; do
  printf '%s\n' "${s%${s#?}}"
  s=${s#?}
  sleep 2
done  

s='Hi every one. How are you all ....'

# word by word
# the follwoing command will
# reset any positional parameters
# and it does not work if shell word
# splitting is disabled (zsh disables it
# by default)

set -- $s

for w; do
  printf '%s\n' "$w"
  sleep 2
done

This User Gave Thanks to radoulov For This Post:
# 3  
Old 07-22-2011
Tools Word By Word

Hi,

Code:
nawk -F' ' '{for(i=1;i<=NF;i++){print $i;system("sleep 1");}}' File_name

Cheers,
RangaSmilie

Last edited by radoulov; 07-22-2011 at 07:07 AM.. Reason: Code tags.
This User Gave Thanks to rangarasan For This Post:
# 4  
Old 07-22-2011
Another one char-by-char, bash specific:

Code:
s='Hi every one. How are you all ....'
while read -rn1;do printf '%s\n' "$REPLY"; sleep 2; done<<<"$s"

# 5  
Old 07-22-2011
Quote:
Originally Posted by radoulov
Code:
s='Hi every one. How are you all ....'

# char by char
while [ ${#s} -gt 0 ]; do
  printf '%s\n' "${s%${s#?}}"
  s=${s#?}
  sleep 2
done  

s='Hi every one. How are you all ....'

# word by word
# the follwoing command will
# reset any positional parameters
# and it does not work if shell word
# splitting is disabled (zsh disables it
# by default)

set -- $s

for w; do
  printf '%s\n' "$w"
  sleep 2
done

Hello, radoulov:

For the sake of novice learners, I am pointing out two spots in your suggestions which make them unsuitable for handling miscellaneous text.

${s%${s#?}}: If there's an asterisk in the text, when the character preceding the asterisk should be output, the asterisk will be printed as well. For example, if the line is '2*3=6', when trying to output 2 by removing '*3=6' using the % shortest suffix removal operator, the asterisk will be forced to match nothing. Output in this instance will be '2*' instead of just '2'.

set -- $s: In the second solution you cannot quote $s since it depends on field splitting. Unfortunately, that also leaves the expansion open to file globbing, leaving the approach vulnerable a few metacharacters. I suppose this could be worked around by toggling a noglob option.

You are probably aware of these things, but, as I said, I mention them mostly for novices unaware of the corner cases.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 6  
Old 07-22-2011
Very good points! Thank you Alister!

If I'm not missing something, this should work around the first issue:

Code:
s='2*3=6'
while [ ${#s} -gt 0 ]; do   
  printf '%.1s\n' "$s"   
  s=${s#?}  
done

As you pointed out, set -f should be added for the second one (and special care should be taken for the IFS characters anyway, but I hope words with embedded tabs (HT/011) are not supposed to be present Smilie and the current IFS contains its default values).

Last edited by radoulov; 07-22-2011 at 11:23 AM..
# 7  
Old 07-22-2011
Quote:
Originally Posted by radoulov
If I'm not missing something, this should work around the first issue:

Code:
s='2*3=6'
while [ ${#s} -gt 0 ]; do   
  printf '%.1s\n' "$s"   
  s=${s#?}  
done

I agree.

A marginally simpler way of achieving the same result:
Code:
s='2*3=6'
while [ -n "$s" ]; do   
  printf '%c\n' "$s"   
  s=${s#?}  
done

Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print a particular character n number of times in a line??

hi, Is it possible to print a particular character n number of times in a line? for example. i am print the following line using echo command.. echo "files successfully moved" i want to count the number of characters that are been displayed. i am doin it using echo "files... (8 Replies)
Discussion started by: Little
8 Replies

2. Shell Programming and Scripting

Print character after pattern found

Hi Gurus, i need your help to create a script the will print a characters after the pattern was found. Sample lines are below: My birthday:"1977-16-07", My birthday:"1975-16-07" My birthday:"1970-16-07". My patter should be "birthday:", then i want to print the following characters which... (18 Replies)
Discussion started by: scripter123
18 Replies

3. Programming

Print line by character in C

Hello, I am reading the K & R C book, and trying to understand more about putchar()/getchar() by practice. Similar to the exercise 1-12, I want try to parse each line by character. Although there are many site about the exercise, but I could find similar case of mine, which is similar to fold... (11 Replies)
Discussion started by: yifangt
11 Replies

4. Programming

Python, How to print a character \

In python, if I use '\', error, use "\", error again, use '\\' or "\\", the print result will be \\ If I want to print a single \, what can I do? Thanks in advance Sorry... I have solved this problem... Thanks anyway (2 Replies)
Discussion started by: Henryyy
2 Replies

5. Shell Programming and Scripting

Print the next ASCII character

Hi, In my file, for few field I have to print the next ASCII character for every character. In the below file, I have to do for the 2,3 and 5th fields. Input File ======== 1|abc|def|5|ghi 2|jkl|mno|6|pqr Expected Ouput file ======= 1|bcd|efg|5|hij 2|klm|nop|6|qrs (2 Replies)
Discussion started by: machomaddy
2 Replies

6. Shell Programming and Scripting

Grep syntax print after certain character

My current code is: user@ubuntu:~/Desktop$ grep -e "\(packaged by\)\|\(employee\)\|\(file name\)\|\(Total Data (MB) Read\)\|\(Begin Time\)" log.txt packaged by = Ron Mexico employee = Michael Vick file name = Mike_Vick_2011.bat Total Data (MB) Read: 11.82 Begin Time: 6/13/2011... (8 Replies)
Discussion started by: chipperuga
8 Replies

7. Shell Programming and Scripting

shell script to print words having first and last character same.

Hi I want to write a shell script to print only those words from a file whose beginning and last character are same. Please help. Thanks, vini (5 Replies)
Discussion started by: vini kumar
5 Replies

8. Shell Programming and Scripting

Make pwd print escape character

I decided I wanted to have the cd command print my full working directory after each cd command, so I put this cw command in .bashrc as a function. cw () { cd "${1}" pwd }While this works I would like pwd to print escapes when a space in a directory name exists. This would... (7 Replies)
Discussion started by: jelloir
7 Replies

9. Shell Programming and Scripting

read the text file and print the content character by character..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... without using... (1 Reply)
Discussion started by: samupnl
1 Replies

10. Shell Programming and Scripting

AWK print a character many times

How can I print a '-' on the same line within awk, say 50 times, without actually typing '-' 50 times? Cheers (3 Replies)
Discussion started by: dbrundrett
3 Replies
Login or Register to Ask a Question