Question

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.

Was it helpful?

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.

OTHER TIPS

 $ cat sprocs.txt | sed -n 's/.*\(\/sprocs\/.*\.sql$\)/\1/p'
/sprocs/admin/foo.sql
/sprocs/foo2.sql

The following regex pattern should work for you, it will create a capturing group that will find anything starting with /sprocs and ending with .sql

(/sprocs.*\.sql$)

Be sure to enable the multi-line mode by passing it in the options for the nant regex otherwise this will not match properly. I believe this is the m option as is explained here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top