Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

specific solution required (batch file programming)

Options
  • 30-06-2005 1:42pm
    #1
    Registered Users Posts: 1,285 ✭✭✭


    hi,

    I'll be brief, I need help writing a batch file that will do some manual editing for me? Or some other simple way to automate the following problem.

    I have a text file woth 100s of entries that look like this.

    704477004986001033 0C
    704477005000000117 0C
    704477005000000158 1C
    704477005000000166 1C
    704477005001000025 0C
    704477005015000102 0C
    704477005015000128 0C
    704477005027000017 0C
    704477005029000015 0C

    Each one is an individual line in the text file. I need to write something that will insert a "D" in front of lines ending in "0C" and an "I" in front of the ones ending in "1C".
    Following this format the above would be edited to appear as

    D704477004986001033 0C
    D704477005000000117 0C
    I704477005000000158 1C
    I704477005000000166 1C
    D704477005001000025 0C
    D704477005015000102 0C
    D704477005015000128 0C
    D704477005027000017 0C
    D704477005029000015 0C


    Can this be solved with batch files? Any advice on a quick way to automate it? maybe a VB program?

    Any advice greatly appreciated

    Cheers

    F


Comments

  • Registered Users Posts: 1,268 ✭✭✭hostyle


    Perl solution any good to you?
    #!/usr/bin/perl -w
    
    open (INFILE, "<old.txt") or die($!);
    open (OUTFILE, ">new.txt") or die($!);
    while (<INFILE>) {
      s/(.*0C)$/D$1/;
      s/(.*1C)$/I$1/;
      print OUTFILE $_;
    }
    close (INFILE);
    close (OUTFILE);
    


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    can't be done with batch files but any search and replace tool worth its salt could do it. Perl solution is elegant.


  • Registered Users Posts: 2,345 ✭✭✭ErinGoBrath


    Personally I think it would be best to write a small VB app.

    Psuedo code:

    Open file
    Read all values into string array
    do while i less then end of array
    If Right(string(i),2) = "OC"
    string(i) = "D" + string(i)
    Else if Right(string(i),2) = "1C"
    string(i) = "I" + string(i)
    Else
    Invalid entry bla bla bla
    End if

    i = i + 1
    loop
    Write results back out to file


    EDIT: Ah! It editing out my indentation!


  • Registered Users Posts: 1,285 ✭✭✭100gSoma


    thats a great help guys... I'll adapt the aforementioned solutions into something workable. Thanks very much again...

    F


  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 91,690 Mod ✭✭✭✭Capt'n Midnight




  • Advertisement
  • Registered Users Posts: 1,285 ✭✭✭100gSoma


    hi all,

    Unfortuantley I never got this sorted. Couldn't find SED for XP anywhere.
    how would I create a program using the PERL code above? Do I need some sort ot executable builder for the code, also what be the interface? command line?
    any help advice much appreciated :(


  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 91,690 Mod ✭✭✭✭Capt'n Midnight




  • Registered Users Posts: 938 ✭✭✭logic


    Try this for the perl script above

    from activestate.com


  • Registered Users Posts: 131 ✭✭theexis


    Hobbes wrote:
    can't be done with batch files but any search and replace tool worth its salt could do it. Perl solution is elegant.

    Oh ye of little faith ;)

    Assuming your source file is c:\temp.txt through this in a batch file (say do.bat) and run do.bat > new.txt.
    @echo off
    
    for /F "tokens=1,2* delims= " %%x in (c:\temp.txt) do (
    
    	if "0C"==%%y (
    	
    		echo D%%x %%y
    		
    	) else (
    	
    		echo I%%x %%y
    	)
    )
    


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Think that line should be...

    if "0C"=="%%y"

    Nice btw!


  • Advertisement
  • Registered Users Posts: 1,285 ✭✭✭100gSoma


    WOW... Thats EXCELLENT theexis. Thanks alot.
    When I run it it 'echos' the change I want to make alright, so the syntax is correct, but is there a way to make it 'print' the change to the file. ie: not just 'echo' but 'write' it.

    thanks for help so far, thats really brilliant.
    thanks hobbes too for pointing out the syntax issue with the " "


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Write it to batch file like A.BAT
    then

    A.BAT > newfile.txt


  • Registered Users Posts: 1,285 ✭✭✭100gSoma


    you probably won't belive this, but during my experimentation I ran this:

    @echo off

    for /F "tokens=1,2* delims= " %%x in (c:\temp.txt) do (

    if "0C"=="%%y" (

    > D%%x %%y

    ) else (

    > I%%x %%y
    )
    )


    Basically I just created 759 files on my desktop. :D ok ok so its kind of funny.

    I need to either amend\edit the c:\temp.txt file as the 'if' statements are true, or else create ONE new file containing 759 etc lines in it. Getting closer to a solution now though. Thanks for all the sugegstions they are really helping.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    yes that would do that as your telling it to output nothing to a file called the name of your text.

    Do as I said. Save the batch as normal (originally shown) and then run it from the command line like...

    batchfile.bat > newtext.txt

    that will echo the output to the new text file.


  • Registered Users Posts: 1,285 ✭✭✭100gSoma


    :D I understand Hobbes!!! Thanks... I now have a 1st batch file that does the calling of the 2nd one and also specifies an output file. It works sweet.
    Thanks also theexis.
    Delighted with that. Saves alot of time. ;)

    Hobbes wrote:
    yes that would do that as your telling it to output nothing to a file called the name of your text.

    Do as I said. Save the batch as normal (originally shown) and then run it from the command line like...

    batchfile.bat > newtext.txt

    that will echo the output to the new text file.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    You can do it in one batch file as well.

    Change
    echo D%%x %%y
    echo I%%x %%y

    to
    echo D%%x %%y >> output.txt
    echo I%%x %%y >> output.txt

    Just make sure output.txt is deleted before the program starts.


  • Registered Users Posts: 1,285 ✭✭✭100gSoma


    Thanks Hobbes. Thats has worked SWEET!!! exactly what I was after...

    Thanks all for the contributions. very much appreciated ;)

    Hobbes wrote:
    You can do it in one batch file as well.

    Change
    echo D%%x %%y
    echo I%%x %%y

    to
    echo D%%x %%y >> output.txt
    echo I%%x %%y >> output.txt

    Just make sure output.txt is deleted before the program starts.


Advertisement