0

I have a bunch of files that contain strings similar to:

Get<1>(abc)
Get<2>(xyz)

How can I recursively search for all files with the above pattern and remove everything before the first '(' and place a string afterwards?

Desired output:

abc.val()
xyz.val()

abc and xyz can change to anything, and the number between '<' and '>' don't matter.

Edit:

The files extensions are .cc and .h.

The string also always starts with Get.

6
  • Welcome, do these files have a specific extension? Jun 21 at 18:49
  • @schrodingerscatcuriosity The file extensions are .cc and .h
    – aWLW
    Jun 21 at 19:01
  • Is Get what we're keying on here? Jun 21 at 19:50
  • @glenn jackman Everything before the first character after the parenthesis, so Get<1>( can be checked. The number can change to anything.
    – aWLW
    Jun 21 at 20:09
  • Yeah, I understand that the number can change. But will we see Put<1>(something)? Is it always Get? Jun 22 at 3:23

1 Answer 1

1

You can use GNU grep to find files that contain at least one occurrence of the pattern and GNU sed to do the substitution in-place:

ere='Get<[[:digit:]]+>\(([[:alnum:]_]+)\)'
grep -rlEZe "$ere" . |
  xargs -r0 sed -i -E "s/$ere/\1.val()/g"

Here [[:alnum:]_]+ matches sequences of one or more alphanumeric characters or underscores. Change to [^()]* for 0 or more characters other than ( or ).

1
  • This seems close to what I'm looking for. The grep was able to find the files, but seems like there's an issue with sed. I'm getting sed: -e expression #1, char 19: invalid reference \1 on 's' command's RHS (maybe an issue with the grouping)
    – aWLW
    Jun 21 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.