且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何sed搜索将整个单词替换为文件中的字符串匹配

更新时间:2023-02-18 20:20:40

按如下所示修改您的sed命令应提取您想要的内容,

Modify your sed command as followed should extract what you want,

sed -e "$(sed 's:\<.*\>:s/&\\w*\\s//ig:' file.txt)"

简要说明,

  • \b匹配单词和非字母数字字符之间的位置.在这种情况下,file.txt中的模式"test"将与"Testing"不匹配.
  • 通过这种方式,修改附加了\w*的搜索模式应该可以. \w实际上与[a-zA-Z0-9_]
  • 相匹配
  • 并且不要忘记消除每个搜索模式后面的空间,应添加\s.
  • \b matches the position between a word and a non-alphanumeric character. In this case, the pattern 'test' in file.txt would not match 'Testing'.
  • In this way, modify the searched pattern appended with \w* should work. \w actually matched [a-zA-Z0-9_]
  • And don't forget to eliminate the space behind each searched pattern, \s should be added.