Search code examples
arraysbashloopspathfilenames

Extract Path Contents / File Name Components (BASH)


I have file names that I'm trying to extract names/words/numbers from for later use.

e.g

/.../Penguins_45_MIA.txt
/.../Sheep_3.5_KIA.txt
/.../Dolphins_19_AWOL.txt

As you can see there are underscore separated components in the file names which are useful to me. I put these in an array ("currentpath") and then into a loop, processing each file one at a time (I think this is a pretty sensible approach?). I have been trying to use the following command to break down the path into its component words and numbers:

partialpath=(${currentpath//_/})

and then selecting a part of the name (now an element) such as the animal using something like this:

${partialpath[-3]}

But this doesn't work - I had been hoping an echo of the above would give me a Dolphins or Penguins etc. When I investigated, echo "${partialpath[@]}" returned:

/.../Dolphins19AWOL.txt

So I assume that the array has only one element? That being, the entire file path all stuck together just without any underscores? Or am I selecting the elements incorrectly?

Also, I have just noticed this last element would still leave the .txt file ending on the last element, which would be suboptimal.

I hope this is clear, I know it's a simple problem but I completely new to writing scripts and I have been scratching my head for the entire day.


Solution

  • ${currentpath//_/} just removed the underscores, it doesn't break the string up. It can be used on arrays, but just does the same string manipulation on each element. Try this:

    $: cat x
    /.../Penguins_45_MIA.txt
    /.../Sheep_3.5_KIA.txt
    /.../Dolphins_19_AWOL.txt
    
    while read line
    do tmp="${line##*/}" # remove ALL path stuff from the line 
       tmp="${tmp%.*}"   # remove JUST LAST piece after a dot
       IFS="_" read -a chunk <<< "$tmp"; # split on underscores
       printf "%10s " "${chunk[@]}"; printf "\n"; 
    done < x
      Penguins         45        MIA
         Sheep        3.5        KIA
      Dolphins         19       AWOL