0

I have a text file as follows:

orthogroup1
orthogroup4
...

And I have a directory with text files as follows:

orthogroup1.faa.reconciled
orthogroup2.faa.reconciled
orthogroup3.faa.reconciled
orthogroup4.faa.reconciled
...

I want to use the text file to get the corresponding filenames in my directory.

The result should be something like:

orthogroup1.faa.reconciled
orthogroup4.faa.reconciled

How can I do this?

Thanks in advance :)

1

3 Answers 3

1

The -f option of grep allows you to take patterns from a file. Use that on the output of find to filter out the list:

find /path/to/dir -type f | grep -f /path/to/patterns
5
  • Ah dang ok, I thought that's what you wanted. Commented Nov 6, 2019 at 16:32
  • Well, if you have control over the list of files to match, you could edit (or generate) it so that each line ends in \., like orthogroup1\. and so on. That will match up to the first dot separator. Commented Nov 6, 2019 at 16:34
  • do you know how can I now copy the output of the command above to a new/different directory? thank you very much :)
    – Zez
    Commented Nov 6, 2019 at 16:35
  • If you mean the textual list, just use the redirect operator: find /path/to/dir -type f | grep -f /path/to/patterns > /path/to/list/of/matches Commented Nov 6, 2019 at 16:39
  • I was aiming to move the actual files, would that be possible?
    – Zez
    Commented Nov 6, 2019 at 16:47
0

Can you define what you mean by "get the filenames in my directory"? What next step do you have in mind for them?

Seems like you probably want to use something like sed or awk, although the latter is probably overkill. See What is the difference between sed and awk? for more information.

0

Something like this:

suffix='.faa.reconciled'
for line in $(cat file.txt); do
    echo $line$suffix
done

Example:

$ cat file.txt
orthogroup1
orthogroup4

$ ls -l ortho*
orthogroup4.faa.reconciled
orthogroup3.faa.reconciled
orthogroup2.faa.reconciled
orthogroup1.faa.reconciled

$ suffix='.faa.reconciled'; for line in $(cat file.txt); do echo $line$suffix; done
orthogroup1.faa.reconciled
orthogroup4.faa.reconciled

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