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
                Multiomic Liquid Biopsy: What DNA/RNA Panels Reveal That DNA Alone Can’t
                by SEQadmin2



                Tumors contain complex mixtures of cell populations that evolve in response to treatment and host-specific factors. Tissue biopsies provide only static snapshots of a tumor’s state. But what if we could follow that evolution over time using a minimally invasive technique?

                Liquid biopsies can detect tumor-derived material circulating in the bloodstream, providing a way to study treatment response, detect emerging resistance, and discover new biomarkers. Circulating
                ...
                10-14-2026, 09:55 AM
              • SEQadmin2
                New Genomics Technologies Take Aim at Long-Standing Limits
                by SEQadmin2


                Researchers using sequencing and genomics tools often have to make trade-offs. They can choose between speed or scale, short reads or long-range information, or targeted panels or a view of the whole transcriptome. New technologies that have been released this year are built to address those tough choices.

                We asked six companies the same four questions to learn about their latest products. The new technologies bring a lot to the table, including rethinking sequencing
                ...
                09-28-2026, 10:25 AM

              ad_right_rmr

              Collapse

              News

              Collapse

              Topics Statistics Last Post
              Started by SEQadmin2, 10-22-2026, 11:13 AM
              0 responses
              25 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 10-22-2026, 10:48 AM
              0 responses
              20 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 10-15-2026, 10:52 AM
              0 responses
              36 views
              0 reactions
              Last Post SEQadmin2  
              Started by SEQadmin2, 10-14-2026, 05:39 AM
              0 responses
              42 views
              0 reactions
              Last Post SEQadmin2  
              Working...