2017-02-03 14 views
2

私はパラメータ$1を持っています。例えば、文字列operations/software/tools-manifestに設定されていて、それを文字列operations-software-tools-manifestに変換したいのです。 e。すべてのスラッシュ(/)をダッシュ​​(-)に置き換えます。これはbashだけで可能ですか?たとえばsed(1)とは呼びませんか?

私は(unsucessfully)試してみました:

[[email protected] ~]$ testparam=operations/software/tools-manifest 
[[email protected] ~]$ echo "${testparam////-/}" 
operations-/software-/tools-manifest 
[[email protected]assepartout ~]$ echo "${testparam///-/}" 
operations/software/tools-manifest 
[[email protected] ~]$ echo "${testparam//\//-/}" 
operations-/software-/tools-manifest 
[[email protected] ~]$ echo "${testparam//\\//-/}" 
operations/software/tools-manifest 
[[email protected] ~]$ echo "${testparam//[/]/-/}" 
operations/software/tools-manifest 
[[email protected] ~]$ echo "${testparam//\x2f/-/}" 
operations/software/tools-manifest 
[[email protected] ~]$ echo "${testparam//\57/-/}" 
operations/software/tools-manifest 
[[email protected] ~]$ 

答えて

4

あなたはすべての前方を置き換えるために"${testparam//\//-}"を使用することができますが-でスラッシュ:

これを実行する最も簡単な、最も汎用的な方法の
echo "${testparam//\//-}" 
operations-software-tools-manifest 
+1

* argl *なぜ私のテストで '/'の末尾を使用したのですか?ありがとう! –

4

ワンあなたのソースと目的地を変数に入れることです。

in="/" 
out="-" 
testparam=operations/software/tools-manifest 
echo "${testparam//$in/$out}" 

あなたも(これが機能するためにはときin='*'testparam='hello*cruel*world')をグロブ抑制したい場合は、"$in"を引用する必要があります。

余分な引用符なし
in="*" 
out="-" 
testparam='operations*software*tools' 
echo "${testparam//"$in"/$out}" 

、単一*入力文字列全体に一致するように展開されるので、出力には-しか含まれません。

関連する問題