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?

3

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.