Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

increment letter variables in awk

In a script like this for splitting a large file by string:

awk  '/MYSTRING/ {n++}{print >"out_" n ".txt" }' LARGEFILE

This produces out_1.txt, out_2.txt, etc.

How can I get letter prefixes as produced by split (out_aa.txt, out_ab.txt, out_ac.txt, ...)?

Thanks

like image 978
philstra Avatar asked Feb 02 '12 22:02

philstra


1 Answers

Its not very straight forward so allow me to use some modulo arithmetic here like this:

awk '/MYSTRING/ {n++} {p=97+int(n/26); q=(n%26)+97; s=sprintf("out_%c%c.txt", p, q); print > s}' LARGEFILE
like image 54
anubhava Avatar answered Nov 11 '22 20:11

anubhava