Search code examples
grepcut

How to cut columns from all files in a directory and make new files with the same names?


I have 32 files in one directory.

The format for each file is as follows-

Sample     geneA     geneB

Name        countsA  countsB

I wish to cut the first two columns and paste them into 32 files for geneA, and the first and third column go into files for geneB.

What I tried was cut -f1,2 * > *_geneA.txt, where the * in the output was taken as a character. Is there a way to do it all in one go?

Thank in advance.


Solution

  • This GNU awk program (see @EdMorton's comment in the comments) takes the target file field name from the first record, creates the file based on the filename and the field name and appends fields to it. In the below example I use your sample data twice in files file1and file2:

    $ awk '
    FNR==1 {                                # first record of each file
        for(i=2;i<=NF;i++)                  # iterate field names 
            f[i]=$i                         # and hash them to f
    }
    {                                       # for all records
        for(i=2;i<=NF;i++) {                # iterate all but first field
            file=FILENAME "_" f[i] ".txt"   # form the file name
            print $1,$i > file              # and print to it
        }
    }' file1 file2
    

    Let's see what done:

    $ ls -rt | tail -n 4 
    file1_geneB.txt
    file1_geneA.txt
    file2_geneB.txt
    file2_geneA.txt
    

    Let's see inside of one:

    $ cat file1_geneA.txt
    Sample geneA
    Name countsA