Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed replace content within double quotes

Tags:

regex

bash

sed

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
like image 862
mach Avatar asked Jun 04 '15 07:06

mach


People also ask

Does sed work with double quotes?

@DummyHead Here's another approach: if you use single quotes, the sed command is exactly as you type it. If you use double quotes, you must take into account all shell substitutions and elaborations to "visualize" what command is eventually passed on to sed.

Does sed support multiline replacement?

By default, when sed reads a line in the pattern space, it discards the terminating newline (\n) character. Nevertheless, we can handle multi-line strings by doing nested reads for every newline.

How do you use sed to substitute a new line?

Using `sed` to replace \n with a comma By default, every line ends with \n when creating a file. The `sed` command can easily split on \n and replace the newline with any character. Another delimiter can be used in place of \n, but only when GNU sed is used.


1 Answers

Replace not-", like this:

sed -i .old '/android:versionName/ s/="[^"][^"]*"/="NEW VERSION NAME"/' AndroidManifest.xml
like image 155
lcd047 Avatar answered Oct 08 '22 18:10

lcd047