8

前:

eng-vshakya:scripts vshakya$ ls
American Samoa.png                  Faroe Islands.png                   Saint Barthelemy.png

後:

eng-vshakya:scripts vshakya$ ls
AmericanSamoa.png                   FaroeIslands.png                    SaintBarthelemy.png

プロトタイプの下で試しましたが、機能しません:(申し訳ありませんが、awk/sedに関してはあまり良くありません:(

ls *.png | sed 's/\ /\\\ /g' | awk '{print("mv "$1" "$1)}'

[上記はプロトタイプであり、実際のコマンドは次のようになります。

ls *.png | sed 's/\ /\\\ /g' | awk '{print("mv "$1" "$1)}' | sed 's/\ //g'

]

4

2 に答える 2

18

これを純粋な bash で実行できる場合は、awk や sed を使用する必要はありません。

[ghoti@pc ~/tmp1]$ ls -l
total 2
-rw-r--r--  1 ghoti  wheel  0 Aug  1 01:19 American Samoa.png
-rw-r--r--  1 ghoti  wheel  0 Aug  1 01:19 Faroe Islands.png
-rw-r--r--  1 ghoti  wheel  0 Aug  1 01:19 Saint Barthelemy.png
[ghoti@pc ~/tmp1]$ for name in *\ *; do mv -v "$name" "${name// /}"; done
American Samoa.png -> AmericanSamoa.png
Faroe Islands.png -> FaroeIslands.png
Saint Barthelemy.png -> SaintBarthelemy.png
[ghoti@pc ~/tmp1]$ 

${foo/ /}表記はbashであり、従来の Bourne シェルでは機能しないことに注意してください。

于 2012-08-01T05:20:53.390 に答える
7

ghitiのソリューションは正しいことです。sedでそれを行う方法を尋ねるので、ここに1つの方法があります:

for file in *; do newfile=$( echo "$file" | tr -d \\n | sed 's/ //g' );
   test "$file" != "$newfile" && mv "$file" "$newfile"; done

tr、ファイル名の改行を削除するためにあり、sed がファイル名全体を 1 行で認識できるようにするために必要です。

于 2012-08-01T12:37:59.427 に答える