Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape a percent sign in AWK printf?

Tags:

printf

awk

I'm making an awk statement that will allow me to print a number of unicode nop's to the screen (in testing, 18 of them). It currently looks like the following:

awk 'BEGIN {while (c++<18) printf "%u9090"}'

When this executes this returns a run time error:

awk: run time error: not enough arguments passed to printf("%u9090")

I realised that I then had to escape my % character since I'm not passing any variables to awk, and it's expecting them. I revised to the following:

awk 'BEGIN {while (c++<18) printf "\%u9090"}'

However I'm still being presented with the same error? The gnu documentation suggests that I should be escaping using \ so I'm a bit amiss at what else to try.

like image 334
Michael A Avatar asked Mar 09 '23 08:03

Michael A


1 Answers

All printfs I know (and in C as per the C Standard) allow you to specify a literal percent with %%.

The GNU docs you reference tell you about how to escape special characters in string literals. However, printf's first arg is interpreted as a format string, so the string literal escape mechanism is the wrong place to look. The proper place to look up is the printf specification (either for awk, or if all else fails, the C language).

like image 76
Jens Avatar answered Apr 06 '23 00:04

Jens