I want to insert a comma after the first column and as well as before the last column of all the lines of a file in which each line has a varying number of fields.
E.g. this is file:
test1 test2 test3
test1 test2 test3 test4 test5 test6
test1 test2 test3 test4
test1 test2 test3 test4 test5
I want to make it look like:
test1, test2, test3
test1, test2 test3 test4 test5, test6
test1, test2 test3, test4
test1, test2 test3 test4, test5
I ma trying something like following but it is not working (either syntax error OR no expected output):
awk '{NF=NF-1;for (i=NF; i==NF; i--) $i="," print $NF }' my_file1
You don’t need that
for-loop. You’re just changing two fields.I find newlines (and other whitespace) can make
awk(and other languages) a whole lot more readable: