如何用sed替换任何不带空格的字母?

时间:2012-05-11 23:01:51

标签: bash 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。

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

4 个答案:

答案 0 :(得分:3)

如果你想支持unicode字母(as mentioned in your question),那么这个perl命令可以替换所有unicode non-letters

echo $line | perl -pe 's/[^\p{L}\s]+/ /g;'

参考:http://www.regular-expressions.info/unicode.html

答案 1 :(得分: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 

答案 2 :(得分: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

答案 3 :(得分:1)

尝试:

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