1

I am trying to replace the android:versionName value in the following manifest with the current date:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    coreApp="true"
    package="com.btaudio"
    android:versionName="0.7-191027"
    android:sharedUserId="android.uid.system">

Here is my batch script with sed command:

chmod 777 AndroidManifest.xml

test=$(date '+%y%m%d')

sed 's|(?<=-).*?(?="\n)|'"$test"'|g' AndroidManifest.xml

My idea is to grab the text between "-" and the double quote that is followed by newline, the variable work fine after using delimiter.

Could anyone please explain to me why the regex expression is not recognized by sed, although on regex101 it show exactly the text that I want to replace (https://regex101.com/r/yHFkrV/1).

I have read about using -e, -E or -r para but none of those work for me. Also I have tried some delimiter on the expression but it does not seem to have any effect.

Edit: I am using sed (GNU sed) 4.2.2 on Ubuntu 16.04.3 LTS

Inian
  • 80,270
  • 14
  • 142
  • 161
Kyoshin
  • 57
  • 5
  • Is lookaround and lookahead supported by your `sed` implementation? Please check! – stephanmg Nov 13 '19 at 11:46
  • 1
    `sed` doesn't support neither look-ahead nor look-behind. – Anubis Nov 13 '19 at 11:46
  • See also https://stackoverflow.com/questions/12176026/whats-wrong-with-my-lookahead-regex-in-gnu-sed – stephanmg Nov 13 '19 at 11:47
  • 1
    @Kyoshin, see [Why does my regular expression work in X but not in Y?](https://unix.stackexchange.com/questions/119905/why-does-my-regular-expression-work-in-x-but-not-in-y) – Sundeep Nov 13 '19 at 12:09

1 Answers1

1

You can't use lookarounds in sed POSIX regex patterns.

Use consuming patterns instead and restore them using the RHS, the replacement pattern (which is easy since they are hardcoded characters, you do not even need additional capturing groups):

sed -i 's|-[^"]*"$|-'"$test"'"|g' AndroidManifest.xml

See an online sed demo.

Details

  • -i - inline changes to the file
  • s - substitution command
  • -[^"]*"$ - -, any 0 or more chars other than " and then end of string
  • -'"$test"'" - -, then $test value, and then "
  • g - multiple occurrences (seems redundant here though)
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563