简体   繁体   中英

Can grep delete context, but not a full line?

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!

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 AF , af and 0-9 chars.

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