Announcement

Collapse
No announcement yet.

Batch Scripting - how to read a string value from a text file

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Batch Scripting - how to read a string value from a text file

    I am trying to create a batch file that steps through a file until it finds a particular line. When it finds the line, it writes a few key words from that line into a new file.


    To make things less abstract let me give an example of what I am trying to do:

    I have a file that I need to look through to extract all values after the Name tag, and put it in a different file if it does not already exist in that file. The lines within the file I am searching through look like this:


    <Uri AffinityCookie="JSESSIONID" AffinityURLIdentifier="jsessionid" Name="/contextroot1/*"/>
    <Uri AffinityCookie="JSESSIONID" AffinityURLIdentifier="jsessionid" Name="/contextroot2/*"/>
    <Uri AffinityCookie="JSESSIONID" AffinityURLIdentifier="jsessionid" Name="/contextroot3/*.jsp"/>



    So I need to look for lines that contain the following text:

    <Uri AffinityCookie="JSESSIONID" AffinityURLIdentifier="jsessionid" Name=

    then only extract the value after the 'Name =' tag


    so in this case I need /contextroot1/*, /contextroot2/* and /contextroot3/*.jsp. Then I need to put these values in a file TestFile.txt if that file does not already contain these values.


    So for now i am able to just get all the lines containing the text:

    <Uri AffinityCookie="JSESSIONID" AffinityURLIdentifier="jsessionid" Name=

    but not sure how can I extract the value after Name=
    and store it in the file TestFile.txt


    I know that this could be easily done using Perl or VBScript, but I do need to use a batch file to get this done.

  • #2
    Re: Batch Scripting - how to read a string value from a text file

    Simple, really. I will assume your input file is at C:\Input.txt (change this name in the script below).


    Code:
     
     
    # Script names.txt
    # Read input file into str variable.
    var str input ; cat "C:\Input.txt" > $input
     
    # Repeatedly extract strings between Name=" and " .
    while ( { sen -c -r "^Name=&\"&\"^"  $input } > 0 ) 
    do
        # Remove portion upto Name=" .
        stex -c "^Name=\"^]" $input > null
        # Print portion upto " .
        stex -c "]^\"^" $input
    done

    Save the script as C:\Scripts\names.txt. Script is in biterscripting. Start biterscripting (download free from http: //www .biterscripting .com), and enter the following command.

    Code:
    script names.txt
    You will see all names one per line. To save output to C:\TestFile.txt, redirect the output of the script as follows.

    Code:
    script names.txt > "C:\TestFile.txt"

    Should do it.

    Patrick
    Last edited by Rems; 29th June 2010, 18:18. Reason: live link removed by mod

    Comment


    • #3
      Re: Batch Scripting - how to read a string value from a text file

      Originally posted by PatrickMc View Post
      Script is in biterscripting
      Keep in mind that the original question did ask specifically for a batch script.

      Originally posted by albert_newton View Post
      I know that this could be easily done using Perl or VBScript, but I do need to use a batch file to get this done.
      Gareth Howells

      BSc (Hons), MBCS, MCP, MCDST, ICCE

      Any advice is given in good faith and without warranty.

      Please give reputation points if somebody has helped you.

      "For by now I could have stretched out my hand and struck you and your people with a plague that would have wiped you off the Earth." (Exodus 9:15) - I could kill you with my thumb.

      "Everything that lives and moves will be food for you." (Genesis 9:3) - For every animal you don't eat, I'm going to eat three.

      Comment

      Working...
      X