1834652 Members
2434 Online
110069 Solutions
New Discussion

Awk question

 
SOLVED
Go to solution
Sachin Patel
Honored Contributor

Awk question

Hi
I have script

test=substr($0,26,2) # result is " 6"
printf("%2s",$test)

How can I replace that leading white space with zero.

Sachin
Is photography a hobby or another way to spend $
2 REPLIES 2
John Poff
Honored Contributor

Re: Awk question

Hello,

From the printf man page:

Print the outputs with their corresponding field widths and precision:

printf "%.6d, %10.6d, %.6f, %.6e, %.6s\n" 123 123 1.23 123.4 MoreThanSix

resulting in the following output

000123, 000123, 1.230000, 1.234000e+02, MoreTh

So, I'd try something like this:

printf("%.2s",$test)


That should print:

06




Robin Wakefield
Honored Contributor
Solution

Re: Awk question

Nearly correct -

echo 1 3456 | awk '{test=substr($0,2,2);printf ("%.2d\n",test)}'

i.e. d NOT s

NB - in awk, variables aren't preceded by a $.

Robin