Sed, numbers and multiplication


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed, numbers and multiplication
# 1  
Old 03-07-2011
Sed, numbers and multiplication

Hi there.

I've used Sed to pull out some numbers, can it also be used to perform calculations on these numbers?

For example I have a text file with a list of weights however some of them are presented like 24x18g, I actually need it to multiple the two number and display it as 432g

24x18g
12x18g
300g
..
..

Many thanks
# 2  
Old 03-07-2011
Please post a data sample
# 3  
Old 03-07-2011
Given the lines in your original post, the following should work:
Code:
sed 'y/x/*/; s/g$//' file | bc | sed 's/$/g/'

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 03-07-2011
AWK:
Code:
echo "24x18g
12x18g
300g" |awk -F"x" '{print NF==1?$0:$1*gensub("g","",1,$2)"g"}'
432g
216g
300g

This User Gave Thanks to yinyuemi For This Post:
# 5  
Old 03-07-2011
Code:
echo "24x18g
12x18g
300g" | awk -Fx '{$0=($1*($2?int($2):1))"g"}1'
432g
216g
300g

These 2 Users Gave Thanks to danmero For This Post:
# 6  
Old 03-08-2011
Hi Dan, thanks for you script - works great however I need it to keep the 'g' and 'Kg' characters in there, at the moment it strips them out, is there anyway with AWK to do this?

Regards
# 7  
Old 03-08-2011
Code:
$ cat infile
24x18g
12x18g
300g
24x18Kg
12x18Kg
300Kg

$ awk -Fx '{t=$0;gsub(/[0-9x]/,"",t);$0=($1*($2?int($2):1)) t}1' infile

432g
216g
300g
432Kg
216Kg
300Kg

These 2 Users Gave Thanks to rdcwayx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed to replace / between the two numbers

i Have a file as following view pz19a0c0/1000T_J_3MoDw9DSLh1ZsCubdua-LKOQmbtiVgkIsiMbSiwF467?sessionId=15451401994597121249 view pz19a0c0/100086X67pR0MwzWnhhSO6sAEoxeFMyhh-IIbUCCdxicaQM4FC9?sessionId=154514019945971212494898 view/cart ... (5 Replies)
Discussion started by: Raghuram717
5 Replies

2. Shell Programming and Scripting

Help about using variables of float numbers in sed

Hi, I need to run a Fortran program which reads a input file with a fixed name many times, each time I need to change a number (real) in that input file, this is how I currently do it and I know it is not elegent at all: cp inputfile.dat backup.dat sed -i 's/28.0/0.01/g' inputfile.dat ./myCode... (3 Replies)
Discussion started by: dypang
3 Replies

3. Shell Programming and Scripting

Using sed to show even numbers and the line

Hello, I have an assignment that requires us to use sed only. The first part of the assignment says to use a sed script to print only the even lines, but if it is print, the number of that lines needs to be printed before the text. ex: 2 line 2 text 4 line 4 text 6 line 6 text ... (3 Replies)
Discussion started by: stevent518
3 Replies

4. Shell Programming and Scripting

sed extracting numbers

I have number 192.168.21.8. I want to extract from this number with sed 21 and 8 to variables a and b. Any Ideas? I did like 's/\(192.168.\)/ /' but its wrong :( (6 Replies)
Discussion started by: Natalie
6 Replies

5. Shell Programming and Scripting

using sed to find and replace multiple numbers

I have looked around and there are several examples of how to use sed, but I don't think any of them help me very much with what I am trying to do. I have a text file like this.... 1! SRCNAM = 00001 ! 1! X = 50.0000, 0.0000,... (10 Replies)
Discussion started by: mercury.int
10 Replies

6. Shell Programming and Scripting

Sed only digits not in numbers

Hi, I have a text file with an array of numbers such as : 123 1 456 45 9817 1 45 I would like to replace the digit "1" in a text file with "A". So it looks like this: 123 A 456 45 9817 A 45 If I use sed 's/1/A/g', I get A23 A 456 45 98A7 A 45 I... (3 Replies)
Discussion started by: jejeking
3 Replies

7. Shell Programming and Scripting

sed with numbers

Patches have the following syntax: Patchxxxx.xxx where x is a number. I'm trying to strip the .xxx off of the patch, so I tried this: sed 's/PATCH./Patch/' patchin.log > patchout.log But that of course changed Patchxxxx.xxx to Patch. Ooops, what is the syntax i'm looking for? thanks (3 Replies)
Discussion started by: dba_frog
3 Replies

8. Shell Programming and Scripting

sed command, look for numbers following letters

If I have a set of strings, C21 F231 H42 1C10 1F113 and I want to isolate the ints following the char, what would the sed string be to find numbers after letters? If I do, *, I will get numbers after letters, but I am looking to do something like, sed 's/*/\t*/g' this will give me... (14 Replies)
Discussion started by: LMHmedchem
14 Replies

9. Shell Programming and Scripting

sed output with numbers

I get the same value "chinchwad" for the following 3 statements. echo "ABCDchinchwad18-Mar-2010-11.sql.zip" | sed -r 's/(+)(+)(.*)/\2/' echo "ABCDchinchwadII18-Mar-2010-11.sql.zip" | sed -r 's/(+)(+)(.*)/\2/' echo "ABCDchinchwad918-Mar-2010-11.sql.zip" | sed -r 's/(+)(+)(.*)/\2/' I expect:... (4 Replies)
Discussion started by: shantanuo
4 Replies

10. Shell Programming and Scripting

Sed to grep only numbers in string

Hi, I would like to get only number in the following strings. var1="Type20" var2="type 3" var3="value 2" var4="Type 1 Datacenter Hall 2" I would like to extract output as 20 from var1 and 3 from var2,2 from var3 and 1 from var4. Appreciate any one help asap.. Regards, Aji (5 Replies)
Discussion started by: ajilesh
5 Replies
Login or Register to Ask a Question