Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Marius
    8armed
    • Dec 2010
    • 30

    #1

    Remove a part of a filename in a Bash loop

    I have many files named like this:

    lib01.GFBAG_UHAU.fastq.sam.bam
    lib02.ABABAB_ZU.fastq.sam.bam
    lib03.ZGAZG_IAUDH.fastq.sam.bam

    Many parts of the filenames are thus variable in length, although they are connected through the same type of punctuation (. or _).
    What I want to achieve is to remove the part .fastq.sam.bam from a filename when I loop trough these files in BASH. How do I achieve this in Bash?
  • SNPsaurus
    Registered Vendor
    • May 2013
    • 525

    #2
    You want to split the string on a "." delimiter and then keep the first two parts. Or use ".fastq.sam.bam" as a delimiter, I suppose!

    To split string in Bash scripting with single character or set of single character delimiters, set IFS(Internal Field Separator) to the delimiter(s) and parse the string to array. To split string in Bash with multiple character delimiter use Parameter Expansions. Examples have been provided for Bash Split String operation.
    Providing nextRAD genotyping and PacBio sequencing services. http://snpsaurus.com

    Comment

    • ungsik
      Junior Member
      • Mar 2013
      • 1

      #3
      Refer to https://unix.stackexchange.com/quest...ck-of-variable

      An example for changing extension from fastq.sam.bam to txt.

      for file in *.fastq.sam.bam
      do
      mv ${file%.fastq.sam.bam} ${file%.fastq.sam.bam}.txt
      done

      Comment

      • pmiguel
        Senior Member
        • Aug 2008
        • 2328

        #4
        Originally posted by ungsik View Post
        Refer to https://unix.stackexchange.com/quest...ck-of-variable

        An example for changing extension from fastq.sam.bam to txt.

        for file in *.fastq.sam.bam
        do
        mv ${file%.fastq.sam.bam} ${file%.fastq.sam.bam}.txt
        done
        Don't you mean:

        for file in *.fastq.sam.bam
        do
        mv $file ${file%.fastq.sam.bam}.txt
        done

        --
        Phillip

        Comment

        • pmiguel
          Senior Member
          • Aug 2008
          • 2328

          #5
          Originally posted by Marius View Post
          I have many files named like this:

          lib01.GFBAG_UHAU.fastq.sam.bam
          lib02.ABABAB_ZU.fastq.sam.bam
          lib03.ZGAZG_IAUDH.fastq.sam.bam

          Many parts of the filenames are thus variable in length, although they are connected through the same type of punctuation (. or _).
          What I want to achieve is to remove the part .fastq.sam.bam from a filename when I loop trough these files in BASH. How do I achieve this in Bash?
          Using BASH parameter expansion:

          Code:
          for i in *.fastq.sam.bam; do mv $i ${i%.fastq.sam.bam}; done;
          Which is pretty fun, the "%" more-or-less meaning "clip what follows from the the very end of the value stored in variable $i." "#" does the analogous thing, but clips from the very front.

          But "%%" does a "greedy" removal of whatever follows it. So:

          Code:
          i=lib01.GFBAG_UHAU.fastq.sam.bam.fastq.sam.bam.fastq.sam.bam
          echo ${i%.fastq.sam.bam*}
          will produce:
          Code:
          lib01.GFBAG_UHAU.fastq.sam.bam.fastq.sam.bam
          whereas:

          Code:
          i=lib01.GFBAG_UHAU.fastq.sam.bam.fastq.sam.bam.fastq.sam.bam
          echo ${i%%.fastq.sam.bam*}
          will produce:
          Code:
          lib01.GFBAG_UHAU
          If you can run Perl, then finding the "rename.pl" script might be less arcane than deploying you BASH powers.

          rename.pl 's/.fastq.sam.bam$//' *.fastq.sam.bam

          Find rename.pl here:


          --
          Phillip

          Comment

          • pmiguel
            Senior Member
            • Aug 2008
            • 2328

            #6
            Originally posted by Marius View Post
            I have many files named like this:

            lib01.GFBAG_UHAU.fastq.sam.bam
            lib02.ABABAB_ZU.fastq.sam.bam
            lib03.ZGAZG_IAUDH.fastq.sam.bam

            Many parts of the filenames are thus variable in length, although they are connected through the same type of punctuation (. or _).
            What I want to achieve is to remove the part .fastq.sam.bam from a filename when I loop trough these files in BASH. How do I achieve this in Bash?
            Using BASH parameter expansion:

            Code:
            for i in *.fastq.sam.bam; do mv $i ${i%.fastq.sam.bam}; done;
            Which is pretty fun, the "%" more-or-less meaning "clip what follows from the the very end of the value stored in variable $i." "#" does the analogous thing, but clips from the very front.

            But "%%" does a "greedy" removal of whatever follows it. So:

            Code:
            i=lib01.GFBAG_UHAU.fastq.sam.bam.fastq.sam.bam.fastq.sam.bam
            echo ${i%.fastq.sam.bam*}
            will produce:
            Code:
            lib01.GFBAG_UHAU.fastq.sam.bam.fastq.sam.bam
            whereas:

            Code:
            i=lib01.GFBAG_UHAU.fastq.sam.bam.fastq.sam.bam.fastq.sam.bam
            echo ${i%%.fastq.sam.bam*}
            will produce:
            Code:
            lib01.GFBAG_UHAU
            If you can run Perl, then finding the "rename.pl" script might be less arcane than deploying you BASH powers.

            rename.pl 's/.fastq.sam.bam$//' *.fastq.sam.bam

            Find rename.pl here:


            --
            Phillip

            Comment

            • malcook
              Member
              • Sep 2009
              • 24

              #7
              A range of options exists for munging the pathnames

              The approach I would use might well depend on what else I going to do in the loop.

              FWIW:

              [basename](https://linux.die.net/man/1/basename) can be used to remove a suffix of a filename.

              [Shell Parameter Expansion](https://www.gnu.org/software/bash/ma...Expansion.html) can be used to strip or replace either suffixes or prefixes of pathnames stored in variables.

              [GNU parallel](https://www.gnu.org/software/parallel/) can be used in effect to replace your bash looping construct, and has simple syntax for to refer to the basename of a file or directory, including `{=perl expression=}` to munge the pathname any way you like. It has MANY great features and is well worth exploring and being in your toolbelt.

              [rename](https://www.computerhope.com/unix/rename.htm) is very useful for batch renaming of files using regular expressions (if that is all you need to do).

              Comment

              Latest Articles

              Collapse

              • SEQadmin2
                Beyond CRISPR/Cas9: Understand, Choose, and Use the Right Genome Editing Tool
                by SEQadmin2



                CRISPR/Cas9 sparked the gene editing revolution for both research and therapeutics.1 But this system still showed severe issues that limited its applications. The most prominent were the heavy reliance on PAM sequences, delivery limitations, double-stranded breaks that prompt unintended edits and cell death, and editing inefficiency (both in targeting and in knock-in reliability).

                Despite this, “CRISPR helped turn genome editing from a specialized technique into
                ...
                07-31-2026, 11:01 AM
              • SEQadmin2
                Proteomic Platforms: How to Choose the Right Analytical Strategy to Improve Detection and Clinical Applications
                by SEQadmin2


                Proteomics platforms are evolving rapidly, with advances in mass spectrometry and affinity-based approaches expanding what researchers can detect and at what scale. As the field moves toward deeper proteome coverage and clinical applications, scientists face an increasingly complex landscape of tools. This article will explore how researchers are navigating these choices to find the right platform for their work.

                The systematic characterization of the human proteome has
                ...
                07-20-2026, 11:48 AM
              • SEQadmin2
                Advanced Sequencing Platforms Tackle Neuroscience’s Toughest Genomics Problems
                by SEQadmin2



                Genomics studies in neuroscience face a special challenge due to the brain’s complexity and scarcity of samples. Mapping changes in cell type and state using conventional next-generation sequencing methods remains challenging. Advances in technologies like single-cell sequencing, spatial transcriptomics, and long-read sequencing have opened the door to deeper studies of the brain and diseases like Alzheimer’s, amyotrophic lateral sclerosis (ALS), and schizophrenia.
                ...
                07-09-2026, 11:10 AM

              ad_right_rmr

              Collapse

              News

              Collapse

              Topics Statistics Last Post
              Started by SEQadmin2, 07-31-2026, 02:55 AM
              0 responses
              18 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 07-24-2026, 12:17 PM
              0 responses
              15 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 07-23-2026, 11:41 AM
              0 responses
              14 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 07-20-2026, 11:10 AM
              0 responses
              25 views
              0 reactions
              Last Post SEQadmin2  
              Working...