这是一个 BASH 示例,说明如果文件中有很多句子,这将是最常见的情况,对吧?
SCRIPT(删除两个字母或更短的单词)
#!/bin/bash
while read line
do
echo "$line" | sed -E 's/\b\w{1,2}\b//g'
done < <( cat sentences.txt )
输入
$ cat sentences.txt
Edgar Allan Poe (January 19, 1809 to October 7, 1849) was an
American writer, poet, critic and editor best known for evocative
short stories and poems that captured the imagination and interest
of readers around the world. His imaginative storytelling and tales
of mystery and horror gave birth to the modern detective story.
Many of Poe’s works, including “The Tell-Tale Heart” and
“The Fall of the House of Usher,” became literary classics. Some
aspects of Poe’s life, like his literature, is shrouded in mystery,
and the lines between fact and fiction have been blurred substantially
since his death.
输出
$ ./grep_tests.sh
Edgar Allan Poe (January , 1809 October , 1849) was
American writer, poet, critic and editor best known for evocative
short stories and poems that captured the imagination and interest
readers around the world. His imaginative storytelling and tales
mystery and horror gave birth the modern detective story.
Many Poe’ works, including “The Tell-Tale Heart” and
“The Fall the House Usher,” became literary classics. Some
aspects Poe’ life, like his literature, shrouded mystery,
and the lines between fact and fiction have been blurred substantially
since his death.