2

This may be a newbie regex question (still learning), but I'm trying to uncapitalize all the objects in my code (but not classes); i.e for the code snippet:

Bishop_Piece Bishop;

Bishop.move()...

I want it to instead be:

Bishop_Piece bishop;

bishop.move()...

What I tried:

find . -type f | xargs sed -i  's/Bishop[;.]/bishop./g'

However this results in:

Bishop_Piece bishop.

bishop.move()...

Basically, I want the character after what I'm searching for (i.e Bishop), to be 'kept' (be it ; or .), which is what explains the bishop./g.

1
  • 2
    When using sed, you can match [;.] with a capture group and return whatever you matched to the output in the same place it was found. Commented Feb 15, 2021 at 20:57

1 Answer 1

3

You can use

find . -type f | xargs sed -i 's/Bishop\([;.]\)/bishop\1/g'

Note that the \([;.]\) here defines a capturing group whose value is referred to with \1 from the replacement pattern.

The (...) parentheses are escaped since this is a POSIX BRE compliant regular expression.

See the online demo:

s='Bishop_Piece bishop;
bishop.move()...'
sed 's/Bishop\([;.]\)/bishop\1/g' <<< "$s"

Output:

Bishop_Piece bishop;
bishop.move()...
Sign up to request clarification or add additional context in comments.

2 Comments

This works. I understand the solution was the \1 part. Thanks!
(and the change of \([;.]\) for the find)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.