NovumCoder
NovumCoder

Reputation: 4627

sed replace entire line with a string using simple regex check

Im using cordova to build an app for android but cannot set the android:minSdkVersion in the AndroidManifest.xml file.

So i decided to modify this file each time i start building the app with a script i wrote.

I want to change the line:

<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />

to this:

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" />

Since cordova does not allow me to set this using the config.xml (only phonegap build allows, but i use cordova only) i need to modify the xml on my own.

I would like to replace whats inside mindSdkVersion to 14.

How can i do that?

I tried this:

sed 's/minSdkVersion="(.*)"/minSdkVersion="14"/g' AndroidManifest.xml

but it still gives me the uses-sdk line unchanged.

Whats wrong with my regexp?

Upvotes: 1

Views: 145

Answers (2)

tripleee
tripleee

Reputation: 189317

There are two problems with your regex.

  1. The parentheses are apparently interpreted as literal parentheses by your sed dialect. They are useless anyway, so you don't need to fix that -- just remove them.

  2. .* is greedy and would match up to just before the last double quote.

With these remarks, the fix is easy.

sed 's/minSdkVersion="[^"]*"/minSdkVersion="14"/g' AndroidManifest.xml

Upvotes: 1

martin
martin

Reputation: 3239

Three things:

  1. You need to modifily the group by replacing (.*) with \(.*\)
  2. You don't need the group.
  3. You match too much, try

    sed 's/minSdkVersion="[0-9]+"/minSdkVersion="14"/g' AndroidManifest.xml

If you want to modify the file, use -i to change the file.

Upvotes: 1

Related Questions