Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How would I pad this with zeros using awk?

I need i padded to 3 places, e.g. 001. I’m trying printf and sprintf per this suggestion, but no variation I try works.

awk '/BEGIN/{i++}/BEGIN/,/END/{print > "mozcert-" i ".pem"}' ca-certificates.crt

First day with awk and sprintf, unsure of proper syntax. Here are some attempts.

awk '/BEGIN/{i++} /BEGIN/,/END/ {print > "mozcert-" printf "%03d\n",i ".pem"}' ca-certificates.crt

awk '/BEGIN/{i++} /BEGIN/,/END/ {print > "mozcert-}" {printf "%03d\n",i} {".pem"}' ca-certificates.crt

awk '/BEGIN/{i++} /BEGIN/,/END/ {print > "mozcert-" {sprintf(^C03d\n",i} ".pem"}' ca-certificates.crt

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The function to format output is printf (or sprintf if you don’t want to print immediately).

awk '/BEGIN/{filename = sprintf("mozcert-%03i.pem", ++i)}
  /BEGIN/,/END/{print > filename}' ca-certificates.crt
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading