作者mystea.bbs@ptt.cc (mystea) 看板: linux
標題Re: [問題] 更改大量檔名有無釜底抽薪的辦法?
時間批踢踢實業 (2008/02/29 Fri 17:50:59)
→ j2sdk:sed
試用sed之後, 已能把字串做一些簡單的替換, 但要完成想做的事情,
還需要把處理過的字串存入環境變數才行.
目前的想法是這樣的:
for name in $(find . -mindepth 2 -type d)
do
cp $name/output ../testresult/(用sed處理過的字串)
done
我的問題是, 不知道(用sed處理過的字串)那邊應該填甚麼.
我知道echo $name|sed 's:\/:-:g'
會顯示我要的結果, 但是卻不能用$name|sed 's:\/:-:g'代替我要的字串.
name2=$name|sed 's:\/:-:g'之後
echo $name2 得到的也是一片空白.
請問有甚麼辦法把sed處理過的字串便成環境變數呢?
※ 引述《mystea (mystea)》之銘言:
: 謝謝您的答覆, 我開始學了一點script, 這篇的內容已經能看懂了.
: 我現在有一個比較複雜的事想做:
: 假使test/底下有
: test/a_a/
: test/b/
: test/a_a/1/
: test/a_a/2/
: test/a_a/3/
: test/b/1/
: test/b/2/
: test/b/3/
: 等等亂七八糟的一堆目錄, 在每一個深度為2的目錄(也就是test/a_a/1 等)裡都有
: 一個檔案叫做output. 但是深度為1的目錄裡, 有的也有output.
: 現在我想要把這種種不同目錄裡的input都複製到同一個目錄testResult/ 底下, 檔名
: 希望是input-a_a-1, input-b-1, ...等(亦即把目錄名裡的/改成-), 這樣的要求, 有
: 辦法用bash script做到嗎?
: 大致上, 我想我需要一個能夠處理字串的工具, 要能夠把有三個"/"的檔名找出來,
: 還要有能夠把/替換成-的工具, 以及把多餘字串丟掉的工具...
: 希望有高手不吝賜教.
: ※ 引述《kenduest.bbs@bbs.sayya.org (小州)》之銘言:
: : for name in $(find test -type f -name input)
: : do
: : mv $name $(dirname $name)/output
: : done
: : for name in $(find test -type d)
: : do
: : cp /path/filename $(name)
: : done
: : 建議學習學寫 shell script 會有幫助
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 76.170.235.113