LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-10-2008, 01:56 AM   #1
stepic
LQ Newbie
 
Registered: Aug 2006
Posts: 18

Rep: Reputation: 0
Convert octal to character


I would like to convert octal numbers to characters.

Here it is a word 'order':
\157\162\144\145\162

When I run in command line:
echo -e "\157\162\144\145\162"
will print order.

I tried with this command:
echo "\157\162\144\145\162" | sed "s/\([\\][0-9][0-9][0-9]\)/`echo -e \\\1`/g"
This print \157\162\144\145\162.


Then I tried to convert the string with echo -e but it doesn't work:
echo "\157\162\144\145\162" | sed "s/\([\\][0-9][0-9][0-9]\)/`echo -e \\\1`/g"
This will print 11111.

What I am doing wrong?
Thank you.
 
Old 04-10-2008, 03:41 AM   #2
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
$ printf "\157\162\144\145\162"
order
 
Old 04-10-2008, 04:00 AM   #3
stepic
LQ Newbie
 
Registered: Aug 2006
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Agrouf View Post
$ printf "\157\162\144\145\162"
order
The octal chars are in a file eg test_file.py
I need to convert all octals them using a command like:
sed "s/\([\\][0-9][0-9][0-9]\)/`echo -e \\\1`/g" test_file.py

But the echo after sed it doesn't work. Neither printf.

Thank you.
 
Old 04-10-2008, 06:53 AM   #4
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
What about:
Code:
while read line
do
   eval "printf \"$line\""
done < test_file.py
Anyway, the `echo -e \\\1` will not work with because it is replaced by the shell before the sed command is executed.
In other words, `echo -e \\\1' is sctricly equivalent to \1 and so in your sed command you replace something with exactly the same thing.

Last edited by Agrouf; 04-10-2008 at 06:55 AM.
 
Old 04-10-2008, 11:53 PM   #5
stepic
LQ Newbie
 
Registered: Aug 2006
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Agrouf View Post
What about:
Code:
while read line
do
   eval "printf \"$line\""
done < test_file.py
Anyway, the `echo -e \\\1` will not work with because it is replaced by the shell before the sed command is executed.
In other words, `echo -e \\\1' is sctricly equivalent to \1 and so in your sed command you replace something with exactly the same thing.
You're right and thanks for the idea, I'll try it.
 
Old 04-11-2008, 04:49 AM   #6
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
Hello stepic
The Agrouf solution doesn't seem to work, so I propose another one which works on my machine :
Code:
printf "$(cat test_file.py)\n"
This works for octal coded characters of the form \NNN, not \0NNN.
To use that other form, replace 'printf' by 'echo -e' (without the quotes).
Hope that solve your problem.
Bye.

Last edited by berbae; 04-11-2008 at 05:01 AM.
 
Old 04-12-2008, 01:14 AM   #7
stepic
LQ Newbie
 
Registered: Aug 2006
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by berbae View Post
Code:
printf "$(cat test_file.py)\n"
Hi berbae,

I've tried the above solution and that's perfect.

Thank you very much!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python: converting a 3 character octal string into and ASCII char llama_meme Programming 1 07-06-2010 02:00 PM
convert decimal to octal using the let command Suinatsa Programming 2 10-24-2006 08:42 AM
Printing the octal representation of a character. smoothdogg00 Programming 1 03-02-2006 10:44 PM
C, read 8 bit octal numbers, convert to 24 bit binary bamalabs Programming 3 01-20-2004 09:59 AM
Can 't convert from a character to octal value in memory Linh Programming 2 07-21-2003 03:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration