Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>awk ' { for (i=1; i&lt;=NF; i++) { a[NR,i] = $i } } NF&gt;p { p = NF } END { for(j=1; j&lt;=p; j++) { str=a[1,j] for(i=2; i&lt;=NR; i++){ str=str" "a[i,j]; } print str } }' file </code></pre> <p>output</p> <pre><code>$ more file 0 1 2 3 4 5 6 7 8 9 10 11 $ ./shell.sh 0 3 6 9 1 4 7 10 2 5 8 11 </code></pre> <p>Performance against Perl solution by Jonathan on a 10000 lines file</p> <pre><code>$ head -5 file 1 0 1 2 2 3 4 5 3 6 7 8 4 9 10 11 1 0 1 2 $ wc -l &lt; file 10000 $ time perl test.pl file &gt;/dev/null real 0m0.480s user 0m0.442s sys 0m0.026s $ time awk -f test.awk file &gt;/dev/null real 0m0.382s user 0m0.367s sys 0m0.011s $ time perl test.pl file &gt;/dev/null real 0m0.481s user 0m0.431s sys 0m0.022s $ time awk -f test.awk file &gt;/dev/null real 0m0.390s user 0m0.370s sys 0m0.010s </code></pre> <p>EDIT by Ed Morton (@ghostdog74 feel free to delete if you disapprove).</p> <p>Maybe this version with some more explicit variable names will help answer some of the questions below and generally clarify what the script is doing. It also uses tabs as the separator which the OP had originally asked for so it'd handle empty fields and it coincidentally pretties-up the output a bit for this particular case.</p> <pre><code>$ cat tst.awk BEGIN { FS=OFS="\t" } { for (rowNr=1;rowNr&lt;=NF;rowNr++) { cell[rowNr,NR] = $rowNr } maxRows = (NF &gt; maxRows ? NF : maxRows) maxCols = NR } END { for (rowNr=1;rowNr&lt;=maxRows;rowNr++) { for (colNr=1;colNr&lt;=maxCols;colNr++) { printf "%s%s", cell[rowNr,colNr], (colNr &lt; maxCols ? OFS : ORS) } } } $ awk -f tst.awk file X row1 row2 row3 row4 column1 0 3 6 9 column2 1 4 7 10 column3 2 5 8 11 </code></pre> <p>The above solutions will work in any awk (except old, broken awk of course - there YMMV).</p> <p>The above solutions do read the whole file into memory though - if the input files are too large for that then you can do this:</p> <pre><code>$ cat tst.awk BEGIN { FS=OFS="\t" } { printf "%s%s", (FNR&gt;1 ? OFS : ""), $ARGIND } ENDFILE { print "" if (ARGIND &lt; NF) { ARGV[ARGC] = FILENAME ARGC++ } } $ awk -f tst.awk file X row1 row2 row3 row4 column1 0 3 6 9 column2 1 4 7 10 column3 2 5 8 11 </code></pre> <p>which uses almost no memory but reads the input file once per number of fields on a line so it will be much slower than the version that reads the whole file into memory. It also assumes the number of fields is the same on each line and it uses GNU awk for <code>ENDFILE</code> and <code>ARGIND</code> but any awk can do the same with tests on <code>FNR==1</code> and <code>END</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload