1

I have to remove the extension from a filename, and I was using this:

preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename)

I want to know what is the sed equivalent of this. My current approach is this:

$(echo $filename | cut -f 1 -d '.')

but it does not work all the time.

and

Do regex and sed have same expressions for matching and the syntax is different or the expressions also change?

anshaj
  • 293
  • 1
  • 6
  • 24
  • For regex in bash with sed http://stackoverflow.com/questions/14072592/replace-strings-using-sed-and-regex#14074855 – JustOnUnderMillions Apr 05 '17 at 11:49
  • Extract filename & extention http://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash#965072 – JustOnUnderMillions Apr 05 '17 at 11:51
  • Possible duplicate of [How can I change the extension name in a string with bash?](http://stackoverflow.com/questions/4411080/how-can-i-change-the-extension-name-in-a-string-with-bash) – l'L'l Apr 05 '17 at 12:03

1 Answers1

2

Here are some examples of cutting text with bash...

FileName="/var/www/html/index.html"
echo "${FileName}"
/var/www/html/index.html

echo "${FileName%/*}"
/var/www/html

echo "${FileName##*/}"
index.html

TmpVal=$(echo "${FileName%.*}")
echo "${TmpVal##*/}"
index

Description...

  • ${variable%pattern} removes first pattern on the right
  • ${variable%%pattern} removes last pattern on the right
  • ${variable#pattern} removes first pattern on the left
  • ${variable##pattern} removes last pattern on the left
Mario
  • 679
  • 6
  • 10