Decimal conversion number of the form x.xx to 0x.xx, sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Decimal conversion number of the form x.xx to 0x.xx, sed?
# 1  
Old 09-12-2014
Decimal conversion number of the form x.xx to 0x.xx, sed?

Hello
I have a file of the form

Code:
...
num  0.12 num num
num 25.53 num num
num  7.82 num num
......

and I want to convert the 2nd field of each line adding a "0" at the numbers >= 0 and < 10 so the output will have the form:

Code:
...
num  00.12 num num
num 25.53 num num
num  07.82 num num
...

I tried with
Code:
sed 's/ 7./07./2'

but it is not white space sensitive so it replaces any occurance of "7."

Thanks in advance for the help
# 2  
Old 09-12-2014
Try
Code:
sed 's/^num  \([0-9]\)\./num 0\1\./'

This User Gave Thanks to junior-helper For This Post:
# 3  
Old 09-12-2014
thank you for the reply but it does not work : (
# 4  
Old 09-12-2014
What/How does not work *exactly*?
# 5  
Old 09-12-2014
I use
Code:
 sed 's/^num  \([0-9]\)\./num 0\1\./' file1 > file2
diff file1 file2

and they are the same (using GNU sed)
# 6  
Old 09-12-2014
but what is in file1 ?
Because based on what you gave us in post#1 I see no reason why it should not work...
# 7  
Old 09-12-2014
Quote:
Originally Posted by vbe
but what is in file1 ?
Because based on what you gave us in post#1 I see no reason why it should not work...
Thanks for the reply.
The form of this file is

Code:
...
201401031200271538 24.50  21 48.70   8.94   0.00 22  8  ...
201401031904573638  5.25  22  2.88  19.15   0.00 12 10 ...
201401041845314038 21.84  21 49.73   9.31   0.00 23 10 ...
...

and the output should be:
Code:
...
201401031200271538 24.50  21 48.70   8.94   0.00 22  8  ...
201401031904573638 05.25  22  2.88  19.15   0.00 12 10 ...
201401041845314038 21.84  21 49.73   9.31   0.00 23 10 ...
...

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

SED - how do I convert a decimal number to asterisk

Hi, My animal ID's have two zeros in them and are also converting to asterisk. I only need to change the zero values in columns two and three. I would appreciate any help. This is my data structure: head phendata.txt 201008809 0.0 0.0 201008810 0.0 0.0 201008813 0.0 0.0 201014103... (6 Replies)
Discussion started by: lel7lel7
6 Replies

2. Shell Programming and Scripting

NAWK conversion of hexadecimal input to decimal output via printf, I am close I can feel it

I have searched and the answers I have found thus far have led me to this point, so I feel I am just about there. I am trying to convert a column of hexadecimal to decimal values so that I can filter out via grep just the data I want. I was able to pull my original 3 character hex value and... (10 Replies)
Discussion started by: PCGameGuy
10 Replies

3. Shell Programming and Scripting

number of digits after decimal

Hi All, I have a file of decimal numbers, cat file1.txt 1.1382666907 1.2603107334 1.6118799297 24.4995857056 494.7632588468 560.7633734425 ..... I want to see the output as only 7 digits after decimal (5 Replies)
Discussion started by: senayasma
5 Replies

4. Shell Programming and Scripting

IP address to decimal format conversion

I have a file which consist of some class 4 IP address as 172.16.112.50 172.16.112.50 172.16.112.50 172.16.112.100 192.168.1.30 172.16.112.100 172.16.112.50 172.16.112.50 172.16.112.50 i want to store them in pure decimal notations instead of the given dotted decimal formats e.g.... (2 Replies)
Discussion started by: vaibhavkorde
2 Replies

5. Shell Programming and Scripting

changing number in bash (number is in form of string)

I have a txt file as database. when i run my program what it does is it ask me for 3 name and stored in the file as name1:name2:name3:1 when u enter 3 name it add those in file as above format and add 1 at the end. I what i want is if i enter same names again it changes that 1 to 2 and so... (3 Replies)
Discussion started by: Learnerabc
3 Replies

6. Shell Programming and Scripting

Add zero in decimal number

echo "scale=2; 282.73/640" | bc This will print .44 How to make the variable as 0.44 (2 Replies)
Discussion started by: sandy1028
2 Replies

7. Shell Programming and Scripting

Decimal to hex conversion

Dear All PROs Thanks in advance need a shell for Decimal to hex conversion input file (decimal values) 65,5,48,66,133,131,118,47 65,5,48,66,133,131,83,63 . . desire output should be (Hex value)... (11 Replies)
Discussion started by: The_Archer
11 Replies

8. Shell Programming and Scripting

need help with ascii to decimal conversion

Hi, Can anyone please help me ascci to decimal conversion in bash I have a file which contains stream of numbers like this,these are ascci values 729711810132973278105991013268971213233 I want to covert it to its actual value like upper code's decimal is "Have a Nice Day!" ... (15 Replies)
Discussion started by: sunilmenhdiratt
15 Replies

9. Shell Programming and Scripting

Decimal to Hexadecimal conversion

Hi frnds :) I need a small help... I have a very long file containing 20 digits decimal number which i want to convert into the corresponding 16 digit hexadecimal values. File looks like.... 11908486672755551741 05446378739602232559 04862605079740156652 . . . I tried the script for i... (7 Replies)
Discussion started by: vanand420
7 Replies
Login or Register to Ask a Question