Sponsored Content
Top Forums Shell Programming and Scripting awk treating variables differently in UNIX-Linux Post 302980773 by Don Cragun on Friday 2nd of September 2016 04:17:58 AM
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:
 

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
All times are GMT -4. The time now is 10:35 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy