Sponsored Content
Top Forums Shell Programming and Scripting Round values only when it's numerics Post 303012149 by nengcheng on Tuesday 30th of January 2018 01:10:15 PM
Old 01-30-2018
Quote:
Originally Posted by Yoda
sprintf is an awk string function. For further reference check: String Functions
Code:
sprintf(format, expression1, ...)
Return (without printing) the string that printf would have printed out with the same arguments

So I am using this function to format and then assign the result instead of printing.
Great, that's an elegant solution.
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conversion to display leading zeros for numerics

I have the following script (which works fine), escept I don't know how to make the MONTH and DAY show up with leading zeros. I have a statement (not in this script) which will show this in a YYYYMMDD format, but the script makes the MONTH and DAY fields show single digits. For today, as an... (4 Replies)
Discussion started by: dsimpg1
4 Replies

2. UNIX for Dummies Questions & Answers

how to round a value

Hello, In a unix shell script,i want to round a variabele to a nearest number Ex: set count=104.4 How can i round that to 105.? Thanks, Sateesh (2 Replies)
Discussion started by: kotasateesh
2 Replies

3. Shell Programming and Scripting

round in KSH

Is there an easy way to round a number up in Korn shell? ie. 10.4 --> 11 Thanks. (6 Replies)
Discussion started by: here2learn
6 Replies

4. Shell Programming and Scripting

round a number

In a shell script - How do I round a decimal number (contained in a variable) to the nearest whole number? (2 Replies)
Discussion started by: achieve
2 Replies

5. Shell Programming and Scripting

Drop records with non-numerics in field X

I have tab delimited file and need to remove all records prior to sort, that have non-numerics in the Field 2. valid/invalid field 2 data examples are: " 123" valid "1 23" invalid " NOPE" invalid I've tried this awk it does not recognize tab as the delimiter or check... (3 Replies)
Discussion started by: akxeman
3 Replies

6. Shell Programming and Scripting

round decimal values and use in loops

i have a file in which 3 values are stored like --4.72 4.42 3.86 what i wanna do is that take read each value and compare with a fixed value say 5 but cant do so as i am getting an error for the same. please check the code cat /home/nsadm/auto/Logging/uptime.txt| while read a b c do if... (2 Replies)
Discussion started by: gemnian.g
2 Replies

7. Shell Programming and Scripting

Round up the decimals

Hi All, I would like to do the following in the shell script 561.76 to 562 I tried using this echo 'scale=0; 749 * 75 /100 ' | bc but just returned only 561 Please help me . I appreciate your help Thanks rajeevm (13 Replies)
Discussion started by: rajeevm
13 Replies

8. Shell Programming and Scripting

How to extract 4th field if numerics?

I have a file which contains fields comma separated & with each field surrounded by quotes. The 4th field contains either a serial number, the text ABC, the text XYZ or it's blank. I want to only extract records which have a serial number. Here's some sample data: > cat myfile... (4 Replies)
Discussion started by: CHoggarth
4 Replies

9. Shell Programming and Scripting

Grepping non-alpa-numerics from first column only

I have data in the following tab-separated format (consists of 200 columns all together, this is just a sampling) </s> 0.001701 0.002025 0.002264 0.001430 -0.001300 . -0.205240 0.177341 -0.426209 -0.661049 -0.048884 0.027032 the -0.159145 0.084377 0.056968 0.050934 0.160689 of -0.230698... (7 Replies)
Discussion started by: owwow14
7 Replies
Format(3)						User Contributed Perl Documentation						 Format(3)

NAME
String::Format - sprintf-like string formatting capabilities with arbitrary format definitions ABSTRACT
String::Format allows for sprintf-style formatting capabilities with arbitrary format definitions SYNOPSIS
use String::Format; my %fruit = ( 'a' => "apples", 'b' => "bannanas", 'g' => "grapefruits", 'm' => "melons", 'w' => "watermelons", ); my $format = "I like %a, %b, and %g, but not %m or %w."; print stringf($format, %fruit); # prints: # I like apples, bannanas, and grapefruits, but not melons or watermelons. DESCRIPTION
String::Format lets you define arbitrary printf-like format sequences to be expanded. This module would be most useful in configuration files and reporting tools, where the results of a query need to be formatted in a particular way. It was inspired by mutt's index_format and related directives (see <URL:http://www.mutt.org/doc/manual/manual-6.html#index_format>). FUNCTIONS
stringf String::Format exports a single function called stringf. stringf takes two arguments: a format string (see FORMAT STRINGS, below) and a reference to a hash of name => value pairs. These name => value pairs are what will be expanded in the format string. FORMAT STRINGS
Format strings must match the following regular expression: qr/ (% # leading '%' (-)? # left-align, rather than right (d*)? # (optional) minimum field width (?:.(d*))? # (optional) maximum field width ({.*?})? # (optional) stuff inside (S) # actual format character )/x; If the escape character specified does not exist in %args, then the original string is used. The alignment, minimum width, and maximum width options function identically to how they are defined in sprintf(3) (any variation is a bug, and should be reported). Note that Perl's sprintf definition is a little more liberal than the above regex; the deviations were intentional, and all deal with numeric formatting (the #, 0, and + leaders were specifically left out). The value attached to the key can be a scalar value or a subroutine reference; if it is a subroutine reference, then anything between the '{' and '}' ($5 in the above regex) will be passed as $_[0] to the subroutine reference. This allows for entries such as this: %args = ( d => sub { POSIX::strftime($_[0], localtime) }, ); Which can be invoked with this format string: "It is %{%M:%S}d right now, on %{%A, %B %e}d." And result in (for example): It is 17:45 right now, on Monday, February 4. Note that since the string is passed unmolested to the subroutine reference, and strftime would Do The Right Thing with this data, the above format string could be written as: "It is %{%M:%S right now, on %A, %B %e}d." By default, the formats 'n', 't', and '%' are defined to be a newline, tab, and '%', respectively, if they are not already defined in the hashref of arguments that gets passed it. So we can add carriage returns simply: "It is %{%M:%S right now, on %A, %B %e}d.%n" Because of how the string is parsed, the normal " " and " " are turned into two characters each, and are not treated as a newline and tab. This is a bug. FACTORY METHOD
String::Format also supports a class method, named stringfactory, which will return reference to a "primed" subroutine. stringfatory should be passed a reference to a hash of value; the returned subroutine will use these values as the %args hash. my $self = Some::Groovy::Package->new($$, $<, $^T); my %formats = ( 'i' => sub { $self->id }, 'd' => sub { $self->date }, 's' => sub { $self->subject }, 'b' => sub { $self->body }, ); my $index_format = String::Format->stringfactory(\%formats); print $index_format->($format1); print $index_format->($format2); This subroutine reference can be assigned to a local symbol table entry, and called normally, of course: *reformat = String::Format->stringfactory(\%formats); my $reformed = reformat($format_string); LICENSE
"String::Format" is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2. AUTHOR
darren chamberlain <darren@cpan.org> perl v5.16.3 2009-05-17 Format(3)
All times are GMT -4. The time now is 10:22 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy