简体   繁体   English

用Unix代替文本中的所有单词,但使用Linux和空格

[英]Substitute all words in text by Unix but Linux and whitespaces

I have a text that reads like this: 我有这样的文字:

Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. Linux提供了一些功能强大的管理工具和实用程序,可以帮助您有效地管理系统。 If you don't know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. 如果您不知道这些工具是什么以及如何使用它们,则可能会花费大量时间尝试执行基本的管理任务。 The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator. 本课程的重点是帮助您了解系统管理工具,这将帮助您成为有效的Linux系统管理员。 Get the Linux Sysadmin Course Now! 立即获取Linux Sysadmin课程!

I want to substitute all the words EXCEPT Linux and whitespaces in the text by the word Unix with the sed tool. 我想用sed工具将Unix中的所有单词EXCEPT Linux和whitespaces替换为Unix。 Any ideas? 有任何想法吗? I have tried this: 我已经试过了:

sed -e 's/[^linux]/linux/g' -e 's/[[:space:]]/[[:space:]]/g'

But it just outputs unix without spaces. 但是它只是输出unix而没有空格。

"Everything BUT" is usually hard with regular expressions. 使用正则表达式通常很难做到“一切都好”。 You can probably do it in 3 steps, first change Linux into a special unique character, then change all words to Unix, then change back the magic characters to Linux. 您可能可以分3个步骤完成,首先将Linux更改为特殊的唯一字符,然后将所有单词更改为Unix,然后将魔术字符更改为Linux。

sed -r -e 's/Linux/@/g' -e 's/[^ @,;\.]+/Unix/g' -e 's/@/Linux/g'

The above retains the punctuation as well. 上面也保留了标点符号。

[...] always defines a character class, thus it matches a SINGLE character. [...]始终定义一个字符类,因此它匹配一个字符。 So [^linux] is not a non-linux word, but a single character which is not l , i , n , u or x . 因此[^linux]不是非Linux单词,而是不是linux的单个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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