Home > Enterprise >  Removing unneeded information in filename using bash script
Removing unneeded information in filename using bash script

Time:12-07

I need to remove everything before and after a name in bash script, the following are examples

test_3123_123_testone-2.cpp
abc_3123_12312_a.cpp
johnchase_4123123123_123123123_johnc-1.cpp

I would need them simply change into

testone.cpp
a.cpp
johnc.cpp

But having trouble with regex and trying to get this setup properly, any advice would be great!

CodePudding user response:

the text right before .cpp but ignoring any dashes(-2) if they exist behind it.

Do exactly that. Write it from the end.

  • "before .cpp" -> so .cpp must be last
  • "ignoring any dashes" - so there is a dash(-2)
    • "if they exist behind it" - the dash is optional
  • "the text" - so match the text.

var=test_3123_123_testone-2.cpp
[[ "$var" =~ ([^_-]*)(-[0-9] )?.cpp$ ]]
echo ${BASH_REMATCH[1]}.cpp

CodePudding user response:

If your filename is in a variable called file, then: echo ${${file##*_}/-[0-9]*[.]/.}.

Breaking that down, inside out:

  • ${file$$*_} remove everything from start of string to last _
  • ${file/-[0-9]*[.]/.} replace "dash number dot" with just a dot

This is not bullet-proof, but it covers the given cases.

$ for file in test_3123_123_testone-2.cpp abc_3123_12312_a.cpp johnchase_4123123123_123123123_johnc-1.cpp
do
  echo ${${file##*_}/-[0-9]*[.]/.}
done
testone.cpp
a.cpp
johnc.cpp
  •  Tags:  
  • bash
  • Related