sed/awk: insert commas every nth character -


contents of files like:

12345678123456781234567812345678 12345678123456781234567812345678 

i convert these rows to:

12345678,12345678,12345678,12345678 12345678,12345678,12345678,12345678 

i use gnu awk that, this:

gawk '{$1=$1}1' fpat='.{8}' ofs=, input.file 

explanation:

the key awk solution usage of fpat , ofs variables. note fpat gawk specific, that's why solution work gnu awk (gawk) only.

fpat - field pattern defines regex how fields should like. in our case field consists of 8 arbitrary characters: .{8}

the ofs variable - output field separator used define comma separator between individual fields in output.

{$1=$1} looks nop operation, triggers awk reassemble current record - using ofs we've defined before. 1 true , make awk printing re-assembled record.


Comments