0

I need to go through thousands of files and find lines that start with

# disabledRule:

On these lines I need to look for a series of keywords, namely fileinto or fileinto_copy followed by create and target. Examples:

{s:4:"type";s:8:"fileinto";s:6:"create";b:0;s:6:"target";s:5:"PATTERN";}
{s:4:"type";s:13:"fileinto_copy";s:6:"create";b:0;s:6:"target";s:5:"PATTERN";}

After those keywords I need to modify the PATTERN and replace . with /.

PATTERN can consist of almost any character e.g.

simple.folder.structure
more.complex.[department_1] @employee-XY

The parts between the keywords can vary slightly and the whole "keywords+PATTERN" pattern can occur multiple times in one line.

It sounds easy but I couldn't figure out a way to make it work with sed/awk without writing a big script and take apart the lines. I think I'm half decent with sed to replace stuff and also to find patterns, but I don't know how to limit the replacement to only the PATTERN.

Any suggestions in what I could use to do this, without writing a longer script? Is there maybe a way with sed capture groups?

Update: As some may have figured out, the files need to be modified because of a migration. We knew that the new software is not fully compatible but as it turns out the above mentioned lines "# disabledRule" are not recognised by the new software at all, hence rendering the problem unsolvable from this perspective.

3
  • Welcome, could you give a real example of PATTERN? edit the question, don't add it in the comments. Feb 18, 2021 at 18:14
  • There are no other . in the key-pairs. It's just the sample, or there could be? Feb 18, 2021 at 18:23
  • Looks like php serialised data. Aug 20, 2021 at 16:24

1 Answer 1

0

Yes, you can probably use address ranges of sed, but you don't tell when a # disabledRule: section ends. If it ends at the next empty line, for example, and there are no other dots in the given lines outside the pattern of filecopy lines, it's extremely simple:

sed '/^# disabledRule:$/,/^$/s_\._/_g'

But in this case you probably would not ask. This one does addition filtering for the keywords:

sed '/^# disabledRule:$/,/^$/{/"fileinto\(_copy\)*".*"create".*"target"/s_\._/_g;}'

If this still doesn't match your requirements, adapt it further or explain more details, at best giving a longer example of the file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .