問sed的問題 - Linux

By Heather
at 2009-08-10T03:25
at 2009-08-10T03:25
Table of Contents
y※ 引述《allstarschh (allstars)》之銘言:
: 1. 想用sed來改某directory 下的所有檔案
: 不過不確定 find如何跟 sed使用
: 例如 說 dir/下 把每個檔名的最後一個字元刪掉
: 像
: find -type f -exec sed '/(.*).$/\1/g' {} \;
: 之類的
: 但是發現sed好像不認得back reference \1
: 而且也不確定怎麼把輸出拿來改檔名
先回答第一個問題。
第二個問題我有空再補上。
你無法使用:find -type f -exec sed '/(.*).$/\1/g' {} \;
來更改檔名。因為這樣只是更改檔案內的本文內容,而不是改檔名。
: 或是說
: find -type f -exec mv {} `echo sed 's/???/???/g'`
: 也不確定怎麼把find的 {} 帶給 ``裡..
: 用|也是
你要用 find + sed + xargs去更改檔名才可。
例如:把工作目錄下的所有副檔名.foo的檔案 改成.bar
可這樣下指令:
find . -type f -name '*.foo' | sed -e p -e 's/\.foo$/.bar/' | xargs -n2 mv
1 2 3
1 處理完後的結果如下:
./a.foo
2 處理完後的結果如下:
./a.foo
./a.bar
3 處理完後的結果如下:
a.bar
或這樣的指令也可達到相同的功能
find . -type f -name '*.bar' | sed -e 's/.*/mv & &/' -e ' s/bar$/foo/ ' | sh
--
: 1. 想用sed來改某directory 下的所有檔案
: 不過不確定 find如何跟 sed使用
: 例如 說 dir/下 把每個檔名的最後一個字元刪掉
: 像
: find -type f -exec sed '/(.*).$/\1/g' {} \;
: 之類的
: 但是發現sed好像不認得back reference \1
: 而且也不確定怎麼把輸出拿來改檔名
先回答第一個問題。
第二個問題我有空再補上。
你無法使用:find -type f -exec sed '/(.*).$/\1/g' {} \;
來更改檔名。因為這樣只是更改檔案內的本文內容,而不是改檔名。
: 或是說
: find -type f -exec mv {} `echo sed 's/???/???/g'`
: 也不確定怎麼把find的 {} 帶給 ``裡..
: 用|也是
你要用 find + sed + xargs去更改檔名才可。
例如:把工作目錄下的所有副檔名.foo的檔案 改成.bar
可這樣下指令:
find . -type f -name '*.foo' | sed -e p -e 's/\.foo$/.bar/' | xargs -n2 mv
1 2 3
1 處理完後的結果如下:
./a.foo
2 處理完後的結果如下:
./a.foo
./a.bar
3 處理完後的結果如下:
a.bar
或這樣的指令也可達到相同的功能
find . -type f -name '*.bar' | sed -e 's/.*/mv & &/' -e ' s/bar$/foo/ ' | sh
--
Tags:
Linux
All Comments
Related Posts
soft-off vs power-off

By Bennie
at 2009-08-09T20:43
at 2009-08-09T20:43
linux的工具列不見了

By Joseph
at 2009-08-09T20:22
at 2009-08-09T20:22
Moblin v2 beta

By Irma
at 2009-08-09T20:13
at 2009-08-09T20:13
簡體轉繁體工具?

By Doris
at 2009-08-09T19:51
at 2009-08-09T19:51
SAMBA+CUPS+非postscript印表機 (已解決)

By Franklin
at 2009-08-09T18:04
at 2009-08-09T18:04