Problem with reading characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with reading characters
# 1  
Old 02-22-2010
Problem with reading characters

Hello,
if I try this (in bash):
Code:
#!/bin/bash
cat leercaracter.sh | while read linea
do
   # read character by character
   echo $linea | while read -n 1 caracter
   do
      echo $caracter
   done
done

New lines, spaces, tabs aren't showed by echo.

How can I 'echo' those characters?

Thank you

Last edited by Franklin52; 02-22-2010 at 07:21 AM.. Reason: Please use code tags!
# 2  
Old 02-22-2010
Try to use quotes around the variable, like:
Code:
echo "$caracter"

# 3  
Old 02-22-2010
I tried that and doesn't work...
With strong quotes the effect is $caracter (literal).
# 4  
Old 02-22-2010
Try This



Code:
#!/bin/bash
while read line
do
#echo $line
echo "$line" | while read -n 1 caracter
do
      echo $caracter
   done
done < leercaracter.sh


Last edited by amitranjansahu; 02-22-2010 at 07:37 AM.. Reason: typo
# 5  
Old 02-22-2010
Try to quote the variable in the first echo too, that should preserve all blanks and tabs. The newline characters are lost in the outer loop, because you read the input file line by line and the terminating newline is not written to the variable by read.
# 6  
Old 02-22-2010
Thank you to all.

Later I will test the solutions. Now, (in my work) I have ksh but not (and is impossible to install) bash...
thank you again.

This is a great forum.

---------- Post updated at 05:56 PM ---------- Previous update was at 12:50 PM ----------

No, the problem continues....

If I code this:
Code:
#!/bin/bash

cat leerfichero.sh | while read linea
do
   echo ""
   echo "$linea" | while read -n 1 caracter
   do
      echo -n "$caracter"
   done
done

I get:
Code:
:
~$ bash leerfichero.sh 

#!/bin/bash

catleerfichero.sh|whilereadlinea
do
echo""
echo"$linea"|whileread-n1caracter
do
echo-n"$caracter"
done

The same result with the other opcion:

Code:
:~/shell/ejercicios$ bash leerfichero.sh 

#!/bin/bash

catleerfichero.sh|whilereadlinea
do
echo""
echo"$linea"|whileread-n1caracter
do
echo-n"$caracter"
done

I don't understand!
# 7  
Old 02-22-2010
You need to set IFS to "" to tell read to not break arguments on whitespace. This property is occasionally useful -- if you set IFS="," you can read in values delimited by commas instead of space-separated ones...

Note I don't set IFS globally anywhere here, since you usually expect it to break on whitespace.

So, to make your program work:
Code:
#!/bin/bash

cat leerfichero.sh | while IFS="" read linea
do
   echo ""
   echo "$linea" | while IFS="" read -n 1 caracter
   do
      echo -n "$caracter"
   done
done

...or better yet, use a redirection to avoid a Useless Use Of Cat:
Code:
#!/bin/bash

while IFS="" read linea
do
   echo ""
   echo "$linea" | while IFS="" read -n 1 caracter
   do
      echo -n "$caracter"
   done
done < leerfichero.sh

...or, even better, use string operations to get the nth character, avoiding pipes and external calls completely. This is much, much faster.

Code:
#!/bin/bash
while IFS="" read linea
do
        echo

        for((N=0; N<${#linea}; N++))
        do
                echo -n "${linea:$N:1}"
        done
done < leerfichero.sh


Last edited by Corona688; 02-22-2010 at 01:15 PM.. Reason: fix lots of typos
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading control characters into variables

Hi, I am trying to write a shell script to help with some digital signature work currently being undertaken where we have a file that contains a number of rows ending with ^M. What I need to do is concatenate this using shell scripting and retain the control character. E.G. abc^M... (5 Replies)
Discussion started by: chris01010
5 Replies

2. Shell Programming and Scripting

Reading expression problem

I want to read a file -line by line which consists of the following statements Chr18:4000-4010 Chr20:4020-4030 Chr15:8000-8050 Pls. give a expression to read the line like this from a file Chr18:4000-4010 the given two replies reading like that Chr18:-10 but it is important to... (2 Replies)
Discussion started by: hravisankar
2 Replies

3. UNIX and Linux Applications

data reading problem..

In LVM I have one volume grp and it has two logical volumes A and B and both are full,so I extended my volume grp to 1GB with another hard disk and add this space to A and put my resr data .Now problem is that I get data from first hard disk only but I m unable to find data which is in second hard... (0 Replies)
Discussion started by: Rahul chauhan
0 Replies

4. Shell Programming and Scripting

How to filter only comments while reading a file including line break characters.

How do I filter only comments and still keep Line breaks at the end of the line!? This is one of the common tasks we all do,, How can we do this in a right way..!? I try to ignore empty lines and commented lines using following approach. test.sh # \040 --> SPACE character octal... (17 Replies)
Discussion started by: kchinnam
17 Replies

5. Shell Programming and Scripting

Problem in reading a file

Hi Guys, I am having a file which does not have any name when i do a ls -l -rw-r--r-- 1 dctrdat1 dctrdata 35 Feb 09 08:04 -rw-r--r-- 1 dctrdat1 dctrdata 11961 Feb 08 06:40 DAI_data.txt Now i want to see what is inside that file. Can you please let me know how to read... (9 Replies)
Discussion started by: mac4rfree
9 Replies

6. Shell Programming and Scripting

Reading a file having junk characters in perl

Can anyone tell me how to read a file in perl having junk characters . I have only one junk character which is repeated many times in the file. While i'm reading and printing the file , it is displaying till the 1st occurence of that junk character and rest of the file is not being read. (1 Reply)
Discussion started by: k_surya
1 Replies

7. Shell Programming and Scripting

problem in reading a file

i need to read record by record i use script #!/bin/ksh for i in 'cat filename' do echo $1 done but i dont get expected result i just get filename echoed on screen (4 Replies)
Discussion started by: er_zeeshan05
4 Replies

8. UNIX for Advanced & Expert Users

problem with socket reading

I am not able to receive the message on socket in the current process when its waiting for its child to exit. code looks something like below //in one thread of the current process //thread 1 =============================================== int numBytes = read(sockid,buf,SIZE); //Now the... (2 Replies)
Discussion started by: swap007
2 Replies

9. Programming

Reading special characters while converting sequential file to line sequential

We have to convert a sequential file to a 80 char line sequential file (HP UX platform).The sequential file contains special characters. which after conversion of the file to line sequential are getting coverted into "new line" or "tab" and file is getting distorted. Is there any way to read these... (2 Replies)
Discussion started by: Rajeshsu
2 Replies

10. Shell Programming and Scripting

Reading Characters from txt file

Hello, I am new to shell scripting, and I am trying to create a script that reads an input like the following firstname:lastname:age firstname:lastname:age firstname:lastname:age in a text file. I have a 2 part question. First how do I open the file in a shell script. And then how can... (7 Replies)
Discussion started by: TexasGuy
7 Replies
Login or Register to Ask a Question