2011-07-08 179 views

回答

2

也許這樣匹配的文本替換

for i in /long/path/filename*; do 
    echo ${i%filename*}/other/path/c01/{magic}; 
done 

現在我想獲得{魔法}:

for i in /long/path/filename*; do 
    A=${i/\/long\/path\/filename/} 
    echo ${i%filename*}/other/path/c01/${A} 
done 
+0

看起來好像沒有辦法直接訪問*部分,所以感謝您向我展示使用變量也是可能的。 –

0
for i in /long/path/filename*; 
do echo ${i%filename*}/other/path/c01/${i#/long/path/filename}; 
done 

man bash

${parameter#word} 
    ${parameter##word} 
      The word is expanded to produce a pattern just as in pathname 
      expansion. If the pattern matches the beginning of the 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. 
+1

不應該'/%{i'是'/ $ {i'? –

+0

@阿德里安:謝謝。錯字+1。 –