mach
mach

Reputation: 8395

sed replace content within double quotes

I need to replace the versionName in a xml file from a shell script using sed.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.sed"
    android:versionCode="1"
    android:versionName="UNKNOWN VERSION NAME">

I've gotten so far as to search for a line containing versionName but how to tell sed to replace everything within the double quotes coming directly after versionName?

sed -i .old '/versionName/ s/WHAT TO WRITE?/NEW VERSION NAME/' AndroidManifest.xml

Upvotes: 6

Views: 14403

Answers (3)

Lungang Fang
Lungang Fang

Reputation: 1537

Considering the nature of xml and the version number, it is actually very safe to use a simpler command:

sed -i '/versionName/s/".*"/"NEW VERSION NAME"/' AndroidManifest.xml

P.S. in my opinion, it is very important to be able to simplify your shell script based on specific circumstances and reasonable assumptions.

Upvotes: 4

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed 's/\([[:blank:]]android:versionName="\)[^"]*"/\1Your New Value"/' YourFile
  • assuming that all section that have android:versionName will be changed
  • with GNU sed still better with replacing [[:blan:k]] by \(^\|[[:blank:]]\) (and a -i if direct modification avoiding temporary file)

Upvotes: 0

lcd047
lcd047

Reputation: 5861

Replace not-", like this:

sed -i .old '/android:versionName/ s/="[^"][^"]*"/="NEW VERSION NAME"/' AndroidManifest.xml

Upvotes: 8

Related Questions