2012-05-11 17 views
1

我需要刪除一切,這是不是一個字母,大寫或小寫,從一個文件中,並用空格代替它,例如:如何用sed替換不帶空格的信件?

The bear ate 3 snakes, then ate 50% of the fish from the river. 

這將成爲:

The bear ate snakes then ate  of the fish from the river 
  • 有時文件包含不尋常的字符它被保存爲UTF-8。

如何用空格替換任何非字母?

回答

2
$ echo "The bear ate 3 snakes, then ate 50% of the fish from the river." | sed "s/[^a-zA-Z]/ /g" 
The bear ate snakes then ate  of the fish from the river 
1

嘗試:

sed 's/[^A-Za-z]/ /g;' myfile.txt 
2

這可能會實現爲你:

echo 'The bear ate 3 snakes, then ate 50% of the fish from the river.' | 
tr -c '[:alpha:]' ' ' 
The bear ate snakes then ate  of the fish from the river 

或:

echo 'The bear ate 3 snakes, then ate 50% of the fish from the river.' | 
sed 's/[^[:alpha:]]/ /g' 
The bear ate snakes then ate  of the fish from the river 
+0

1用於使用'[:阿爾法:]' –