Sponsored Content
Top Forums Shell Programming and Scripting awk treating variables differently in UNIX-Linux Post 302980764 by Don Cragun on Friday 2nd of September 2016 03:29:01 AM
Old 09-02-2016
The standards describe the 0 flag in generic printf format arguments as follows:
Quote:
For a, A, d, e, E, f, F, g, G, i, o, u, x, and X conversion specifiers, leading zeros
(following any indication of sign or base) shall be used to pad to the field width
rather than performing space padding, except when converting an infinity or NaN.
If the '0' and '−' flags both appear, the '0' flag shall be ignored. For d, i, o, u,
x, and X conversion specifiers, if a precision is specified, the '0' flag shall be
ignored. For other conversion specifiers, the behavior is undefined.
Since the s format conversion specifier is not in the above list and the awk printf function description does not specify any changes to the generic format rules that apply in this case, the behavior of %05s is undefined and different versions of awk are allowed to do anything they want in this case. It appears that the awk you are using on your UNIX branded system uses the 0 flag to zero fill the string, the awk you are using on your Linux distribution ignores the 0 in the format specification, and other versions of awk might or might not exit with an illegal format specification error or drop core.
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

2. Programming

How to convert byteArray variables to HexaString variables for Linux?

Hello everybody, I am having problem in converting byte array variables to Hexa String variables for Linux. I have done, converting byte array variables to Hexa String variables for Windows but same function doesn't work for linux. Is there any difference in OS ? The code for Windows is given... (2 Replies)
Discussion started by: ritesh_163
2 Replies

3. Shell Programming and Scripting

simple CSH Script behaves differently on Solaris and RedHat Linux

I have a simple csh-script on a Solaris Workstaion which invokes the bc calculator: #!/bin/csh set shz=2 set zshift=5 set shzp=`bc -l <<END \ scale = 3 \ -1. * $shz + $zshift \ END` echo $shzp The result ($shzp) in this case is 3 (-1*2+5). It works fine on Solaris 8. ... (2 Replies)
Discussion started by: two reelers
2 Replies

4. Windows & DOS: Issues & Discussions

