【问题标题】:How convert empty space between parts of a word multisectoral?如何转换单词多部门之间的空白空间?
【发布时间】:2016-05-25 18:44:23
【问题描述】:

如何通过正则表达式从这个文件中逐行转换单词 multisectoral 部分之间的空白空间?并替换为“_”?在 linux 中(sed 或 ex)

来自

0656.txt W * PUNC PUNC my word
0656.txt W * CONJ CONJ my best word

0656.txt W * PUNC PUNC my_word
0656.txt W * CONJ CONJ my_best_word

【问题讨论】:

  • 欢迎来到 StackOverflow!这不是代码编写服务,而是帮助提供者。请向我们展示您迄今为止为解决此问题所做的尝试。
  • “词的一部分” 可以是很多东西。你能在你的正则表达式中详细说明你希望的规则吗?无论如何,也许这有帮助:$ sed -r 's/(([a-z]+) ([a-z]))/\1_\2/g)' file
  • multisectoral 到底是什么意思?

标签: regex linux sed


【解决方案1】:

您的问题是否含糊不清,并且没有严格的目标(词的一部分)?

keyword='my'

while read line || [[ -n "$line" ]]; do
    replace=$(echo "$line" | sed -re "s/^.*($keyword)/\1/g" | sed -re "s/ /_/g")
    echo "$line" | sed -re "s/($keyword.*)/$replace/g"
done <"file.txt" >"outfile.txt"

这会将关键字后面的单词之间的空格 替换为下划线。然后将其插入到较大的字符串中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-17
    • 2013-07-09
    • 2015-04-09
    • 1970-01-01
    • 2012-09-25
    • 2011-05-29
    • 2022-01-14
    • 2021-12-15
    相关资源
    最近更新 更多