Search code examples
splitawkincrementprefix

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


Solution

  • 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