Sunil Kumar
Sunil Kumar

Reputation: 397

Linux - How can i replace some string with same string enclosed with braces?

I have some files in a dir, when i grep some string, i get result like below.

scripts/FileReplace> grep -r "case" *

dir1/file2:case 'a'

dir2/file3:case "ssss"

file1:case 1

After i use replace cmd i expect strings in files updated as below

CASE ('a')

CASE ("ssss")

CASE (1)

ie,, "case" is replaced with "CASE" and text after space is enclosed in braces as above.

Any suggestion how i can do this with shell cmd.

Upvotes: 0

Views: 62

Answers (1)

choroba
choroba

Reputation: 241758

You can use sed and its substitution:

find . -type f -exec sed -i 's/case \(.\+\)/CASE (\1)/' {} +
  • .\+ matches anything that has at least one character.
  • \(...\) creates a capture group, you can reference the first capture group as \1.
  • running with -i~ instead of -i will create backups of the files; recommended especially if you're just experimenting.

Upvotes: 3

Related Questions