我需要从文件中删除不是字母、小写或大写的所有内容,并将其替换为空格,例如:
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。
如何用空格替换任何非字母?
如果你想支持 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." | sed "s/[^a-zA-Z]/ /g"
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.' |
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
尝试:
sed 's/[^A-Za-z]/ /g;' myfile.txt