Reading control characters into variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading control characters into variables
# 1  
Old 04-23-2014
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.

Code:
abc^M
def^M
ghi^M

becomes

Code:
abc^Mdef^Mghi^M

I have a file called afile.txt

with contents

Code:
abc^M
def^M
ghi^M

And a script that looks like

Code:
for line in `cat afile.txt`
do
line2=${line2}${line}
done
    echo $line2

This returns:
Code:
ghi

So where am I going wrong? Any help gratefully appreciated.

Thanks.

P.S.

If I remove the ^M from each line it works, but I need to keep ^M in the string.
# 2  
Old 04-23-2014
Hi,

You can use the following line of code;

Code:
cat -v -t afile.txt | paste -s | sed 's/  //g'

Note to get the character that looks like a space between the two slashes, you need to use "ctrl+v ctrl+i"

Regards

Gull04
# 3  
Old 04-23-2014
I maybe missing something, but it returns nothing.
# 4  
Old 04-23-2014
Hi,

What is your OS?

Check the following;

Can you see the original file if you just run the,

Code:
cat -v -t afile.txt

Let me know the above.

Regards

Gull04
# 5  
Old 04-23-2014
You do not go wrong anywhere, but if $line2 contains abc^Mdef^Mghi^M then if you print the variable on your terminal the only thing visible is ghi, because of the CR characters, which move the cursor to the beginning of the line..

Try using set -x or redirecting the variable to a file and you'll see they are there...




--
It is better not to use for line in `cat afile.txt`. Consider using:
Code:
while read line
do
...
done < afile.txt

# 6  
Old 04-23-2014
Code:
cat text.txt | awk '{ORS=""}1'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Control Characters

Hallo Team, I am trying to get rid of the dollar sign. I managed to remove all the other special characters but i am struggling with this one. -bash-3.2$ cat -e missing_revenue_20141112.csv|less|head BW0522168531211141180935668@196.23.110.141$ BW092218784121114-370120610@196.23.110.141$... (4 Replies)
Discussion started by: kekanap
4 Replies

2. UNIX for Dummies Questions & Answers

Control characters in UNIX

Hi, My files are showing some control characters in vi editor ^M ^@ and somtimes ^H I removed ^M with %s/^M//g command but how to represent ^@ and ^H e.g. for ^M it is hold ctrl then v and m.. Please help.. I am very new to unix.. (7 Replies)
Discussion started by: prabhat.diwaker
7 Replies

3. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

4. Shell Programming and Scripting

Control Characters

Can somebody please help me with the query. ? I want a part of program of which should look for control characters in the flat file , when it finds it, displaying message that Control Characters found..! Please help me (13 Replies)
Discussion started by: iamnoone
13 Replies

5. Shell Programming and Scripting

Urgent : Control Characters

Can somebody please help me with the query. ? I want a part of program of which should look for control characters in the flat file , when it finds it, displaying message that Control Characters found..! Please help me (1 Reply)
Discussion started by: iamnoone
1 Replies

6. Shell Programming and Scripting

control variables...

Hi. I´ve a question to a running script: i=0 #fill an Array with all files in a folder ending with .sys for Par in *.sys ; do Par2="$Par" ; i=$((i... (1 Reply)
Discussion started by: Lock3
1 Replies

7. Shell Programming and Scripting

screen control characters

Hi, Can anyone help me with controlling the cursor position from a shell script. Things like moving left,right,up,down etc Anyone have any ideas? (2 Replies)
Discussion started by: ajcannon
2 Replies

8. UNIX for Dummies Questions & Answers

Remove control characters

Hi, When I do a man and save it into a file, I end up getting a lot of control characters. How can I remove them?? I tried this: /1,$ s/^H//g But I get an error saying "no previous regular expression". Can someone help me with this. Thanks, Aravind (5 Replies)
Discussion started by: aravind_mg
5 Replies

9. Programming

Reading a process control block

Hello, I want to know what call to use to read the details of a process control block in solaris ?:) (2 Replies)
Discussion started by: hmurali
2 Replies

10. UNIX for Dummies Questions & Answers

printing control characters

using c-shell, does anyone know how to send control characters to the printer before the job? I need to set a printer to print in condensed mode HELP (1 Reply)
Discussion started by: mglinsk
1 Replies
Login or Register to Ask a Question