Find and replace floating HEX points with its normal equivalent value in bash
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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]\+'
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.
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.
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
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:
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.