bash - awk- print a column in different rows -
I have this simplified table of numbers formed by 2 columns and several rows. Here is the point for each column, take the value and sort them into rows so that each row has 4 values. This file.txt:
1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2
And this is the result I want :
1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2
I do the following, I am creating a script for clarity. Wk.
awk -f script.awk file.txt
where script.awk is
{I = 1; I & lt; = NF; I ++) printf "% s" (NR% 4 == 0? RS: FS), $ i;}
but it does not wok I know that the following command is a Works for columns, but I do not know why they do not work for loops in each column.
{printf "% s" (NR% 4 == 0? RS: FS), $ 1;}
If possible, then I I'm new to this language because I want an explanation of sommands being used. Thanks!
I will post a common solution:
awk '{s} For (i = 1; i & lt; = nf; i ++) a [i, nr] = $ i; } For end {} (i = 1; i & lt; = nf; i ++) {for (j = 1; j & lt; = nr; j ++) printf "% s% s", a [ I, j], (j% 4 = = 0 || j == nr? "\ N": ""); }} 'File
This works for the dynamic column in your input file, for example:
kent $ cat f 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 for kent $ awk '{(i = 1; i & lt; = nf ; I ++) A [i, nr] = $ i; } For end {} (i = 1; i & lt; = nf; i ++) {for (j = 1; j & lt; = nr; j ++) printf "% s% s", a [ I, j], (j% 4 = = 0 || j == nr? "\ N": ""); }} 'F1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4
< P> You need to change the 4
to control how many boxes you have in your output, also it is awk -v cols = "$ var" ...
Comments
Post a Comment