I am familiar with function split() which is used in awk programs. I was wondering how I can use it to split a string into smaller strings with length of n? Something like (n is set at 2):
input:
abcdefgh
output:
ab cd ef gh
The awk function split(s,a,sep) splits a string s into an awk array a using the delimiter sep. Variable hms is an array so hms[2] is 34 . The last three statements are equivalent, but the last two more convenient for longer arrays. In the second you can specify the start index and number of elements to print.
To split a string by newline, call the split() method passing it the following regular expression as parameter - /\r?\ n/ . The split method will split the string on each occurrence of a newline character and return an array containing the substrings. Copied!
Use the Split method when the substrings you want are separated by a known delimiting character (or characters). Regular expressions are useful when the string conforms to a fixed pattern. Use the IndexOf and Substring methods in conjunction when you don't want to extract all of the substrings in a string.
Since you accepted a non-split() solution, here's one that's non-gawk-specific, briefer, and probably faster :-):
$ awk '{gsub(/.{2}/,"& ")}1' file
ab cd ef gh
$ awk '{gsub(/.{3}/,"& ")}1' file
abc def gh
Just like the loop-based one you accepted, it adds a trailing blank char when the input string is an exact multiple of the number of chars you want it split on.
Split function in (g)awk
is meant to split a string on delimiters. It populates an array with fields that are delimited by the delimiter. Since your string has none and you basically want to split the string in chunks, you can use GNU awk
feature of split
which also creates an optional array based on separator.
gawk 'BEGIN {
n = split ("abcdefgh", ary, /../, seps);
for (i=1; i<=n; i++)
printf "%s%s", seps[i], (i<n?FS:RS)
}'
ab cd ef gh
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With