awk treating variables differently in UNIX-Linux


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk treating variables differently in UNIX-Linux
# 1  
Old 09-02-2016
awk treating variables differently in UNIX-Linux

Hi, awk seem to be acting differently in Unix and Linux when it comes to formatting. This is making it difficult to migrate scripts.
for example:
UNIX:
Code:
echo "123" |awk '{printf ("%05s\n" ,$1)}'
00123
echo "123" |awk '{printf ("%05d\n" ,$1)}'
00123
echo "S12" |awk '{printf ("%05s\n" ,$1)}'
00S12

in Linux:
Code:
echo "123" |awk '{printf ("%05s\n" ,$1)}'
  123
echo "123" |awk '{printf ("%05d\n" ,$1)}'
00123
echo "S12"|awk '{printf ("%05s\n" ,$1)}'
  S12

Could anyone help me understand why such a difference? And how can I tell awk to treat everything as string and pad with zeros at the beginning in Linux?

Thank you in advance.. Smilie

---------- Post updated at 12:30 PM ---------- Previous update was at 12:20 PM ----------

Just found the awk in Linux is pointing to gawk. Could that be the reason?
# 2  
Old 09-02-2016
Yes... like most gcommands.. gnu version often offers more options and so behaviour may not be like standard UNIX commands, think of gtar etc...
This User Gave Thanks to vbe For This Post:
# 3  
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:
# 4  
Old 09-02-2016
Ok, it is not just awk.

Code:
UNIX>printf "%05s\n" 123
00123

Linux>printf "%05s\n" 123
  123

# 5  
Old 09-02-2016
Quote:
Originally Posted by wanderingmind16
Ok, it is not just awk.

Code:
UNIX>printf "%05s\n" 123
00123

Linux>printf "%05s\n" 123
  123

As I mentioned in post #3 above, this is not surprising. You will probably also find the same difference in the printf family of functions in libc on those systems (for C and C++ programs using printf(), fprintf(), sprintf(), etc.).
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 09-02-2016
Yes, thank you Don Cragun. I posted it about printf before I read your reply.

Is there any way I can make it compatible to the UNIX code with %s? If not, how can I tell printf to format a variable with leading zeros no matter if it is an integer or string.
# 7  
Old 09-02-2016
If you save the following in a file named tester:
Code:
#!/bin/ksh
width=${1:-5}
printf '%s\n' 123 12345 12345678 "" S12 | awk -v w="$width" ' 
{	if((len = length($1)) >= w)
		printf("%s\n", $1)
	else	printf("%0*d%s\n", w - len, 0, $1)
}'

make it executable and invoke it with:
Code:
./tester

it, produces the output:
Code:
00123
12345
12345678
00000
00S12

If you invoke it with:
./tester 10
it produces the output:
Code:
0000000123
0000012345
0012345678
0000000000
0000000S12

and, if you invoke it with:
Code:
./tester 2

it produces the output:
Code:
123
12345
12345678
00
S12

Although this script was written and tested using a Korn shell, it will work with any shell that performs the basic parameter expansions required in the POSIX shell.

Does this help?

PS: As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.

Last edited by Don Cragun; 09-02-2016 at 05:22 AM.. Reason: Add PS.
These 2 Users Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question