1

I'm trying to replace a number in a unity file for my automated build process. I've tried different versions of the regexp, as well as commands, however none seem to work correct. I currently have

perl -0777 -ne 'print "${1}0" while /(WebGLSupport\s+m_APIs:\s[a-b0-9]{8,16}\s+m_Automatic\:\s)1/sg'  ../../CityBotVRSimWebGL/HandTracking/ProjectSettings/ProjectSettings.asset

which correctly prints and replaces the '1'

WebGLSupport
    m_APIs: 0b000000
    m_Automatic: 0

instead of the original

<...>
  - m_BuildTarget: WebGLSupport
    m_APIs: 0b000000
    m_Automatic: 1
<...>

However when I try to do an actual replace the complete content of the file is deleted (not the file itself)

perl -0777 -i -ne 's/(WebGLSupport\s+m_APIs:\s[a-b0-9]{8,16}\s+m_Automatic\:\s)1/${1}0/'  ../../CityBotVRSimWebGL/HandTracking/ProjectSettings/ProjectSettings.asset

Can anyone tell me what is going wrong. I'm really confused since the regexp seems to be correct. Thanks!

2 Answers 2

3

The -i switch says you are doing an inplace edit. However, you use the -n switch to process each line but don't output anything. Perhaps you wanted the -p which outputs the value of $_ (the current "line").

Sign up to request clarification or add additional context in comments.

Comments

2

It seems that you want to do a global find and replace on your file. You need -p on the command line and your regex can be simplified.

perl -0777 -i.bak -pe \
's{WebGLSupport\s+m_APIs:\s[a-b0-9]{8,16}\s+m_Automatic\:\s\K1\b}{0}g; ' \
ProjectSettings.asset

The \K assertion means to keep everything up until that point in the string and don't subject it to replacement. This effectively allows you to "replace this text that is preceded by this other text". No need for parens or captures. The 1 that comes after \K is all that is replaced by the new text, 0.

I added \b after the 1 to indicate that this must be a word boundary. Presumably you don't want to replace other occurrences of WebGLSupport that merely start with a 1. e.g.: m_Automatic: 123.

If the string only occurs once in the file, you can remove the /g flag.

https://perldoc.perl.org/perlrun
https://perldoc.perl.org/perlreref#ANCHORS

2 Comments

It's probably impossible to get 123, so 0 is probably fine. But if it is possible, it's far more likely it should be replaced with 0 rather than left as is. So \S+ would make more sense than 0\b.
Yes, it is correct that the full string/ regex only appears once in the document, whereas m_Automatic: x appears quite often. However x can only be 0 or 1 and in my case it only always needs to be changed to 0 and only at this position. Searching for the whole string and replacing the number with 0 by using -p works fine, so thanks. Is \K a perl thing or is it actually a common regexp? Never heard about it so far, though I've also mostly been using regex with javascript/ typescript. Seems like a nice feature

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.