【问题标题】:Remove Words Shorter Than 4 Characters Using Linux使用 Linux 删除少于 4 个字符的单词
【发布时间】:2017-10-11 03:57:22
【问题描述】:

我已阅读以下内容并尝试根据我的需要重新设计命令逻辑。但是,我只是没能把它弄好。

Delete the word whose length is less than 2 in bash

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

Remove all words bigger than 6 characters using sed

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


请帮助我使用简单的awksed 命令来执行以下操作:

Here is an example line of fantastic data

得到:

Here example line fantastic data

【问题讨论】:

  • 为什么你不能应用适合你逻辑的答案?我确信这不是很难。你尝试了什么?
  • 是的,这些都是我尝试重做逻辑的失败。第一个真的不行。第二个好像我只是想念它。
  • sed -E 's/\b\w{1,3}\b[[:blank:]]*//g' file

标签: linux bash unix awk sed


【解决方案1】:
$ 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

【讨论】:

  • 啊,这是完美的,因为我正在通过脚本传输我的数据。您要添加代码以取出空格吗?我会标记你是正确的。如果没有,我可以在另一个管道中执行此操作。无论哪种方式,感谢您的快速帮助。
  • @DomainsFeatured 我已经通过另一个 sed 命令删除了多余的空格。
  • 您可以一次性使用多个 sed 命令,以; 分隔,而不是使用管道生成另一个子shell。
  • @stevesliva 感谢您的意见。我对第二根管子也不满意。修改了我的答案。
  • @HakanBaba - 好吧,它只有在它有效时才有用......我实际上是在尝试我的想法并最终选择了sed 's/\b\w\{1,3\}\b\s*//g'。先前的建议完全删除了所有空白。而且我不认为-E 是必要的。
【解决方案2】:

如果将句子存储在变量中,则可以在 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

【讨论】:

    【解决方案3】:

    这是一个 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.
    

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多