I have some CSV / tabular data in a file, for example:
1,7,3,2 8,3,8,0 4,9,5,3 8,5,7,3 5,6,1,9
(They are not always numbers, just random values, separated by commas. Single-digit numbers are easier for example.)
I want to shuffle a random 40% of any of the columns. For example, say the third. Therefore, perhaps 3 and 1 are interchanged with each other. Now the third column:
1 << Came from the last position 8 5 7 3 << Came from the first position
I am trying to do this in place in a file from a bash script that I am working on, and I am not very lucky. I keep wandering around the pretty crazy and barren rabbit holes that leave me for being wrong (constant failure is what pushes me away).
I noted this question with a lot of things, because I'm not quite sure which tool I should use for this.
Edit: I'm probably going to end up accepting Rubens's answer, no matter how silly it is, because it has its own concept of exchange (which, I think, I could emphasize more in the original question), and this allows me to point out percentage of the column to exchange. It also works, which is always a plus.
For someone who doesn't need this and just wants to play a regular game, Jim Harrison's answer also works (I tested it).
A word of warning, however, is about Rubens' decision. I accepted this:
for (i = 1; i <= NF; ++i) { delim = (i != NF) ? "," : ""; ... } printf "\n";
deleted printf "\n"; and moved the newline character as follows:
for (i = 1; i <= NF; ++i) { delim = (i != NF) ? "," : "\n"; ... }
because just having "" in the else case led awk write broken characters at the end of each line ( \00 ). At some point, he even managed to replace my entire file with Chinese characters. Although, frankly, this probably influenced the fact that I was doing something superfluous, stupid, on this problem.