LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2012, 08:05 PM   #1
AmirJamez
Member
 
Registered: Apr 2012
Posts: 42

Rep: Reputation: Disabled
Find and replace floating HEX points with its normal equivalent value in bash


Dear Everyone,

I was wondering if we can find and replace some floating points HEX numbers with these ---> "0x1p+0" type to their equivalent value in normal form using only bash scripts.

As far as I know, the syntax for conversion is :
Code:
$ printf "f\n" 0x1p+0
also we can use sed -i in bash ( or in vi/vim editor) for search and replace, but the question is kinda mixture of these two. in other word, I would like bash to search and spot these hex floating points and replace their value using the normal value in order to recompile the output .c cleanly.

Thanks for your time,

Amir
 
Old 09-13-2012, 11:18 PM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

First of all, I do not familiar with this syntax for floating point numbers and would be grateful if you provide some link to a place where it is explained.

I can suggest two a bit different approaches: 1) using GNU sed and 2) (preferred) - using combination of any flavour of sed and m4 macro processor.

1.
Code:
$ cat in8 
text with strange    numbers like this 0x1p+0
or this 0x12p+5 one
$ sed 's/0x[^p]\+p[+-]./`printf "%f" &`/; s/^/echo /e' in8
text with strange numbers like this 1.000000
or this 576.000000 one
We basically transform each input line to a shell command and evaluate it using /e (GNU sed extension). Note that echo squeezes all extra whitespaces, so it is probably not what you want, because your program become a real mess after this procedure.

2. I realized that this task can be naturally done using macro processor, and the m4 is a very good choice:
Code:
$ sed 's/0x[^p]\+p[+-]./esyscmd(printf "%f" &)/' in8 | m4
text with strange    numbers like this 1.000000
or this 576.000000 one
esyscmd(<shell command>) here is a m4 macro which expands to standard output of the shell command. See `info m4 esyscmd' for details.

EDIT: The dot in `0x[^p]\+p[+-].' means any character. I suspect that only digits can be there, and more than one, so you can change it to `0x[^p]\+p[+-][0-9]\+'

Last edited by firstfire; 09-13-2012 at 11:23 PM.
 
Old 09-14-2012, 07:11 AM   #3
AmirJamez
Member
 
Registered: Apr 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
Thanks firstfire for the reply, I am going to test it in some minutes.

By the way, for your reference, I will give you an explanation of Floating point HEX :

http://www.exploringbinary.com/hexad...int-constants/

Also in c99 standard, we can use printf for converting the HEX constant to normal form as bellow :

http://stackoverflow.com/questions/4...-constant-in-c

But as I found in bash also using "bash printf" with a few changes allowed like this :

http://linuxconfig.org/bash-printf-s...-with-examples


Best,

Amir
 
1 members found this post helpful.
Old 09-15-2012, 12:02 PM   #4
AmirJamez
Member
 
Registered: Apr 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
Thanks firstfire for the reply on the other post

1- but the point is; there are lots of .c files in a project, so I need to have printf output be replaced exactly on the same file, cuz otherwise it will be a mess.

2- other option could be a c program which I can place within the running scripts that search and replace the value to the original file.

best,

Amir
 
Old 09-16-2012, 12:18 AM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Quote:
Originally Posted by AmirJamez View Post
1- but the point is; there are lots of .c files in a project, so I need to have printf output be replaced exactly on the same file, cuz otherwise it will be a mess.
Well, actually it was a kind of homework for you Consider using redirections and a temporary file, e.g. (untested)
Code:
tempfile="/tmp/tempfile.c"
for f in *.c;
do
   sed 's/0x[^p]\+p[+-][0-9]\+/esyscmd(printf "%f" &)/' "$f" > $tempfile;
   m4 "$tempfile" > "$f";
done
Quote:
2- other option could be a c program which I can place within the running scripts that search and replace the value to the original file.
This way has its benefits, but mostly pedagogical ones. It is not that easy, as it may seem.
 
1 members found this post helpful.
Old 09-16-2012, 09:54 AM   #6
AmirJamez
Member
 
Registered: Apr 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
Thanks man, it worked.

But after compiling some files in the source I got this compiler error msg :

Code:
"llvm_transformed_context_ini.c", line 3411: compiler error: wasted space: 1047210000
actually line 4311, is the last line of this .c file without anything written on it, Do you have any idea?

Best,

Amir
 
Old 09-16-2012, 10:10 AM   #7
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Quote:
Originally Posted by AmirJamez View Post
actually line 4311, is the last line of this .c file without anything written on it, Do you have any idea?
Hmm... Remove it? The only links discussing similar problem I managed to look up [1,2] are related to Sun compiler. I myself have never seen such error message.

You can remove last line of the file, if it is empty, using
Code:
sed -i '${/^$/d}' file
 
Old 09-16-2012, 10:19 AM   #8
AmirJamez
Member
 
Registered: Apr 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
No, it deleted the last empty line but the error kept remaining.


Amir
 
Old 09-16-2012, 11:04 AM   #9
AmirJamez
Member
 
Registered: Apr 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
Oh by the way, in one of the other file I found some remaining HEX floating points which haven't be converted. is it like converting just one instance in a line ? since this lie had multiple HEX floating points and just the first one was being converted:

Code:
 llvm_cbe_tmp__938 = fprintf(llvm_cbe_tmp__937, ((&_OC_str67.array[((signed int )0u)])), llvm_cbe_tmp__936, 0u, llvm_cbe_tmp__934, (((double )llvm_cbe_tmp__932)), (((double )llvm_cbe_tmp__930)), (((double )llvm_cbe_tmp__928)), 0u, 0.000000, 0x0p+0, 0x0p+0, 0u, (((double )llvm_cbe_tmp__926)), (((double )llvm_cbe_tmp__924)), (((double )llvm_cbe_tmp__922)), 0u, (llvm_cbe_tmp__899 / (((double )(signed int )llvm_cbe_tmp__901))));
I used as you mentioned this script:

Code:
echo " HEX to normal floating point conversion"
tempfile="opt/tmp/tempfile.c"
for f in opt/*.c;
do
   sed 's/0x[^p]\+p[+-][0-9]\+/esyscmd(printf "%f" &)/' "$f" > $tempfile;
   m4 "$tempfile" > "$f";
done
Doesn't need to add -i to sed command ?

best,

Amir
 
Old 09-16-2012, 11:14 AM   #10
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Quote:
Originally Posted by AmirJamez View Post
Doesn't need to add -i to sed command ?

best,

Amir
No, you don't need -i. You need to add `g' flag to s///:
Code:
sed 's/0x[^p]\+p[+-][0-9]\+/esyscmd(printf "%f" &)/g' "$f" > $tempfile;
Then substitutions will be performed for each occurrence of regular expression on a line (not only the first one).
 
1 members found this post helpful.
Old 09-16-2012, 12:49 PM   #11
AmirJamez
Member
 
Registered: Apr 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
but there is an interesting point which is whenever I add sed -i, I can compiler them with -c without any error, the moment I remove it the compilation fails and the compilation error: wasted space appears.

best,

Amir
 
Old 09-17-2012, 05:06 AM   #12
AmirJamez
Member
 
Registered: Apr 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
Yes, you were right.

Anyways you can refer to this thread if you would like to answer my compilation error

Thanks for helping buddy


Amir
 
  


Reply

Tags
converting, float, printf, sed bash



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH :sed find and replace nano2 Linux - General 2 10-24-2011 04:56 PM
Bash search line and replace hex to binary ekim Programming 5 07-09-2011 01:24 AM
Floating points arithmeti in Shell script pc_catalyst Programming 6 12-08-2006 05:18 PM
gcc floating point to normal conversion morph_ind Programming 6 09-20-2005 10:46 AM
floating points, signal 8, joystick probs zippo85 Linux - Hardware 8 12-23-2004 01:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:53 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