I have a list of files in which I have this information

Chrom   Position        Ref Alt TumorVAF    NormalVAF
chr1    56987           CA  C   0.222222    0
chr1    4133415         AG  A   0.425       0
chr1    4611030         A   AC  0.36        0

This is the name of one of my files


I have another matched file which has too much information

This is the name of these big files


As you are seeing, most of the name of txt files are matched

I want to cut the 4th and 5th columns from my small files and add these columns to the matched big files

Manually this is possible but time consuming for a lot of files

Is these any way to do that by coding?

  • $\begingroup$ Note that you don't need a .txt extension if you're not working on Windows. .vcf is fine, and so is no extension at all. $\endgroup$
    – terdon
    Jan 16 '20 at 17:14

As you are seeing, most of the name of txt files are matched

Our eyes and brains are smarter than computers. We can see patterns that can be difficult to describe. For a computer to be useful in this task, the difference between the two file names must be explicitly defined.

In your example (which seems to be an ANNOVAR-generated output file with a prefix specified during annovar run), the common part seems to be the output of $(basename LP6005334-DNA_H01_vs_LP6005333-DNA_H01.passed.somatic.indels.vcf.parsed.txt .parsed.txt). If this is true for all files, you could automate that using a loop such as:

for f in *.parsed.txt
    other_file=$(basename "$f" .parsed.txt)
    # Rest of your logic goes here

You could then paste the columns from $f to $other_file like so (not tested):

paste "$other_file" <(cut -f3,4 $f)

This is an outline to get to your solution. You should be able to build off this.

  • $\begingroup$ @terdon I'd appreciate it if you added a comment suggesting the changes instead of editing the post directly. $\endgroup$
    – Ram RS
    Jan 17 '20 at 18:17
  • 1
    $\begingroup$ Oh. Sorry. I'm happy to do that if you prefer, I'm just very used to editing directly since that is the norm on all SE sites I participate in. But if you object I won't do it again. For what it's worth, if you see something you can improve in any of my answers, please go ahead and edit. I much prefer it :) However, others will likely also edit, it really is the expected behavior on SE sites. $\endgroup$
    – terdon
    Jan 17 '20 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.