Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove a line with Ant replaceregexp, but not leave an empty line

Tags:

ant

I have the following text file:

    Manifest-Version: 3.0.0
    Ant-Version: Apache Ant 1.7.1
    Created-By: 10.0-b19 (Sun Microsystems Inc.)
    Require-Bundle: org.eclipse.linuxtools.cdt.libhover;bundle-version="1.
     1.0"
    Bundle-SymbolicName: com.qnx.doc.gestures.lib_ref
    Bundle-Version: 3.0.0.20121120
    Bundle-Localization: plugin
    Bundle-Name: %plugin.name
    Bundle-Vendor: %plugin.providername

And I'm trying to use the following pattern in a replaceregexp task

    regexp pattern='Require-Bundle: org.eclipse.linuxtools.cdt.libhover;bundle-version="1.
   [\s\S]*'

To end up with this:

    Manifest-Version: 3.0.0
    Ant-Version: Apache Ant 1.7.1
    Created-By: 10.0-b19 (Sun Microsystems Inc.)
     1.0"
    Bundle-SymbolicName: com.qnx.doc.gestures.lib_ref
    Bundle-Version: 3.0.0.20121120
    Bundle-Localization: plugin
    Bundle-Name: %plugin.name
    Bundle-Vendor: %plugin.providername

The trouble is I keep getting this:

    Manifest-Version: 3.0.0
    Ant-Version: Apache Ant 1.7.1
    Created-By: 10.0-b19 (Sun Microsystems Inc.)

     1.0"
    Bundle-SymbolicName: com.qnx.doc.gestures.lib_ref
    Bundle-Version: 3.0.0.20121120
    Bundle-Localization: plugin
    Bundle-Name: %plugin.name
    Bundle-Vendor: %plugin.providername

What should my regex be to get rid of the empty line?

Thanks.

like image 263
Drew Avatar asked Oct 06 '22 22:10

Drew


1 Answers

Something like this should work:

Require-Bundle: org\.eclipse\.linuxtools\.cdt\.libhover;bundle-version="1\.\s*1.0"\s*

(using \s* to match zero or more whitespace characters, which include \r and \n) but since you're dealing with a manifest file it would make more sense to use a proper manifest parser. Unfortunately the Ant <manifest> task doesn't provide a way to remove attributes, but it's pretty straightforward with a <script> task:

<property name="manifest.file" location="path/to/manifest.txt" />

<script language="javascript"><![CDATA[
    importPackage(java.io);
    importPackage(java.util.jar);

    // read the manifest
    manifestFile = new File(project.getProperty('manifest.file'));
    manifest = new Manifest();
    is = new FileInputStream(manifestFile);
    manifest.read(is);
    is.close();

    // remove the offending attribute
    manifest.getMainAttributes().remove(new Attributes.Name('Require-Bundle'));

    // write back to the original file
    os = new FileOutputStream(manifestFile);
    manifest.write(os);
    os.close();
]]></script>
like image 80
Ian Roberts Avatar answered Oct 09 '22 11:10

Ian Roberts