Answers.Body: <p>This may help:</p>
  <pre><code>cat ecDNA_samples.tsv | xargs -I % grep '^%\s' pcawg.tsv
  </code></pre>
  <p>The command above executes <code>grep '^&lt;line&gt;\s' pcawg.tsv</code>, for each line of ecDNA_samples.tsv. Which matches the lines taken from ecDNA_samples.tsv to the sequences of characters between line start and a white-space in pcawg.tsv.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-10T01:55:32.830Z
Answers.Text: So I ran this code as cat ecDNA_samples.tsv | xargs -I % grep '^%\s' pcawg.tsv>ecDNA_normal.tsv, but when I opened the file ecDNA_normal.tsv in my directory, the file was empty.
Answers.UserDisplayName: Michelle Yang
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-10T16:19:52.367Z
Answers.Text: Use `>>` instead of `>` so an empty result at the end doesn't end up as the result instead of *everything* you've captured.
Answers.UserDisplayName: Ram RS
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-11T03:17:43.007Z
Answers.Text: I see! Thank you!
Answers.UserDisplayName: Michelle Yang
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-13T01:40:20.110Z
Answers.Text: @RamRS It worked.
Answers.UserDisplayName: Michelle Yang
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-09T19:12:31.583Z
Answers.LastActivityDate: 2021-08-09T22:16:07.147Z
Answers.LastEditDate: 2021-08-09T22:16:07.147Z
Answers.UserDisplayName: C. Aknesil
Answers.Body: <p>Just in case an R solution is acceptable as well, this should be fairly robust code because it properly parses the TSV files and matches up column names:</p>
  <pre><code>#!/usr/bin/env Rscript
  
  library(tidyverse)
  read_tsv(&quot;pcawg.tsv&quot;) %&gt;%
     inner_join(read_tsv(&quot;ecDNA_samples.tsv&quot;)) %&gt;%
     write_tsv(&quot;filtered_pcawg.tsv&quot;)
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-09T23:58:22.937Z
Answers.LastActivityDate: 2021-08-09T23:58:22.937Z
Answers.UserDisplayName: gringer
Body: <p>So I have a file, let's call it pcawg.tsv. It's formatted like the one down below.</p>
  <pre><code>Tumor sample ID  Normal sample ID  Mechanism
  a                1                 deletion
  b                2                 ecDNA
  c                3                 ecDNA
  </code></pre>
  <p>I have a file of tumor sample ID's with the ecDNA mechanism called ecDNA_samples.tsv. It's formatted like this:</p>
  <pre><code>Tumor sample ID
  a
  b
  </code></pre>
  <p>To filter and extract the lines of <code>pcawg.tsv</code> that contain the tumor sample ID's in <code>ecDNA_samples.tsv</code> into a new file, I used this line of code below.</p>
  <pre><code>grep -f ecDNA_samples.tsv pcawg.tsv &gt; ecDNA.vcf.tsv
  </code></pre>
  <p>Would this be the right code to use?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-08-09T06:46:29.077Z
Comments.Text: This has been asked on https://unix.stackexchange.com/questions/110645/select-lines-from-text-file-which-have-ids-listed-in-another-file
Comments.UserDisplayName: Throckmorton
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-08-09T23:50:29.570Z
Comments.Text: Is there a particular reason why you want to use `grep` to do this? It would be possible to make a more robust solution using R.
Comments.UserDisplayName: gringer
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-08-10T00:16:05.823Z
Comments.Text: I kind of have to use this with Linux because I'm working with a Linux-based secure computing portal, since we have patient data which is confidential.
Comments.UserDisplayName: Michelle Yang
Tags: <linux><cancer><filtering><awk><grep>
Title: Using grep to get lines from a file (.tsv) that contain specific samples listed in a sample file (.tsv)
ThreadText: # Using grep to get lines from a file (.tsv) that contain specific samples listed in a sample file (.tsv)
  
  (Asked by: username_0 on 2021-08-09)
  
  So I have a file, let's call it pcawg.tsv. It's formatted like the one down below.
  
  ```
  Tumor sample ID  Normal sample ID  Mechanism
  a                1                 deletion
  b                2                 ecDNA
  c                3                 ecDNA
  
  ```
  
  I have a file of tumor sample ID's with the ecDNA mechanism called ecDNA\_samples.tsv. It's formatted like this:
  
  ```
  Tumor sample ID
  a
  b
  
  ```
  
  To filter and extract the lines of `pcawg.tsv` that contain the tumor sample ID's in `ecDNA_samples.tsv` into a new file, I used this line of code below.
  
  ```
  grep -f ecDNA_samples.tsv pcawg.tsv > ecDNA.vcf.tsv
  
  ```
  
  Would this be the right code to use?
  
  **Tags:** linux, cancer, filtering, awk, grep
  
  > **Comments:**
  > **username_1**: This has been asked on https://unix.stackexchange.com/questions/110645/select-lines-from-text-file-which-have-ids-listed-in-another-file  
  > **username_2**: Is there a particular reason why you want to use `grep` to do this? It would be possible to make a more robust solution using R.  
  > **username_0**: I kind of have to use this with Linux because I'm working with a Linux-based secure computing portal, since we have patient data which is confidential.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2021-08-09. Score: 1 | Accepted answer
  
  This may help:
  
  ```
  cat ecDNA_samples.tsv | xargs -I % grep '^%\s' pcawg.tsv
  
  ```
  
  The command above executes `grep '^<line>\s' pcawg.tsv`, for each line of ecDNA\_samples.tsv. Which matches the lines taken from ecDNA\_samples.tsv to the sequences of characters between line start and a white-space in pcawg.tsv.
  
  > **Comments:**
  > **username_0**: So I ran this code as cat ecDNA_samples.tsv | xargs -I % grep '^%\s' pcawg.tsv>ecDNA_normal.tsv, but when I opened the file ecDNA_normal.tsv in my directory, the file was empty.  
  > **username_4**: Use `>>` instead of `>` so an empty result at the end doesn't end up as the result instead of *everything* you've captured.  
  > **username_0**: I see! Thank you!  
  > **username_0**: @username_4 It worked.  
  
  ---
  
  ### Answer by username_2 on 2021-08-09. Score: 0
  
  Just in case an R solution is acceptable as well, this should be fairly robust code because it properly parses the TSV files and matches up column names:
  
  ```
  #!/usr/bin/env Rscript
  
  library(tidyverse)
  read_tsv("pcawg.tsv") %>%
     inner_join(read_tsv("ecDNA_samples.tsv")) %>%
     write_tsv("filtered_pcawg.tsv")
  
  ```
  
  
  ---
  
  