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

Zez
  • 105
  • 5

3 Answers3

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
Alex Howansky
  • 44,270
  • 7
  • 68
  • 92
  • Ah dang ok, I thought that's what you wanted. – Alex Howansky Nov 06 '19 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. – Alex Howansky Nov 06 '19 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 Nov 06 '19 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` – Alex Howansky Nov 06 '19 at 16:39
  • I was aiming to move the actual files, would that be possible? – Zez Nov 06 '19 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.

zzzbra
  • 23
  • 1
  • 7
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
Max S.
  • 1
  • 1