4

I've got a list in a text file similar in structure to the following:

symlink("toolbox", "/system/bin/schedtop");
symlink("toolbox", "/system/bin/sendevent");
symlink("toolbox", "/system/bin/setconsole");
symlink("toolbox", "/system/bin/setenforce");
symlink("toolbox", "/system/bin/setprop");
symlink("toolbox", "/system/bin/setsebool");
symlink("mksh", "/system/bin/sh");

What I want to do is change them, using a Bash script, to be like the following:

symlink("toolbox", "/system/bin/schedtop", "/system/bin/sendevent", "/system/bin/setconsole", "/system/bin/setenforce", "/system/bin/setprop", "/system/bin/setsebool")
symlink("mksh", "/system/bin/sh");

This means grouping the lines and merging them based on the original file, in this case, toolbox.

Currently I'm just sorting the file by name, but that's not satisfactory, of course! Can anyone point me in the right direction?

5

awk is well suited for such grouping tasks, e.g.:

$ awk -F'"' ' { a[$2]=a[$2] ", \""$4"\"" } 
        END { for(i in a) printf("symlink(\"%s\"%s);\n", i, a[i]); }' input

The output for your example:

symlink("toolbox", "/system/bin/schedtop", "/system/bin/sendevent",
                   "/system/bin/setconsole", "/system/bin/setenforce", 
                   "/system/bin/setprop", "/system/bin/setsebool");
symlink("mksh", "/system/bin/sh");

(line breaks added for better layout)

Where -F'"' instructs awk to interpret the double quote character as field delimiter. $2 etc. reference the 2nd etc. field, for each line an entry is added/modified to the associative array a. The END action iterates over each key of the associative array and pretty prints the groups.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.