1

I have a directory with a bunch of files

For example:

ExperimentA_Rep1.bed  
ExperimentA_Rep2.bed
ExperimentA_Rep3.bed 
ExperimentB_Rep1.bed 
ExperimentD_Rep1.bed 
ExperimentC_Rep1.bed
ExperimentC_Rep2.bed
 . . . 
ExperimentZ_Rep5.bed

I need to scan through the filenames in a directory and concatenate the files that are from the same experiment, but different replicates, into a new file.

I.e, cat ExperimentA_Rep1.bed ExperimentA_Rep2.bed > ExperimentA_merged.bed

But I can't hardcode this, it needs to be applicable for any dataset of experiments.

If I ran a python script that would be okay.

0

The following assumes that the Rep portion of the filename is numbered sequentially from 1 (i.e. there won't be a Rep2 or Rep3 unless there is also a Rep1). It uses the presence of a "Rep1" file to decide whether or not to run the cat for a particular series of Experiment* files...this is only necessary if there may be some files missing in the sequence ExperimentA* .. ExperimentZ*. If that's not possible then just delete [ -e Experiment${f}_Rep1.bed ] && from the cat line.

for f in {A..Z} ; do
    [ -e Experiment${f}_Rep1.bed ] && cat Experiment${f}_Rep*.bed > Experiment${f}_merged.bed
done

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.