Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, Say I have a text file with10 columns. I need to reorder them based on a list of column numbers that will reorder them. My problem is this: If ...
  1. #1
    Just Joined!
    Join Date
    Oct 2006
    Posts
    13

    Extracting columns in particular order

    Hi,

    Say I have a text file with10 columns. I need to reorder them based on a list of column numbers that will reorder them.

    My problem is this:

    If I want to cut out 5 columns (columns 1,2,3,9,10) in the order 1,10,2,9,3 then I have tried using:

    Code:
    cut -f1,10,2,9,3 my_file.txt > reordered_file.txt
    But this just extracts the columns in order as if I used:

    Code:
    cut -f1,2,3,9,10 my_file.txt > reordered_file.txt
    How can I cut these columns and place them into the new file in the order I specify? Does anyone know whether I need to use awk to do this and if so could someone help with the syntax, or give a link to help me?

    While this might seem quite trivial, I will actually need to do this for a file containing ~14000 columns with ~12000 columns that I need to extract in a particular order.

    Any help would be greatly appreciated!

    Many thanks,

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    452
    awk '{print $1 $10 $2 $9 $3}' <filename
    the sun is new every day (heraclitus)

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    There is a short perl code that may be of interest:
    Code:
    # The Missing Textutils, Ondrej Bojar, obo@cuni.cz
    # http://www1.cuni.cz/~obo/textutils/recut
    #
    # 'recut' is a simple 'cut' but unlike 'cut' allows for repetitions and
    # reordering of columns:
    #  recut 3-5,1,2
    However, you would need perl, which can be an obstacle for some folks.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...