Can I able to give the value(32) in the form of variable in the below awk command instead of hardcoding 32 directly?
df -h | head -1|awk '{printf "%s%32s",$1,$2}'
I tried like awk -v var=32
but its not working.
Can anyone suggest me please.
Can I able to give the value(32) in the form of variable in the below awk command instead of hardcoding 32 directly?
df -h | head -1|awk '{printf "%s%32s",$1,$2}'
I tried like awk -v var=32
but its not working.
Can anyone suggest me please.
awk -v var=32 '{printf "%s%*s\n", $1, var, $2}'
awk -v var=32 '{printf "%s%"var"s", $1,$2}'
Note: The solution of Ed Morton is cleaner as it does not use AWK string concatenations but makes direct use of the printf
formatting
Simultaneously, if you want to get rid of your head
then you can combine everything as:
$ df -h | awk -v var=32 '{printf "%s%"var"s", $1,$2; exit}'
One alternative to the answer given by Ed Morton, could be useful, if you have the number already stored in an environment variable (say: FIELD_WIDTH
). In this case,
df -h | head -1|awk '{printf "%s%" ENVIRON["FIELD_WIDTH"] "s",$1,$2}'
would do the job.
UPDATE:
You should also get rid of the head
:
df -h | awk 'NR==1 {printf "%s%" ENVIRON["FIELD_WIDTH"] "s",$1,$2}'