awk if condition match and fix print decimal place


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
awk if condition match and fix print decimal place

Hi All,
I have problem in the middle of implementing to users, whereby the complaint is all about the decimal place which is too long. I need two decimal places only, but the outcome from command is always fixed to 6.
See the sample :
before:
Code:
Sort Total
      Site    Sort  SortName   Parts   %/Total
----------------------------------------------
               9                1    0.06
              10               6    0.35

using command:
Code:
awk '$1 ==    10 {$2-=1;$3=$2/1720*100} 1' inputfile|sed 's/^10 /               10               /g'

result:
Code:
Sort Total
      Site    Sort  SortName   Parts   %/Total
----------------------------------------------
               9                1    0.06
              10               5 0.290698

The thing is, I need the 0.290698 above to just have 2 decimal places, become --> 0.29
THks !

Desire result:
Code:
Sort Total
      Site    Sort  SortName   Parts   %/Total
----------------------------------------------
               9                1    0.06
              10               5    0.29

# 2  
Hello horsepwoer,

Could you please try following and let us know if this helps.
Code:
awk '$1 ==    10 {$2-=1;$3=$2/1720*100;printf "\t\t%d\t\t%d    %.2f\n",$1, $2, $3;next} 1'  Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Try:
Code:
awk '$1==10{$2--; $3=$2/17.2} 1' CONVFMT="%7.2f" file | sed 's/^10 /               10              /g'

or
Code:
awk '$1==10{sub($2 "[ \t]*" $3, sprintf("%d %7.2f", --$2, $2/17.2))}1' file

or
Code:
awk '$1==10{$2--; $3=$2/17.2; printf "%16d %15d %7.2f\n", $1, $2, $3; next}1'  file

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Thks guys, all the example above is working fantastically !
At the end i insert this "CONVFMT="%7.2f"" into the command, and it works perfectly Smilie
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #299
Difficulty: Easy
Novell bought the SUSE (then SuSE) brands and trademarks in 2003.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Zero padding a Number before and after a decimal place

Hi I was hoping someone could help me with a sed script I am trying to write? I am on a Mac running ElCapitan I have some text that I have converted from a pdf that I want to format into an xml file. In the file I have managed to delete all the text I do not need. The text I have left is... (8 Replies)
Discussion started by: Paul Walker
8 Replies

2. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. UNIX for Dummies Questions & Answers

How to trim the decimal place for all the columns?

Dear all, I have a file call test.txt which has 2000 columns, 1000 rows. If I want to trim all the columns to 3 decimal places, how can I do it? I know how to use awk prinf to trim specic columns. But I don't know how to trim all the columns. Thank you. data sample: 0.976004565 9.34567845... (6 Replies)
Discussion started by: forevertl
6 Replies

4. Shell Programming and Scripting

Using awk to place decimal points at proper position

Hi, I have one input file which is delimited by pipe. I want to put decimal points in this input file at particular position in particular column and also get the negative sign (if any) at start of that column. $ cat Input_file.txt 11|10102693|1|20151202|10263204|20151127|N|0001... (7 Replies)
Discussion started by: Prathmesh
7 Replies

5. UNIX for Dummies Questions & Answers

How to set decimal place in awk?

Dear all, I have a data test.txt as below. X22.30799720_T cg03868770 -0.5645412582127 2.4084685750406e-175 X22.30781182_A cg03868770 -0.5620426397492 3.5818034129169e-172 X22.30780724_C cg03868770 -0.5616890165605 2.9765569717858e-168 what I want is: X22.30799720_T cg03868770... (3 Replies)
Discussion started by: forevertl
3 Replies

6. Shell Programming and Scripting

Print hex Ip address in decimal format inside awk script

Hi to all, May someone help me with the following. The awk script below is part of a bigger awk script, and this part attempts to print an Ip address that is in hex format in decimal format. I'm trying with following code but I'm getting 0.0.0.0 and the correct answer is 192.168.140.100 ... (9 Replies)
Discussion started by: Ophiuchus
9 Replies

7. Shell Programming and Scripting

Unable to get the Specific column with 2 decimal place

Hi, I have an issue converting decimal places of a particular column, i am using below script to get the output, but the output is not generating in desired format. awk -F"," 'BEGIN{OFS=","}{if(NR==0)getline;if ($7 != "") {if ($7 > 0) $7=$7/100 ; {printf "%.2f"... (3 Replies)
Discussion started by: rramkrishnas
3 Replies

8. Shell Programming and Scripting

awk if condition match and print all

Hi, I am trying to do something like this ... I use awk to match a pattern, and then print out all col. My code is : awk '{if ($1 ==300) print $1,$2-'$sbin7',$3}' tmp.txt output= 300 2 whereby sbin7=2, The thing is, I want to print all col and row, not just the matched line/row only, but... (10 Replies)
Discussion started by: horsepower
10 Replies

9. Shell Programming and Scripting

awk to print lines based on string match on another line and condition

Hi folks, I have a text file that I need to parse, and I cant figure it out. The source is a report breaking down softwares from various companies with some basic info about them (see source snippet below). Ultimately what I want is an excel sheet with only Adobe and Microsoft software name and... (5 Replies)
Discussion started by: rowie718
5 Replies

10. Shell Programming and Scripting

add numbers with decimal place in UNIX

i am trying to add numbers with decimal place and I am prompted with an error. this expr command works :add=`expr 1 + 1` :echo $add :2 but when i am trying to add :addThis=`expr 1.1 + 1` :expr: An integer value was expected. is there a way to add numbers with decimal place in UNIX? (4 Replies)
Discussion started by: tads98
4 Replies

Featured Tech Videos