<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>

CC BY-SA 4.0
2021-08-10T01:55:32.830Z
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.
Michelle Yang
CC BY-SA 4.0
2021-08-10T16:19:52.367Z
Use `>>` instead of `>` so an empty result at the end doesn't end up as the result instead of *everything* you've captured.
Ram RS
CC BY-SA 4.0
2021-08-11T03:17:43.007Z
I see! Thank you!
Michelle Yang
CC BY-SA 4.0
2021-08-13T01:40:20.110Z
@RamRS It worked.
Michelle Yang
CC BY-SA 4.0
2021-08-09T19:12:31.583Z
2021-08-09T22:16:07.147Z
2021-08-09T22:16:07.147Z
C. Aknesil
<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>

CC BY-SA 4.0
2021-08-09T23:58:22.937Z
2021-08-09T23:58:22.937Z
gringer
<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>

CC BY-SA 4.0
2021-08-09T06:46:29.077Z
This has been asked on https://unix.stackexchange.com/questions/110645/select-lines-from-text-file-which-have-ids-listed-in-another-file
Throckmorton
CC BY-SA 4.0
2021-08-09T23:50:29.570Z
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.
gringer
CC BY-SA 4.0
2021-08-10T00:16:05.823Z
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.
Michelle Yang
<linux><cancer><filtering><awk><grep>
Using grep to get lines from a file (.tsv) that contain specific samples listed in a sample file (.tsv)
# 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 '^\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**: @RamRS 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")


---

