I have a list of files like this:
wgEncodeCaltechRnaSeqGm12878R1x75dFastqRep1.fastq.trim.tags.sam
wgEncodeCaltechRnaSeqGm12878R1x75dFastqRep2.fastq.trim.tags.sam
wgEncodeCshlLongRnaSeqGm12878CellPapFastqRd1Rep1.fastq.trim00.tags.sam
wgEncodeCshlLongRnaSeqGm12878CellPapFastqRd1Rep1.fastq.trim01.tags.sam
wgEncodeCshlLongRnaSeqGm12878CellPapFastqRd1Rep1.fastq.trim02.tags.sam
wgEncodeCshlLongRnaSeqGm12878CellPapFastqRd1Rep2.fastq.trim00.tags.sam
wgEncodeCshlLongRnaSeqGm12878CellPapFastqRd1Rep2.fastq.trim01.tags.sam
wgEncodeCshlLongRnaSeqGm12878CellPapFastqRd1Rep2.fastq.trim02.tags.sam
wgEncodeCshlLongRnaSeqGm12878CellPapFastqRd2Rep1.fastq.trim00.tags.sam
wgEncodeCshlLongRnaSeqGm12878CellPapFastqRd2Rep1.fastq.trim01.tags.sam
I want to remove the Rd1, Rd2 and .sam stings from their file names. With the following bash script, I can remove the Rd1, Rd2 and .sam strings using two commands....
for i in $(ls)
do
echo "${i/Rd?/}"
echo "${i/.sam/}"
done
But I want to know how to do the two substitutions in one step Do you know how to do it?
Thanks for your time!
We will use two functions of the regex module- re. sub() and re. subn() to replace multiple substrings in a string. sub() - It replaces the contents of a string based on patterns.
Use the String. slice() method to remove the last 2 characters from a string, e.g. const removedLast2 = str. slice(0, -2); . The slice method will return a new string that doesn't contain the last 2 characters of the original string.
You can use extended patterns to do it all in bash
.
shopt -s extglob
echo ${i//@(Rd?|.sam)}
Here's the breakdown:
//
to replace all occurrences of the pattern, not just the first.@(Rd?|.sam)
is an extended pattern, which matches either Rd?
or .sam
. The
pipe separates the two sub-patterns.Technically, you'd like to be able to avoid removing ".sam" from the middle of the word, but it looks like this is safe for your use case.
Of course we know!
for i in *
do
echo $i | sed 's/Rd.//;s/\.sam$//'
done
And when you want rename these files:
for i in *
do
mv "$i" "$(echo $i | sed 's/Rd.//;s/\.sam$//')"
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With