Answers.Body: <p>Something like this should do job:</p>
  <pre><code># creating a new file with the first line of the original    
  head -1 foo.txt &gt; bar.txt
  
  # &quot;&gt;&gt;&quot; appends to the file on the right hand side
  # &quot;^&quot; dictates that the search term should be at the beginning of a line
  grep &quot;^YL&quot; foo.txt &gt;&gt; bar.txt 
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-09-26T09:26:21.733Z
Answers.Text: Thanks very much, will give it a try now
Answers.UserDisplayName: Justin1609
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-09-25T17:42:13.390Z
Answers.LastActivityDate: 2021-09-26T15:52:59.473Z
Answers.LastEditDate: 2021-09-26T15:52:59.473Z
Answers.UserDisplayName: haci
Answers.Body: <p>One way using <a href="/questions/tagged/awk" class="post-tag" title="show questions tagged &#39;awk&#39;" rel="tag">awk</a>:</p>
  <pre><code>awk 'NR==1 || /^YL/ { print $0 }' file.tsv
  </code></pre>
  <p>The default action is to print the line/record, so all you need is the condition:</p>
  <pre><code>awk 'NR==1 || /^YL/' file.tsv
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-09-26T10:49:54.493Z
Answers.Text: 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.
Answers.UserDisplayName: Justin1609
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-09-26T11:02:00.683Z
Answers.Text: @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.
Answers.UserDisplayName: Steve
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-09-26T14:37:58.960Z
Answers.Text: okay awesome thanks @Steve I appreciate it, will give it a try
Answers.UserDisplayName: Justin1609
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-09-26T10:42:19.977Z
Answers.LastActivityDate: 2021-09-26T10:42:19.977Z
Answers.UserDisplayName: Steve
Answers.Body: <p>Alternatively, you can use <code>grep</code> like so to remove all lines starting with <code>KLTH</code>:</p>
  <pre><code>grep -v '^KLTH' in_file &gt; out_file
  </code></pre>
  <p>For example:</p>
  <pre><code>echo &quot;Gene_id\nKLTH001\nYL99812&quot; | grep -v '^KLTH'
  </code></pre>
  <p>Prints:</p>
  <pre><code>Gene_id
  YL99812
  </code></pre>
  <p>Here, GNU <a href="https://www.gnu.org/software/grep/manual/grep.html" rel="nofollow noreferrer"><code>grep</code></a> uses option:<br />
  <code>-v</code> : Print lines that do <strong>not</strong> match.</p>
  <p>In the regular expression (regex):<br />
  <code>^</code> : Match the beginning of the line.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-09-26T21:24:47.470Z
Answers.LastActivityDate: 2021-09-26T21:24:47.470Z
Answers.UserDisplayName: Timur Shtatland
Body: <p>Hi there I have generated a counts table of samples I need to compare by differential expression analysis.</p>
  <p>The layout of the counts table is as follows:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Gene_id</th>
  <th>Sample_A_r1</th>
  <th>Sample_A_r2</th>
  <th>Sample_B_r1</th>
  <th>Sample_B_r2</th>
  <th>Sample_AB_r1</th>
  <th>Sample_AB_r2</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>KLTH001</td>
  <td>0</td>
  <td>0</td>
  <td>2</td>
  <td>5</td>
  <td>8</td>
  <td>3</td>
  </tr>
  <tr>
  <td>KLTH002</td>
  <td>0</td>
  <td>0</td>
  <td>5</td>
  <td>3</td>
  <td>9</td>
  <td>7</td>
  </tr>
  <tr>
  <td>YL99812</td>
  <td>6</td>
  <td>4</td>
  <td>0</td>
  <td>0</td>
  <td>12</td>
  <td>14</td>
  </tr>
  <tr>
  <td>YL99813</td>
  <td>9</td>
  <td>7</td>
  <td>0</td>
  <td>0</td>
  <td>15</td>
  <td>19</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>So here, for the differential expression analysis, I only want to compare for example the genes starting with &quot;YL-&quot;. So in a new file I want the rows including the first row which is &quot;Gene_id&quot; and the sample names e.g. &quot;Sample_A_r1&quot; and then only the rows with genes starting with &quot;YL-&quot; 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.</p>
  <p>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 <code>grep</code> and <code>awk</code> I am just not sure of exactly how to do this. I am still quite new to linux. Any help will be appreciated.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-09-25T17:00:45.227Z
Comments.Text: Would something like this work?
  
  ```awk ' $1 YL* ' counts_table.tsv > edited_counts_table.tsv```
Comments.UserDisplayName: Justin1609
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-09-25T17:28:19.640Z
Comments.Text: Would this preserve the header/first line?
Comments.UserDisplayName: haci
Tags: <linux><filtering><awk><grep><featurecounts>
Title: How to copy only certain counts for genes in tsv file to new file in linux
ThreadText: # How to copy only certain counts for genes in tsv file to new file in linux
  
  (Asked by: username_0 on 2021-09-25)
  
  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.
  
  **Tags:** linux, filtering, awk, grep, featurecounts
  
  > **Comments:**
  > **username_0**: Would something like this work?  ```awk ' $1 YL* ' counts_table.tsv > edited_counts_table.tsv```  
  > **username_1**: Would this preserve the header/first line?  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2021-09-25. Score: 3
  
  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 
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks very much, will give it a try now  
  
  ---
  
  ### Answer by username_2 on 2021-09-26. Score: 1
  
  One way using [awk](/questions/tagged/awk "show questions tagged 'awk'"):
  
  ```
  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
  
  ```
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_2**: @username_0 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.  
  > **username_0**: okay awesome thanks @username_2 I appreciate it, will give it a try  
  
  ---
  
  ### Answer by username_3 on 2021-09-26. Score: 1
  
  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`](https://www.gnu.org/software/grep/manual/grep.html) uses option:  
  `-v` : Print lines that do **not** match.
  
  In the regular expression (regex):  
  `^` : Match the beginning of the line.
  
  
  ---
  
  