Search code examples
regexstringnant

Regular expression to manipulate text file - Windows


I am having issues manipulating a text file to get my desired results.

For example, I have the following lines within a text file called sprocs.txt which was created from an svn diff, similar to this:

     M      https://localhost/svn/Repo/branches/projectA/sprocs/admin/foo.sql
     M      https://localhost/svn/Repo/branches/projectA/sprocs/foo2.sql
     M      https://localhost/svn/Repo/branches/projectA/sprocs/admin
     M      https://localhost/svn/Repo/branches/projectA/sprocs

I am trying to edit this file so that it keeps everything starting with /sprocs, but deletes each line that does not end in .sql. For example, the file above would return the following:

    /sprocs/admin/foo.sql 
    /sprocs/foo2.sql

BTW: I plan to have these commands be handled by nant. Any ideas anyone? Thanks in advance.


Solution

  • This would do the job using pure NAnt:

    <target name="go">
      <property
        name="file.path"
        value="C:\foo\sprocs.txt" />
      <foreach
        item="Line"
        in="${file.path}"
        property="line">
        <property
          name="MATCH"
          value="" />
        <regex
          pattern="(?'MATCH'/sprocs.*\.sql)$"
          input="${line}"
          failonerror="false" />
        <if test="${string::get-length(MATCH) > 0}">
          <echo message="match: ${MATCH}" />
          <!-- do whatever you want to do with your match -->
        </if>
      </foreach>
    </target>
    

    Not as elegant as palako's solution but it also works.