Awk script in DOS and Linux behaves differently :(

Hi, I have an awk script which performs simple operations of variable assignments and finally printing the variables in custom form. BEGIN {FS=OFS="\n"} { v1=substr($0,1,15) v2=substr($0,16,200) v3=substr($0,216,20) print v1 "|" v2 "|" v3 } The input file being processed... (2 Replies)
Discussion started by: vidyak
2 Replies

5. UNIX for Advanced & Expert Users

Unix script rm behaiving differently in difference machine

Hi All The below is the script, if i supply the script with the argument -f then it will delete the tmp file . We are having 3 unix system out of that two system are processing this file only one system is not processing and the error showin is below the script .. can you please help me ... (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

6. Shell Programming and Scripting

awk - treating remaining columns as one

Hi all, For no particular reason, I would like to use awk on a file that contains multiple columns, but let's say only columns 1 and 2 have some text values, and the remainder of the line contains text that I would like to treat as one column, considering I have spaces as delimiter for the... (33 Replies)
Discussion started by: ppucci
33 Replies

7. Shell Programming and Scripting

awk script modification - treat certain files differently

awk 'BEGIN{OFS=","} FNR == 1 {if (NR > 1) {print fn,fnr,nl} fn=FILENAME; fnr = 1; nl = 0} {fnr = FNR} /UNUSUAL/ && /\.gz/ ~ /FILENAME/ {nl++} <'{system ("gunzip -cd FILENAME")}' END ... (2 Replies)
Discussion started by: SkySmart
2 Replies

8. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

9. Shell Programming and Scripting

Why awk perform differently when using variable?

Hi Gurus, I hit a hard block in my script. when using awk command with variable, I got different result. Please see below: my test file as below: $ cat demofile.txt filename-yyyy-abcd filename-xxx-week-pass filename-xxx-week-run for testing purpose, I put 3 awk command in one script.... (7 Replies)
Discussion started by: ken6503
7 Replies

10. Shell Programming and Scripting

AIX to RHEL migration - awk treating 0e[0-9]+ as 0 instead of string issue

Greetings Experts, We are migrating from AIX to RHEL Linux. I have created a script to verify and report the NULLs and SPACEs in the key columns and duplicates on key combination of "|" delimited set of big files. Following is the code that was successfully running in AIX. awk -F "|" 'BEGIN {... (5 Replies)
Discussion started by: chill3chee
5 Replies
FPRINTF(2)							System Calls Manual							FPRINTF(2)

NAME
fprintf, printf, sprintf, vfprintf, vprintf, vsprintf - print formatted output SYNOPSIS
#include <stdio.h> int fprintf(FILE *f, char *format, ...); int printf(char *format, ...); int sprintf(char *s, char *format, ...); int vfprintf(FILE *f, char *format, char *args); int vprintf(char *format, char *args); int vsprintf(char *s, char *format, char *args); DESCRIPTION
Fprintf places output on the named output stream f (see fopen(2)). Printf places output on the standard output stream stdout. Sprintf places output followed by the null character () in consecutive bytes starting at s; it is the user's responsibility to ensure that enough storage is available. Vfprintf, vprintf, and vsprintf are the same, except the args argument is a pointer to an argument in an argument list of the calling function, and the effect is as if the calling function's argument list from that point on is passed to the printf rou- tines. Each function returns the number of characters transmitted (not including the in the case of sprintf), or a negative value if an output error was encountered. These functions convert, format, and print their trailing arguments under control of a format string. The format contains two types of objects: plain characters, which are simply copied to the output stream, and conversion specifications, each of which results in fetching of zero or more arguments. The results are undefined if there are arguments of the wrong type or too few arguments for the format. If the format is exhausted while arguments remain, the excess are ignored. Each conversion specification is introduced by the character %. After the %, the following appear in sequence: Zero or more flags, which modify the meaning of the conversion specification. An optional decimal digit string specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left adjustment, described later, has been given) to the field width. An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversions, the number of dig- its to appear after the decimal point for the e, E, and f conversions, the maximum number of significant digits for the g and G con- versions, or the maximum number of characters to be written from a string in s conversion. The precision takes the form of a period (.) followed by an optional decimal integer; if the integer is omitted, it is treated as zero. An optional h specifying that a following d, i, o, u, x or X conversion specifier applies to a short int or unsigned short argument (the argument will have been promoted according to the integral promotions, and its value shall be converted to short or unsigned short before printing); an optional h specifying that a following n conversion specifier applies to a pointer to a short argument; an optional l (ell) specifying that a following d, i, o, u, x, or X conversion character applies to a long or unsigned long argu- ment; an optional l specifying that a following n conversion specifier applies to a pointer to a long int argument; or an optional L specifying that a following e, E, f, g, or G conversion specifier applies to a long double argument. If an h, l, or L appears with any other conversion specifier, the behavior is undefined. A character that indicates the type of conversion to be applied. A field width or precision, or both, may be indicated by an asterisk (*) instead of a digit string. In this case, an int arg supplies the field width or precision. The arguments specifying field width or precision, or both, shall appear (in that order) before the argument (if any) to be converted. A negative field width argument is taken as a - flag followed by a positive field width. A negative precision is taken as if it were missing. The flag characters and their meanings are: - The result of the conversion is left-justified within the field. + The result of a signed conversion always begins with a sign (+ or -). blank If the first character of a signed conversion is not a sign, or a signed conversion results in no characters, a blank is prefixed to the result. This implies that if the blank and + flags both appear, the blank flag is ignored. # The result is to be converted to an ``alternate form.'' For o conversion, it increases the precision to force the first digit of the result to be a zero. For x or X conversion, a non-zero result has 0x or 0X prefixed to it. For e, E, f, g, and G conver- sions, the result always contains a decimal point, even if no digits follow the point (normally, a decimal point appears in the result of these conversions only if a digit follows it). For g and G conversions, trailing zeros are not be removed from the result as they normally are. For other conversions, the behavior is undefined. 0 For d, i, o, u, x, X, e, E, f, g, and G conversions, leading zeros (following any indication of sign or base) are used to pad the field width; no space padding is performed. If the 0 and - flags both appear, the 0 flag will be ignored. For d, i, o, u, x, and X conversions, if a precision is specified, the 0 flag will be ignored. For other conversions, the behavior is undefined. The conversion characters and their meanings are: d,o,u,x,X The integer arg is converted to signed decimal (d or i), unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal nota- tion (x or X); the letters abcdef are used for x conversion and the letters ABCDEF for X conversion. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is no characters. f The double argument is converted to decimal notation in the style [-]ddd.ddd, where the number of digits after the decimal point is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is explicitly no deci- mal point appears. e,E The double argument is converted in the style [-]d.ddde+-dd, where there is one digit before the decimal point and the number of digits after it is equal to the precision; when the precision is missing, it is taken as 6; if the precision is zero, no decimal point appears. The E format code produces a number with E instead of e introducing the exponent. The exponent always contains at least two digits. g,G The double argument is printed in style f or e (or in style E in the case of a G conversion specifier), with the precision speci- fying the number of significant digits. If an explicit precision is zero, it is taken as 1. The style used depends on the value converted: style e is used only if the exponent resulting from the conversion is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional portion of the result; a decimal point appears only if it is followed by a digit. c The int argument is converted to an unsigned char, and the resulting character is written. s The argument is taken to be a string (character pointer) and characters from the string are printed until a null character () is encountered or the number of characters indicated by the precision specification is reached. If the precision is missing, it is taken to be infinite, so all characters up to the first null character are printed. A zero value for the argument yields undefined results. P The void * argument is printed in an implementation defined way (for Plan 9: the address as hexadecimal number). n The argument shall be a pointer to an integer into which is written the number of characters written to the output stream so far by this call to fprintf. No argument is converted. % Print a %; no argument is converted. If a conversion specification is invalid, the behavior is undefined. If any argument is, or points to, a union or an aggregate (except for an array of character type using %s conversion, or a pointer cast to be a pointer to void using %P conversion), the behavior is undefined. In no case does a nonexistent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result. SOURCE
/sys/src/libstdio SEE ALSO
fopen(2), fscanf(2), print(2) BUGS
There is no way to print a wide character (rune); use print(2) or bio(2). FPRINTF(2)
All times are GMT -4. The time now is 11:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy