Xubuntu [SOLVED]: Print file data content column wise

Xubuntu [SOLVED]: Print file data content column wise

Home Forums Xubuntu Xubuntu [SOLVED]: Print file data content column wise

Tagged: , ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36863

    Anonymous

    QuestionQuestion

    I have a tab separated file file.txt whose contents are as follows :

    word11 word12 word13 word14
    word21 word22 word23 word24
    word31 word32 word33 word34
    word41 word42 word43 word44
    

    I want to copy each column in a different file. For example:
    First file will have contain :

    word11
    word21
    word31
    word41
    

    Second file will contain:

    word12
    word22
    word32
    word42
    

    I wrote the script for the same (There are 12 columns):

    for i in {1..12}
    do
            awk -F "t" '{print $i}' file.txt > /tmp/output-$i.txt
    done
    

    But all output files contain all the data:

    word11 word12 word13 word14
    word21 word22 word23 word24
    word31 word32 word33 word34
    word41 word42 word43 word44
    

    Thanks for the help.

    #36864

    Anonymous

    Accepted AnswerAnswer

    You are trying to use shell variables in Awk which would never work. But what you are trying to do can be done totally in Awk itself. The for loop upto NF will parse all your columns, you don’t need to hard-code it anymore.

    awk -v FS="t" '{for (i=1;i<=NF;i++) print $i > ("/tmp/output-"i);}'  file.txt
    

    Source: https://stackoverflow.com/questions/48007002/print-file-data-content-column-wise
    Author: Inian
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.