Darren
Darren

Reputation: 307

Extracting filenames containing one or more numbers then cat contents to output file

If possible, I'm looking for a bash one liner that concatenates all the files in a folder that are labelled motif<number>.motif into an output.txt file.

I have a few issues I'm struggling with.

A: The <number> contained in the filename can be one or two digits long and I don't know how to use regex (or something similar) to get all the files with either one or two digits. I can get the filenames containing single or double digit numbers separately using:

motif[0-9].motif or motif[0-9][0-9].motif

but can't work out how to get all the files listed together.

B: The second issue I have is I don't know how many files will be in the directory in advance, so I can just use a range of numbers to select the files. This command is in the middle of a long pipeline.

So lets say I have 20 folders:

motif1.motif motif2.motif ... motif19.motif motif20.motif

I'd need to cat >> the contents of all of them into output.txt.

Upvotes: 1

Views: 361

Answers (1)

anubhava
anubhava

Reputation: 785376

You can do:

cat motif{[0-9],[0-9][0-9]}.motif > output

or with extglob:

shopt -s extglob nullglob
cat motif[0-9]?([0-9]).motif > output

Upvotes: 2

Related Questions