跳到主要内容

sed

For more details, refer to sed.

s/regexp/replacement/

quote

Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. The replacement might contain the special character & to refer to that portion of the pattern space which matched, and the special escapes \1 through \9 to refer to the corresponding matching sub-expressions in the regexp.

For example, the following sed command removes the ./ prefix from the beginning of a string if it exists:

echo "./test.md" | sed 's~^./~~'
# test.md

In the preceding substitution command s~^./~~:

  • s is the sed command that specifies a string substitution.
  • ~ is the delimiter used to separate the different parts of the command.
  • ^./ is a regular expression that matches any string that starts with ./.
  • The replacement string is an empty string.

When you use find ${DIR_PATH} command to search for files, the result will be prefixed with ${DIR_PATH}. To remove the prefix, you can use the # operator or the sed command. For example:

find ${DIR_PATH} | while IFS= read -r DIR; do
echo "${DIR}" | sed "s~^${DIR_PATH}~~"
done

-E, -r, --regexp-extended

To use extended regular expressions in sed, use the -E option. For example, the following sed command removes the number prefix and the comma from the beginning of a string:

echo "12345678,test.md,..." | sed -E 's~^[0-9]+,~~'
# test.md,...

For more details, refer to --regexp-extended.