Awk literal increment

In a script like this, to split a large file into a line:

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

This creates out_1.txt, out_2.txt, etc.

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

thank

+5
source share
2 answers

Its not very straightforward, so let me use some arithmetic modulo as follows:

awk '/MYSTRING/ {n++} {p=97+int(n/26); q=(n%26)+97; s=sprintf("out_%c%c.txt", p, q); print > s}' LARGEFILE
+11
source

This might work for you:

awk  -vv "$(echo {a..z}{a..z})" 'BEGIN{split(v,a);n++} /MYSTRING/ {n++}{print >"out_" a[n] ".txt" }' LARGEFILE
0
source

All Articles