LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Handling Control Characters in Linux. (https://www.linuxquestions.org/questions/linux-general-1/handling-control-characters-in-linux-630958/)

bala.linux 03-27-2008 04:05 AM

Handling Control Characters in Linux.
 
Hello All,

I have a doubt in processing the control characters in unix. Assume that I have logged all the user's key press with output in a text file which has terminal control characters. For example,

^[]0;root@2FWD49:~^G[root@2FWD49 ~]# l^H^[[Kdate^M
Thu Mar 27 14:40:54 IST 2008^M
^[]0;root@2FWD49:~^G[root@2FWD49 ~]# clear^M
^[[H^[[2J^[]0;root@2FWD49:~^G[root@2FWD49 ~]# logout^M
^[[H^[[2J


I want to process all the control characters and get the content like below,

[root@2FWD49 ~]# date
Thu Mar 27 14:40:54 IST 2008
[root@2FWD49 ~]# clear

When I cat that file, I get the above output (terminal process the characters and shows). I am looking a unix utility to perform. Please let me know in case any command in unix does this job.

Thanks and Regards,
Bala

pixellany 03-27-2008 06:49 AM

There are lots of tools to do something like this. I would start with SED.

The trick is to define the generic pattern you want to remove. I'm having trouble defining the pattern in any simple way.

Here is one example to get started:
sed 's/\^.*;//g' filename
This deletes all strings beginning with "^" and ending with ";"

Go here for a really good tutorial on SED: http://www.grymoire.com/Unix/Sed.html

bala.linux 03-27-2008 11:51 AM

Quote:

Originally Posted by pixellany (Post 3101864)
There are lots of tools to do something like this. I would start with SED.

The trick is to define the generic pattern you want to remove. I'm having trouble defining the pattern in any simple way.

Here is one example to get started:
sed 's/\^.*;//g' filename
This deletes all strings beginning with "^" and ending with ";"

Go here for a really good tutorial on SED: http://www.grymoire.com/Unix/Sed.html

Thanks for your reply.
I just donot want to replace the control character. If I want, then I can just use 'sed'/'tr'. But I need to process them. Like,
clead(backspace)r

I want the output to be clear and not cleadr.
Is this possible?

pixellany 03-27-2008 01:28 PM

Using this example--but the way you stated it in the original post.

Before:
clear^M

After:
clear

sed 's /\^.//g' filename > newfilename

This deletes all occurrences of "^" followed by any single character. The problem is that the constructs you want to delete are more complicated.


All times are GMT -5. The time now is 10:23 AM.