1

I have say 100 text files of which 50 of them have file name starting with sshv2l and other fifty without sshv2l. I want to merge them in a loop like this (below) where I have two sets of merged files. For the ones with sshv2l, I did something like below, but could not merge files without sshv2l. How do I write if not condition within for loop in bash?

for f in sshv2l*; do
echo "Merging file :" $f
cat ${f} >> sshv2l_merged.fastq
done

1 Answer 1

4

Your loop code can just be replaced with single cat:

cat sshv2l* >> /path/to/sshv2l_merged.fastq

Now to grab all the files not starting with sshv2l you can use extglob negation glob:

# enable extglob
shopt -s extglob nullglob

cat !(sshv2l*) >> /path/to/not_sshv2l_merged.fastq

# disable extglob
shopt -u extglob nullglob

I have added some imaginary /path/to/ path to make sure you keep *.fastq files outside current directory to avoid them getting concatenated in cat commands.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I have added it in answer.
Thanks! Also shopt was not available readily, so had to do: exec bash then source ~/.bashrc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.