简体   繁体   中英

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. 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. Get the Linux Sysadmin Course Now!

I want to substitute all the words EXCEPT Linux and whitespaces in the text by the word Unix with the sed tool. Any ideas? I have tried this:

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

But it just outputs unix without spaces.

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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