Visit Our UNIX and Linux User Community


awk: round output or delimit output of arithmatic string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk: round output or delimit output of arithmatic string
# 1  
Old 06-18-2010
awk: round output or delimit output of arithmatic string

I have a file with the following content.

Code:
> cat /tmp/internetusage.txt
6709.296322 30000 2/7/2010 0.00

I am using the following awk command to calculate a percentage from field 1 and 2 from the file.

Code:
awk '{ print $1/$2*100 }' /tmp/internetusage.txt

This outputs the value "22.3643" as a percent but I want to just round it to 22.

I realise I could use cut or another awk using "." as the delimiter to achieve this but I would like to just use one awk command if possible. My googling has not been successful in finding a solution.

Thanks
# 2  
Old 06-18-2010
Hi.

Use printf instead of print

Code:
$ awk '{ printf "%d\n", $1/$2*100 }' /tmp/internetusage.txt
22

This User Gave Thanks to Scott For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #920
Difficulty: Medium
The Unix time number is zero at the Unix epoch, and increases by exactly 86400 per day since the epoch.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To get a single string in output

I am looking forward to achive below expecrted result, but when I am trying i could now get the whole string between two / / what I am getting is the last word i need to take complete line which is between / / ignore the line after , and before / which is (Test failed: text expected not to... (8 Replies)
Discussion started by: mirwasim
8 Replies

2. Shell Programming and Scripting

Delimit file based on character length using awk

Hi, I need help with one problem, I came across recently. I have one input file which I need to delimit based on character length. $ cat Input.txt 12345sda231453 asd760kjol62569 sdasw4g76gdf57 And, There is one comma separated file which mentions "start of the field" and "length... (6 Replies)
Discussion started by: Prathmesh
6 Replies

3. Shell Programming and Scripting

Getting awk Output as number instead of String

Hi All, I have a file a.txt, content as mentioned below: 22454750 This data in this control file and I have a variable called vCount which contains a number. I need to extract the 22454750 from the above file and compare with the variable vCount. If match fine or else exit. ... (5 Replies)
Discussion started by: Arun Mishra
5 Replies

4. Shell Programming and Scripting

Awk script to run a sql and print the output to an output file

Hi All, I have around 900 Select Sql's which I would like to run in an awk script and print the output of those sql's in an txt file. Can you anyone pls let me know how do I do it and execute the awk script? Thanks. (4 Replies)
Discussion started by: adept
4 Replies

5. Shell Programming and Scripting

awk search an output string between two strings

I would like to search for strings stored in searchstringfile.txt in inputfiles. searchstringfile.txt J./F. Gls. Wal F. Towerinput1.txt What is needed is J./F. 12 var Gls. Wal 16 interp. Tower 12 input2.txt Awk shall search for F. 16 pt. J./F. 22 output.txt input1.txt J./F. = 12 var... (3 Replies)
Discussion started by: sdf
3 Replies

6. UNIX for Advanced & Expert Users

Date Conversion on output string from awk

Hi, I would like to convert the output from awk function to date and print on the screen. Example : echo "Start Date: May 24 2010" | gawk -F": " '{print $2}' Output : May 04 2010 I want this to be converted to 2010/05/24 Can i use date function here and how? Thanks, Deepika (2 Replies)
Discussion started by: deepikad
2 Replies

7. Shell Programming and Scripting

redirect an awk string output to a script input with pipes

Hi, I have a function in a bash script that returns a string after some operations using awk. The following code returns 555 $VARIABLE="EXAMPLE" get_number $VARIABLE this value I'd like to pass it as a second argument of another script with the following usage myscript.sh <param1>... (7 Replies)
Discussion started by: rid
7 Replies

8. Shell Programming and Scripting

how to find a particular string from a set of string output

Hi , I have a line by line output as follows,for example output of ls sample1 sample2 sample i need to check if this output contains the exact string sample.If i use grep , it will find out all strings that contain sample as a part of their word.I dont want to do a pattern matching... (11 Replies)
Discussion started by: padmisri
11 Replies

9. Shell Programming and Scripting

arithmatic with awk

hi, i am trying some awk arthmatic calculation,i have a problem if any one can help let say if i have a file exm.txt 3 + 2 3 * 2 3 / 2 3 - 2 the output expected is awk -f exm.awk exm.txt 3 + 2 = 5 3 * 2 = 6 3 / 2 = 1.5 3 - 2 = 1 i simply used exm.awk { print $1 " + " $3 "= " $1 +... (3 Replies)
Discussion started by: phone_book
3 Replies

10. Shell Programming and Scripting

output a particular string

Hi experts, I have many files it contains like below. GET:SUB:MSI,7601; RESP:0:SISDN,72030067:MSI,7601:T11,1:T21; GET:SUB:MSI,7602; RESP:0:SISDN,72030068:MSI,7602:T11,1:T21; GET:SUB:MSI,7603; Resp:1000; GET:SUB:MSI,7604; Resp:1000; GET:SUB:MSI,7605;... (4 Replies)
Discussion started by: thepurple
4 Replies

Featured Tech Videos