SED - how do I convert a decimal number to asterisk


 
Thread Tools Search this Thread
Top Forums Programming SED - how do I convert a decimal number to asterisk
# 1  
Old 04-16-2012
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:

Code:
 head phendata.txt
201008809 0.0 0.0
201008810 0.0 0.0
201008813 0.0 0.0
201014103 0.703125 -0.262959
201014118 0.703125 -1.26296
201014136 0.0 0.0
201017233 0.0 0.0
201017322 0.703125 0.669447
201017640 0.703125 -0.330553
201024891 0.703125 2.16991

This is my command so far:

Code:
head phendata.txt | sed 's/\0.0/*/g'
2*08809 * *
2*0881*.*.0
2*08813 * *
2*14103 0.703125 -0.262959
2*14118 0.703125 -1.26296
2*14136 * *
2*17233 * *
2*17322 0.703125 0.669447
2*1764*.703125 -0.330553
2*24891 0.703125 2.16991

I don't want to take the long way and use awk - i would like to learn how sed specifically recognises 0.0 and not 00. Thanks in advance Smilie

Last edited by fpmurphy; 04-16-2012 at 09:59 AM..
# 2  
Old 04-16-2012
How about?

Code:
sed 's/ 0\.0/*/g'

# 3  
Old 04-16-2012
So what is the output you are expecting...
# 4  
Old 04-16-2012
Hmm i think this ( sed 's/ 0\.0/ */g') will work with a space added... Thanks!

The output should look like this (Column 2 and 3 0.0 values converted to asterisks)
PHP Code:
201008809 * *
201008810 * *
201008813 * *
201014103 0.703125 -0.262959
201014118 0.703125 
-1.26296
201014136 
* *
201017233 * *
201017322 0.703125 0.669447
201017640 0.703125 
-0.330553
201024891 0.703125 2.16991 
---------- Post updated at 06:32 PM ---------- Previous update was at 05:06 PM ----------

Hmm yeah that hasnt worked. For values of 0.0811563 it is replacing the 0.0...

PHP Code:
Y05DL0108 0.703125 1.08116
Y05DL0110 0.703125 
*811563
Y05DL0115 0.703125 
*811563 
# 5  
Old 04-17-2012
Quote:
Originally Posted by lel7lel7
Hmm yeah that hasnt worked. For values of 0.0811563 it is replacing the 0.0...

PHP Code:
Y05DL0108 0.703125 1.08116
Y05DL0110 0.703125 
*811563
Y05DL0115 0.703125 
*811563 
Try this one then...
Code:
sed 's/0\.0 /*/g;s/0\.0$/ */g' file

This User Gave Thanks to shamrock For This Post:
# 6  
Old 04-17-2012
For dealing with columns, use a tool that understands columns, like awk, so you can do a straightforward 'check if this exact column matches this exact string' comparison, instead of weird convolutions with sed to try and force a stream tool to do operations on columns.

Code:
awk '{ for (N=1; N<=NF; N++) if($N  == "0.0" ) $N="*" } 1' filename

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 04-17-2012
This is great, both of these are working for me. I appreciate the help!
Cheers.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Convert a numeric to 2 decimal point value

Hi , I have a file which contains text like A|Mau|Code|12|Detail B|Mau|Code|20|Header I want to write a command using awk which will output A|Mau|Code|12.00|Detail B|Mau|Code|20.00|Header I used a command like awk -F"|" {printf "%s|%s|%s|%.2f|%s",$1,$2,$3,$4,$5}' which does the... (4 Replies)
Discussion started by: LoneRanger
4 Replies

2. UNIX for Dummies Questions & Answers

Convert hexadecimal value to decimal value

Hi All, cat filename | awk '{print $1, $2, $4, $5, $6, $7, $8, $9, $10;}' | awk 'NF > 0' OUTPUT: 2015-01-19 00:12:32 00000000fbfa0000 000000009ae5cf80 014d 015d 0017 003c 0362de20 2015-01-19 00:13:52 00000000fc820000 00000000994c6758 014c 015d 000b 003c 08670250 2015-01-19 00:14:25... (12 Replies)
Discussion started by: sam@sam
12 Replies

3. Shell Programming and Scripting

Sed/awk command to convert number occurances into date format and club a set of lines

Hi, I have been stuck in this requirement where my file contains the below format. 20150812170500846959990854-25383-8.0.0 "ABC Report" hp96880 "4952" 20150812170501846959990854-25383-8.0.0 End of run 20150812060132846959990854-20495-8.0.0 "XYZ Report" vg76452 "1006962188"... (6 Replies)
Discussion started by: Chinmaya Kabi
6 Replies

4. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

5. Shell Programming and Scripting

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

Hello I have a file of the form ... 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: ... num 00.12 num num num 25.53 num num num 07.82 num... (10 Replies)
Discussion started by: phaethon
10 Replies

6. UNIX for Dummies Questions & Answers

Convert hexa decimal to decimal

Hi, I want to convert two hexadecimal numbers to decimal using unix command line. 1cce446295197a9d6352f9f223a9b698 fc8f99ac06e88c4faf669cf366f60d I tried using `echo "ibase=16; $no |bc` printf '%x\n' "1cce446295197a9d6352f9f223a9b698" but it doesn't work for such big number it... (4 Replies)
Discussion started by: sudhakar T
4 Replies

7. Shell Programming and Scripting

Convert hexadecimal value in decimal value

hi all, this is my script: #! /bin/sh minutes=$( { i2cget -f -y 0 0x51 3; } 2>&1 ) minutes=${minutes:2} hour=$( { i2cget -f -y 0 0x51 4; } 2>&1 ) hour=${hour:2} day=$( { i2cget -f -y 0 0x51 5; } 2>&1 ) day=${day:2} month=$( { i2cget -f -y 0 0x51 7; } 2>&1 ) month=${month:2} ... (6 Replies)
Discussion started by: enaud
6 Replies

8. Shell Programming and Scripting

Convert hex to decimal

can someone help me in converting hex streams to decimal values using perl script Hex value: $my_hex_stream="0c07ac14001676"; Every hex value in the above stream should be converted in to decimal and separated by comma. The output should be: 12,07,172,20,00,22,118 (2 Replies)
Discussion started by: Arun_Linux
2 Replies

9. Shell Programming and Scripting

Convert exponential value to decimal

Hi, Here is my script to read a file into array: awk -F '+' ' # load first file into array indexed by fields 1 and 2 NR == FNR { file1nr = FNR for (i=3; i<NF; i++) { file1 = $i } I have this... (5 Replies)
Discussion started by: Sangtha
5 Replies
Login or Register to Ask a Question