Can grep delete context, but not a full line?

Viewed 115

I am removing keys from a config file by the following command:

cat showrunningconfig.txt | grep -v '[ \t\r\n\v\f]*[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]'

This removes the whole line. But I want to remove only the relevant patterns. grep has the -o option, which shows only the relevant pattern and not the whole line. But the -o option is not working in combination with -v

Any idea? Thanks a lot!

1 Answers

You should use sed when you have a partial pattern to remove from a string.

sed -i 's/[[:space:]]*[[:xdigit:]]\{8\}//g' showrunningconfig.txt

See the online demo

s="Text A1f4E3D4 and more text"
sed 's/[[:space:]]*[[:xdigit:]]\{8\}//g' <<< "$s"
# => Text and more text

Details

  • -i - in-place replacement (GNU sed option)
  • s/[[:space:]]*[[:xdigit:]]\{8\}//g:
    • s - substitute command
    • [[:space:]]* - 0+ whitespaces
    • [[:xdigit:]]\{8\} - eight A-F, a-f and 0-9 chars.
Related