# How to copy only certain counts for genes in tsv file to new file in linux

Hi there I have generated a counts table of samples I need to compare by differential expression analysis.

The layout of the counts table is as follows:

Gene_id Sample_A_r1 Sample_A_r2 Sample_B_r1 Sample_B_r2 Sample_AB_r1 Sample_AB_r2
KLTH001 0 0 2 5 8 3
KLTH002 0 0 5 3 9 7
YL99812 6 4 0 0 12 14
YL99813 9 7 0 0 15 19

So here, for the differential expression analysis, I only want to compare for example the genes starting with "YL-". So in a new file I want the rows including the first row which is "Gene_id" and the sample names e.g. "Sample_A_r1" and then only the rows with genes starting with "YL-" in their names. This will allow me to only compare the changes between Sample_A alone versus when it was combined with Sample_B, or in the table Sample_AB.

If anyone can please give me an indication of how to do this in linux I would really appreciate it. I have seen other similar posts, involving grep and awk I am just not sure of exactly how to do this. I am still quite new to linux. Any help will be appreciated.

• Would something like this work? awk ' $1 YL* ' counts_table.tsv > edited_counts_table.tsv Sep 25 '21 at 17:00 • Would this preserve the header/first line? – haci Sep 25 '21 at 17:28 ## 3 Answers Something like this should do job: # creating a new file with the first line of the original head -1 foo.txt > bar.txt # ">>" appends to the file on the right hand side # "^" dictates that the search term should be at the beginning of a line grep "^YL" foo.txt >> bar.txt  • Thanks very much, will give it a try now Sep 26 '21 at 9:26 One way using : awk 'NR==1 || /^YL/ { print$0 }' file.tsv


The default action is to print the line/record, so all you need is the condition:

awk 'NR==1 || /^YL/' file.tsv

• Thanks I will try this out, it seems the previous command didn't copy all of the occurrences of "YL-" across. Could you maybe suggest how I could just remove any gene names starting with "KLTH-" from my original counts file as well? I would just like to be able to check that I got all records from the original file in the separated files I create. Sep 26 '21 at 10:49
• @Justin1609 You can just negate the pattern match expression: awk 'NR==1 || !/^KLTH/' file. This will just print the first line, or lines that don't start with KLTH. Sep 26 '21 at 11:02
• okay awesome thanks @Steve I appreciate it, will give it a try Sep 26 '21 at 14:37

Alternatively, you can use grep like so to remove all lines starting with KLTH:

grep -v '^KLTH' in_file > out_file


For example:

echo "Gene_id\nKLTH001\nYL99812" | grep -v '^KLTH'


Prints:

Gene_id
YL99812


Here, GNU grep uses option:
-v : Print lines that do not match.

In the regular expression (regex):
^ : Match the beginning of the line.