sedで冗長置換を避ける方法は?

sedで冗長置換を避ける方法は?

最初のファイルには以下が含まれます。

#. This is the file name to process: waveheight.txt
#. This is the latest data to process if exists: waveheightNew.txt
 FilNam=Project2128/Input/waveheightNew.txt
 if [[ ! -f ${FilNam} ]]; then FilNam=Project2128/Input/waveheight.txt; fi

2番目のファイルには次のものが含まれます。

#. This is the file name to process: waveheightBin.txt
#. This is the latest data to process if exists: waveheightNewBin.txt
 FilNam=Project2128/Input/waveheightNewBin.txt
 if [[ ! -f ${FilNam} ]]; then FilNam=Project2128/Input/waveheightBin.txt; fi

.txtBin.txt今、?に変更してファイルを処理する必要があります。を使用すると、2番目のファイルがsed "s/.txt/Bin.txt/"生成されます。その時BinBin.txt頃だったらぎこちなく見えたはずです。sed "s/Bin.txt/.txt/"sed "s/.txt/Bin.txt/"

不要な試合はスキップする方が賢明でしょうか?

ベストアンサー1

Binテキストに置き換えたい項目がある場合は、それを含めるとその項目自体が置き換えられます。

sed 's/\(Bin\)\{0,1\}\.txt/Bin.txt/g'

またはsedEREをサポートしている場合-E(または-rいくつかの以前のバージョンのGNUまたはbusybox sed):

sed -E 's/(Bin)?\.txt/Bin.txt/g'

Bewareは、.すべての単一文字に一致する正規表現演算子です。\.テキストを一致させる必要があります。指す

おすすめ記事