简体   繁体   English

使用 Linux 删除少于 4 个字符的单词

[英]Remove Words Shorter Than 4 Characters Using Linux

I have read the following and tried to rework the command logic for what I want.我已阅读以下内容并尝试根据我的需要重新设计命令逻辑。 But, I just haven't been able to get it right.但是,我只是没能把它做好。

Delete the word whose length is less than 2 in bash 在bash中删除长度小于2的单词

Tried: echo $example | sed -e 's/ [a-zA-Z0-9]\\{4\\} / /g'试过: echo $example | sed -e 's/ [a-zA-Z0-9]\\{4\\} / /g' echo $example | sed -e 's/ [a-zA-Z0-9]\\{4\\} / /g'

Remove all words bigger than 6 characters using sed 使用 sed 删除所有大于 6 个字符的单词

Tried: sed -e s'/[A-Za-z]\\{,4\\}//g'试过: sed -e s'/[A-Za-z]\\{,4\\}//g'


Please help me with a simple awk or sed command for the following:请使用简单的awksed命令帮助我执行以下操作:

Here is an example line of fantastic data

And get:并得到:

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

If you store the sentence in a variable, you can iterate through it in a for loop.如果将句子存储在变量中,则可以在 for 循环中遍历它。 Then you can evaluate if each word is greater than 2 characters.然后您可以评估每个单词是否大于 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

This is a BASH example of how to do it if you have a lot of sentences in a file which would be the most common case right?这是一个 BASH 示例,说明如果文件中有很多句子,这是最常见的情况,该怎么做?

SCRIPT (Remove words with two letters or shorter) SCRIPT(删除两个字母或更短的单词)

#!/bin/bash

while read line
do

    echo "$line" | sed -E 's/\b\w{1,2}\b//g' 

done < <( cat sentences.txt )

INPUT输入

$  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.

OUTPUT输出

$ ./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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM