Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove two substrings from a string

Tags:

string

bash

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!

like image 778
Geparada Avatar asked Jun 15 '12 18:06

Geparada


People also ask

How do I remove multiple substrings from a string in Python?

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.

How do I remove two characters from a string?

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.


2 Answers

You can use extended patterns to do it all in bash.

shopt -s extglob
echo ${i//@(Rd?|.sam)}

Here's the breakdown:

  1. Use // to replace all occurrences of the pattern, not just the first.
  2. @(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.

like image 166
chepner Avatar answered Sep 20 '22 11:09

chepner


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
like image 25
Igor Chubin Avatar answered Sep 21 '22 11:09

Igor Chubin