I am maintaing a linux driver and I want to replace all %p in driver with %pK. For doing this i used following command.grep %p[^K] -Rl . | xargs sed -i 's/%p[^K]/%pK/g'It fails in below case.%p-%p --> %pK%pK (hyphen is removed)Please suggest proper regular expressionfor sed command to work.
Replace %p with %pK in linux kernel code
sed;grep;regular expression
The sed replacement in the question would remove any characters after a %p, not just dashes, since the [^K] is part of the matched string. You can work around that by capturing the character after %p with (escaped) parenthesis, and putting it back with \1. In sed:$ echo '%p %p %pK' | sed 's/%p\([^K]\)/%pK\1/g'%pK %pK %pKThough you might want to use [^a-zA-Z] or similar just in case you were to use other modifiers for %p.