Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awk-- splitting a string into sub-strings with length n

Tags:

awk

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
like image 412
user3684042 Avatar asked Sep 23 '14 21:09

user3684042


People also ask

How do I split text in awk?

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.

How do I split a string into N?

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!

How do I split a string into substring?

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.


2 Answers

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.

like image 198
Ed Morton Avatar answered Oct 13 '22 09:10

Ed Morton


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 
like image 40
jaypal singh Avatar answered Oct 13 '22 09:10

jaypal singh