[英]Remove Words Shorter Than 4 Characters Using Linux
我已閱讀以下內容並嘗試根據我的需要重新設計命令邏輯。 但是,我只是沒能把它做好。
試過: echo $example | sed -e 's/ [a-zA-Z0-9]\\{4\\} / /g'
echo $example | sed -e 's/ [a-zA-Z0-9]\\{4\\} / /g'
試過: sed -e s'/[A-Za-z]\\{,4\\}//g'
請使用簡單的awk
或sed
命令幫助我執行以下操作:
Here is an example line of fantastic data
並得到:
Here example line fantastic data
$ echo Here is an example line of fantastic data | sed -E 's/\b\(\w\)\{,3\}\b\s*//g'
Here is an example line of fantastic data
如果將句子存儲在變量中,則可以在 for 循環中遍歷它。 然后您可以評估每個單詞是否大於 2 個字符。
sentence="Here is an example line of fantastic data";
for word in $sentence; do
if [ ${#word} -gt 2]; then
echo -n $word;
echo -n " ";
fi
done
這是一個 BASH 示例,說明如果文件中有很多句子,這是最常見的情況,該怎么做?
#!/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.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.