Sed decimal issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed decimal issue
# 1  
Old 03-08-2011
Sed decimal issue

Hi

I've been trying to add a decimal place to a list of weights in a file but have been having problems with the decimal place for the Kg.

I can convert the g weights with this code:
Code:
sed -i "/[0-9][g]/s/^/0./"

How do I convert the Kg weights into decimal so that 1Kg would be 1.0Kg
Code:
250g
500g
750g
1Kg
1.2Kg
30g
375g
500g
30g

Many thanks

Last edited by Franklin52; 03-08-2011 at 03:11 AM.. Reason: Please use code tags
# 2  
Old 03-08-2011
Code:
awk '/Kg/ {printf "%.1fKg\n", $1;next} {printf "%.3fKg\n", $1/1000}' infile

0.250Kg
0.500Kg
0.750Kg
1.0Kg
1.2Kg
0.030Kg
0.375Kg
0.500Kg
0.030Kg


Last edited by rdcwayx; 03-08-2011 at 12:42 AM..
This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 03-08-2011
Code:
sed -r -e 's/(^[0-9])Kg/\1.0Kg/' -e 's/(^[0-9]{1})g$/0.00\1Kg/' -e 's/(^[0-9]{2})g$/0.0\1Kg/' -e 's/(^[0-9]{3})g$/0.\1Kg/'

This User Gave Thanks to yinyuemi For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. Shell Programming and Scripting

How to round up value upto 2 decimal places using sed?

Please help me in rounding up value upto 2 decimal palces using sed command #!/usr/bin/bash a=15.42 b=13.33 c=`echo $a*$b |bc -l` echo $c above code is is giving output "205.5486" but i want the output as "205.55" Thank you... (15 Replies)
Discussion started by: ranabhavish
15 Replies

5. 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

6. Shell Programming and Scripting

Remove comma from decimal value using sed command

Hi Experts , My requirement is like this .. I have source comming as 4,234.55 I need and out put = 4234.55 I need to write a sed command .. I have already used sed command for multiple conditions in a file for replacing comma , double quotes , brackets , retain negative values..... (3 Replies)
Discussion started by: bshivali
3 Replies

7. Homework & Coursework Questions

Decimal to BCD (Binary Coded Decimal)

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Design an algorithm that accepts an input a decimal number and converts it into BCD (Binary... (2 Replies)
Discussion started by: caramba
2 Replies

8. UNIX for Dummies Questions & Answers

Decimal to BCD (Binary Coded Decimal)

Anybody please help me... Design an algorithm that accepts an input a decimal number and converts it into BCD (Binary Coded Decimal) representation. Also, draw its Flow Chart. This is a unix qn... plz post algorithm for that :confused: (1 Reply)
Discussion started by: caramba
1 Replies

9. Shell Programming and Scripting

Issue with a sed one liner variant - sed 's/ ; /|/g' $TMP1 > $TMP

Execution of the following segment is giving the error - Script extract:- OUT=$DATADIR/sol_rsult_orphn.bcp TMP1=${OUT}_tmp1 TMP=${OUT}_tmp ( isql -w 400 $dbConnect_OPR <<EOF select convert(char(10), s.lead_id) +'|' + s.pho_loc_type, ";", s.sol_rsult_cmnt, ";", +'|'+ s.del_ind... (3 Replies)
Discussion started by: kzmatam
3 Replies
Login or Register to Ask a Question