標準のUnixユーティリティは、「foo.bar.baz」を「foo.bar」+「.baz」に分割しますか?

標準のUnixユーティリティは、「foo.bar.baz」を「foo.bar」+「.baz」に分割しますか?

次のように、$basename2つの部分の出力$stem合計を含む文字列を分割したいと思います。$ext

  1. この文字列は"${stem}${ext}"元の文字列と同じです$basename
  2. または空にすることができ$stemます$ext(文字列によって異なります$basename)。
  3. 空でない場合は、$ext次から始めて.もう含めることはできません。.

このためにシェル関数を書くことは難しくありません。しかし、そうする前にこれを実行できる標準のUnixコマンドがあるかどうかを知りたいです。

編集する:

FWIW、()スクリプトで私がコーディングした方法はzsh次のとおりです。

stem=${basename%.*}
ext=${basename#"$stem"}

編集:タイプミス(${base#...- > ${basename#...)を修正し、Stephane Chazelasの提案(...#$stem- > ...#"$stem")を統合しました。

ベストアンサー1

Bashに含まれる一般的な文字列解析機能を使用すると、必要なものに非常に近い操作を実行できます。

$ F="foo.bar.baz"

$ echo ${F%.*}
foo.bar

$ echo ${F/*./.}
.baz

したがって、次のように整理できます。

$ $ F="foo.bar.baz"

$ stem=${F%.*}
$ ext=${F/*./.}

echo "${stem}${ext}"
foo.bar.baz

Bashのマニュアルページから

${F%.*}
${parameter%word}
${parameter%%word}
   Remove  matching suffix pattern.  The word is expanded to produce a pattern 
   just as in pathname expansion.  If the pattern matches a trailing portion
   of the expanded value of parameter, then the result of  the  expansion  is
   the expanded  value  of  parameter with the shortest matching pattern (the
   ``%'' case) or the longest matching pattern (the ``%%'' case) deleted.  If
   parameter is @ or *, the pattern removal operation is applied  to  each
   positional parameter  in turn, and the expansion is the resultant list.  
   If parameter is an array variable subscripted with @ or *, the pattern
   removal operation is applied to each member of the array in  turn,  and  
   the  expansion  is  the resultant list.
${/*./.}
${parameter/pattern/string}
   Pattern  substitution.   The pattern is expanded to produce a pattern just
   as in pathname expansion.  Parameter is expanded and the longest match of 
   pattern against its value is replaced with string.  If pattern  begins 
   with  /, all  matches  of  pattern are replaced with string.  Normally
   only the first match is replaced.  If pattern begins with #, it must match
   at the beginning of the expanded value of parameter.  If pattern  begins
   with  %,  it  must match  at the end of the expanded value of parameter.
   If string is null, matches of pattern are deleted and the / following
   pattern may be omitted.  If parameter is @ or *, the substitution operation
   is  applied  to  each  positional  parameter in turn, and the expansion 
   is the resultant list.  If parameter is an array variable subscripted
   with @ or *, the substitution operation is applied to each member of the
   array in turn, and the expansion  is  the resultant list.

これらの機能については、以下で読むことができます。バッシュのマニュアルページ

おすすめ記事