2

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!

4
  • 4
    Better use sed for that. Commented Feb 26, 2020 at 9:56
  • Could you please post sample of input and expected output in your question and let us know for better understanding. Commented Feb 26, 2020 at 9:57
  • 1
    Try LC_ALL=C sed -i 's/[[:space:]]*[A-Fa-f0-9]\{8\}//g' showrunningconfig.txt Commented Feb 26, 2020 at 10:00
  • You can also use [[:xdigit:]] instead of [A-Fa-f0-9]. Commented Feb 26, 2020 at 10:07

1 Answer 1

2

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.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.