Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk opposite of split

Tags:

People also ask

What does split do 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.

Which function in awk is used to divide a string into pieces separated by the field seperator and store the pieces in an array?

Before splitting the string, patsplit() deletes any previously existing elements in the arrays array and seps . Divide string into pieces separated by fieldsep and store the pieces in array and the separator strings in the seps array.


what would be an opposite of split() in awk? Imagine I have array containig characters/integers.

What I've tried:

color = "#FFFF00";
printf("color original: %s\n", color);
split(color, chars, "");
joined = "";
for (i=1; i <= length(chars); i++) {
    joined = joined + chars[i];
}
printf("color joined: %s\n", joined);

however the output is:

color original: #FFFF00
color joined: 0

that is of course incorrect.

UPDATE: cool, ended up with the following code (inspired by join function present in answers):

color = "#FFFF00";
printf("color original: %s\n", color);
split(color, chars, "");
joined = "";
for (i=1; i <= length(chars); i++) {
    joined = joined "" chars[i];
}
printf("color joined: %s\n", joined);

the trick was not to use + sign when joining things back