Announcement

Collapse
No announcement yet.

Remove a part of a filename in a Bash loop

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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?

  • #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!

    https://www.tutorialkart.com/bash-sh...-split-string/
    Providing nextRAD genotyping and PacBio sequencing services. http://snpsaurus.com

    Comment


    • #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


      • #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

        Working...
        X