我需要刪除一切,這是不是一個字母,大寫或小寫,從一個文件中,並用空格代替它,例如:如何用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。
如何用空格替換任何非字母?
我需要刪除一切,這是不是一個字母,大寫或小寫,從一個文件中,並用空格代替它,例如:如何用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
如何用空格替換任何非字母?
$ 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
嘗試:
sed 's/[^A-Za-z]/ /g;' myfile.txt
如果你想爲Unicode字母(as mentioned in your question)
支持那麼這個perl的命令,以取代所有unicode non-letters
:
echo $line | perl -pe 's/[^\p{L}\s]+/ /g;'
這可能會實現爲你:
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
1用於使用'[:阿爾法:]' –