Read character by character in line in which space is also included


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read character by character in line in which space is also included
# 1  
Old 10-28-2014
Read character by character in line in which space is also included

Hi friend,

I have one file , and i want to read that file character by character.

I need this script in ksh.

while using read option with -n1 am getting error.

Code:
while read -n1 c

Code:
 read has bad option

And if i am using below script, then if in a line has space like this ( Pallvi mahajan) Then it stuck after m and loop become iterative after m
Code:
typeset -L1 single 
while read line 
do    
 while (( ${#line} > 0 ))    
 do   
      single="$line"        
 # do what you like with $single     
    echo $single        
 line=${line#$single}    
 done 
done < input.txt

Please help me how i can read character by character in line if there is space and any another character are there.

Last edited by pallvi_mahajan; 10-28-2014 at 08:14 AM.. Reason: code need to be coreect
# 2  
Old 10-28-2014
CygWin bash terminal. KSH should wirk...
Code:
AMIGA:~> line="This is a string with spaces."
AMIGA:~> n=0
AMIGA:~> while (( $n <= ${#line} )); do echo "${line:$n:1}"; n=$((n+1)); done
T
h
i
s

i
s

a

s
t
r
i
n
g

w
i
t
h

s
p
a
c
e
s
.

AMIGA:~> _

# 3  
Old 10-28-2014
Hi ,

thanks , i have tried but getting error in below statemnet
Code:
echo "${line:$n:1}";

Code:
bad substitution

# 4  
Old 10-28-2014
if it's ok to use perl

Code:
$ printf "abc def    ghi" | perl -ne ' @x = split("",$_); print "$_\n" foreach(@x); '
a
b
c

d
e
f




g
h
i

# 5  
Old 10-28-2014
HI Thanks,

But i have no perl editor
# 6  
Old 10-28-2014
I don't think read -n1 should be a problem in ksh unless you have an old version.
# 7  
Old 10-28-2014
just type
Code:
perl -v

on your command line
if it gives output like this, you are good to use the command line provided by me in the other comment

Code:
perl -v

This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi

Copyright 1987-2009, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

How to read the nth character from the line.?

I have Index Line and I tried to get the 9th character from the file and to check the character is "|" or not. Shell Scripting. Sample Index file. "91799489|K8E|188.004.A.917994892.1099R.c.01.pdf|2013|10/15/2014|002|B|C|C"... (3 Replies)
Discussion started by: pavand
3 Replies

2. Shell Programming and Scripting

How to add a character after the first word/space on each line?

Hi:) I have a large file ( couple thousand lines ) and I'm trying to add a character after the first word/space on each line. eg: First line of text Second line of text Third line of text Fourth line of text Fifth line of text I'd like to accomplish: First - line of text Second... (8 Replies)
Discussion started by: martinsmith
8 Replies

3. Shell Programming and Scripting

How to read the filenames with space character in it

Hi, My Requirement is to read the filenames which possess space charatcer in it and pass the same file name to the other shell script as input parameter. Please provide your valuable suggestion. (5 Replies)
Discussion started by: cnraja
5 Replies

4. Shell Programming and Scripting

How to read one character form each line of the file?

Hi, Maybe this iscorrect forum for my question... I should read one character at a fixed position from each line of the file. So how ??? should be substituted in the code below: while read line ; do single_char=`???` echo "$single_char" done < $input_file OK...I did get an... (0 Replies)
Discussion started by: arsii
0 Replies

5. Shell Programming and Scripting

Read line based on character,

Hi Experts, I have called file1.txt contains below CREATE TABLE "IHUBDEV2"."TLM_BREAK_RULES" ( "OID" VARCHAR2(32) NOT NULL ENABLE, "TLM_PAY_CLASS_OID" VARCHAR2(32) NOT NULL ENABLE, "PUNCHED_BREAKS" NUMBER(1,0) DEFAULT 0 NOT NULL ENABLE, "NORMAL_BREAKS"... (3 Replies)
Discussion started by: naree
3 Replies

6. 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

7. Shell Programming and Scripting

Get rid of the 7th character of each line if this is a space

I have a text file like this ... B 16 1.340E+05 A 18 3.083E+02 Wu123 1.365E+02 ... I would like to get rid of the 7th character of each line if this is a space character. Thank you, Sarah (5 Replies)
Discussion started by: f_o_555
5 Replies

8. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

9. UNIX for Dummies Questions & Answers

read a variable character by character, substitute characters with something else

im having trouble doing this: i have a variable with 2 characters repeating e.g. aababbbaaaababaabbaabbba is there a way i can search the variable for a's and b's and then change a's to b's and b's to a's? im guessing its like getting the 1's compliment of the string im doing this in... (2 Replies)
Discussion started by: vipervenom25
2 Replies

10. Shell Programming and Scripting

Read First Character of Each Line in File

I need a better way to read the first character of each line in a file and check if it equals the special character ¤. This character tells me where there is a break in the reports. The file has over 500,000 lines. Currently, this is my code - if ] I am using Korn Shell as a scripting... (7 Replies)
Discussion started by: azelinsk
7 Replies
Login or Register to Ask a Question