如何使用AWK旋转数据

时间:2018-01-05 18:30:03

标签: shell awk pivot transpose

自:

DT  X   Y   Z
10  75  0   3
20  100 1   6
30  125 2   9

要:

DT  ID  VALUE
10  X   75
20  Y   0
30  Z   3
10  X   100
20  Y   1
30  Z   6
10  X   125
20  Y   2
30  Z   9

2 个答案:

答案 0 :(得分:0)

你可以试试这个awk(MAWK Version 1.2)

您的数据可以是5x5或更多

<div id="container">
  <ul class="tree">
    <li>
      <div>OFFICES</div>
      <ul>
        <li>
          <ul class="tree">
            <li>
              <div>Center</div>
              <ul>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 1<br> Person Name 1
                        <br> Degree 1
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 2<br> Person Name 2<br> Degree 1
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 3<br> Person Name 3<br> Degree 1
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 5<br>
                        <br>
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 6<br> Person Name 6
                        <br> Degree 1
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 7<br> Person Name 7
                        <br> Degree 1
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 8<br>
                        <br>
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="”" width="100"></a>Directorate 9<br> Person Name 9
                        <br> Degree 1
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 10<br> Person Name 10
                        <br> Degree 1
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 11<br>
                        <br>
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 12<br> Person Name 12
                        <br> Degree 1
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 13<br> Person Name 13
                        <br> Degree 1
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 14<br>
                        <br>
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 15<br>
                        <br>
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 16<br> Person Name 16
                        <br> Degree 1
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 17<br>
                        <br>
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 18<br>
                        <br>
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 19<br> Person Name 19
                        <br> Degree 1
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 20<br> Person Name 20
                        <br> Degree 1
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 21<br> Person Name 21<br> Degree 1
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 22<br>
                        <br>
                      </div>
                    </li>
                  </ul>
                </li>
                <li>
                  <ul class="tree">
                    <li>
                      <div>
                        <a id="hypPerson" href="">
                          <img alt="Image" height="100" src="" width="100"></a>Directorate 23<br>
                        <br>
                      </div>
                    </li>
                  </ul>
                </li>
              </ul>
            </li>
          </ul>
</div>

答案 1 :(得分:0)

已完成

#my original dataset is separated by "," and have 280 cols 

        tempfile=dataset.csv;
        col_count=`head -n1 $tempfile | tr -cd "," | wc -c`;
        col_count=`expr $col_count + 1`;


        for i in `seq 4 $col_count`; do 
                                    echo $i;
                        pt="{print \$"$i"}";
                    col_name=`head -n 1 $tempfile | sed s'/ //'g | awk -F"," "$pt"`;
                    awk -F"," -v header="DT,ID,$col_name" -f st.awk $tempfile | awk 'NR>1 {print substr($0,index($0,$1))",'"$col_name"'"}' | sed 's/ //g' >> New$tempfile;

             done;

  # file st.awk:
# the code below was found on some stackoverflow page, with some minor changes
    BEGIN {
        # Parse headers into an assoc array h
        split(header, a, ",")
        for(i in a) {
            h[a[i]]=2
        }   

    }

    # Find the column numbers in the first line of a file
    FNR==1{
        split("", cols) # This will re-init cols
        for(i=1;i<=NF;i++) {
            if($i in h) {
                cols[i]=1
            }
        }   
        next
    }

    # Print those columns on all other lines
    {
        res = ""
        for(i=1;i<=NF;i++) {
            if(i in cols) {
                s = res ? OFS : ""
                res = res "," $i
            }
        }   
        if (res) {
            print res 
        }   
    }