Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: batch file to delete before character and after certain character  (Read 1040 times)

0 Members and 1 Guest are viewing this topic.

ironsurface17

    Topic Starter


    Greenhorn

    • Experience: Beginner
    • OS: Unknown
    I was working a batch file to extract certain characters from a file.txt and save as file2.txt :



    variant":{"key":"standard","name":"Standard","short":"Std"},"
    "initial "Fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
    "fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0"}]}
    {"flipBoard":"Flip board","aiNameLevelAiLevel":"%1$s level %2$s","yourTurn":"Your turn","abortGame":"Abort


    I want to extract the bolded characters and save it to file2.txt.

    what would the completed batch file looks like


    « Last Edit: February 17, 2018, 08:14:33 PM by ironsurface17 »

    Salmon Trout



      Genius

      Thanked: 925
      • Yes
    • Computer: Specs
    • Experience: Experienced
    • OS: Other
    Re: batch file to delete before character and after certain character
    « Reply #1 on: February 18, 2018, 09:25:01 AM »
    Does line 3 always start with these 6 characters? "fen":

    Salmon Trout



      Genius

      Thanked: 925
      • Yes
    • Computer: Specs
    • Experience: Experienced
    • OS: Other
    Re: batch file to delete before character and after certain character
    « Reply #2 on: February 18, 2018, 11:57:57 AM »
    And, when you say "extract", do you mean you want the output file to be the original file, but without those characters, or should the output file be just those characters?

    Salmon Trout



      Genius

      Thanked: 925
      • Yes
    • Computer: Specs
    • Experience: Experienced
    • OS: Other
    Re: batch file to delete before character and after certain character
    « Reply #3 on: February 18, 2018, 01:30:20 PM »
    Are the characters to be extracted

    * always on line 3?
    * a fixed length starting at character 8 and ending at character 61?

    « Last Edit: February 18, 2018, 01:45:15 PM by Salmon Trout »

    Hackoo



      Intermediate
    • Thanked: 12
    • Experience: Expert
    • OS: Windows 10
    Re: batch file to delete before character and after certain character
    « Reply #4 on: February 18, 2018, 01:45:26 PM »
    Give a try for this batch and explain more your aim if this not what you want ?

    Code: [Select]
    @echo off
    Set "Tmpvbs=%temp%\Tmpvbs.vbs"
    Set "InputFile=file.txt"
    Set "OutPutFile=file2.txt"
    Call :Extract "%InputFile%" "%OutPutFile%"
    Start "" "%OutPutFile%" & exit
    ::****************************************************
    :Extract <InputData> <OutPutData>
    (
    echo Data = WScript.StdIn.ReadAll
    echo Data = Extract(Data,"\b[^""fen:](\w*)\/(\w*).*\b"^)
    echo WScript.StdOut.WriteLine Data
    echo '************************************************
    echo Function Extract(Data,Pattern^)
    echo    Dim oRE,oMatches,Match,Line
    echo    set oRE = New RegExp
    echo    oRE.IgnoreCase = True
    echo    oRE.Global = True
    echo    oRE.Pattern = Pattern
    echo    set oMatches = oRE.Execute(Data^)
    echo    If not isEmpty(oMatches^) then
    echo        For Each Match in oMatches 
    echo            Line = Line ^& Match.Value ^& vbcrlf
    echo        Next
    echo        Extract = Line
    echo    End if
    echo End Function
    echo '************************************************
    )>"%Tmpvbs%"
    cscript /nologo "%Tmpvbs%" < "%~1" > "%~2"
    If Exist "%Tmpvbs%" Del "%Tmpvbs%"
    exit /b
    ::****************************************************

    ironsurface17

      Topic Starter


      Greenhorn

      • Experience: Beginner
      • OS: Unknown
      Re: batch file to delete before character and after certain character
      « Reply #5 on: February 18, 2018, 02:29:30 PM »
      Thank you so much. It deleted  including/everything after the }]}    :)


       Just one note. The html txt file has multiple "fen":"  is there a way to delete everything and including the last "fen":" and not the very first "fen":" it finds. It seems like the code found the first "fen":" in the text file and deleted everything before it.

      Thank you so much for this code Hackoo. Please include your paypal email. I will donate $20 for your effort and help.

      ...
      ...
      more code before this line..
      some code,"fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" code continues
      some code,"fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" code continues
      variant":{"key":"standard","name":"Standard","short":"Std"},"
      "initial "fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
      "fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0"}]}
      {"flipBoard":"Flip board","aiNameLevelAiLevel":"%1$s level %2$s","yourTurn":"Your turn","abortGame":"Abort
      .... code continues...
      « Last Edit: February 18, 2018, 03:07:28 PM by ironsurface17 »

      ironsurface17

        Topic Starter


        Greenhorn

        • Experience: Beginner
        • OS: Unknown
        Re: batch file to delete before character and after certain character
        « Reply #6 on: February 18, 2018, 04:33:29 PM »
        Thank you so much. It deleted  including/everything after the }]}    :)


         Just one note. The html txt file has multiple "fen":"  is there a way to delete everything before and including the last "fen":" and not the very first "fen":" it finds. It seems like the code found the first "fen":" in the text file and deleted everything before it.



        ...
        ...
        more code before this line..
        some code,"fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" code continues
        some code,"fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" code continues
        variant":{"key":"standard","name":"Standard","short":"Std"},"
        "initial "fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
        "fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0"}]}
        {"flipBoard":"Flip board","aiNameLevelAiLevel":"%1$s level %2$s","yourTurn":"Your turn","abortGame":"Abort
        .... code continues...
        « Last Edit: February 18, 2018, 05:21:25 PM by ironsurface17 »

        ironsurface17

          Topic Starter


          Greenhorn

          • Experience: Beginner
          • OS: Unknown
          Re: batch file to delete before character and after certain character
          « Reply #7 on: February 18, 2018, 05:21:39 PM »
          sorry!
          « Last Edit: February 18, 2018, 05:50:22 PM by ironsurface17 »

          patio

          • Moderator


          • Genius
          • Maud' Dib
          • Thanked: 1686
            • Yes
          • Experience: Beginner
          • OS: Windows 7
          Re: batch file to delete before character and after certain character
          « Reply #8 on: February 18, 2018, 05:26:19 PM »
          Why are you reapetedly quoting replies ? ?...juast curious...
             
           
          " Anyone who goes to a psychiatrist should have his head examined. "

          Hackoo



            Intermediate
          • Thanked: 12
          • Experience: Expert
          • OS: Windows 10
          Re: batch file to delete before character and after certain character
          « Reply #9 on: February 19, 2018, 12:21:50 AM »
          I'm still can't understand your aim  ???
          Can you join your html text (Data) in attachements and explain more your purpose You want to extract or replace ???
          With the batch that i posted it will extract and copy only those lines :

          Quote
          rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" code continues
          rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" code continues
          rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
          rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0

          ironsurface17

            Topic Starter


            Greenhorn

            • Experience: Beginner
            • OS: Unknown
            Re: batch file to delete before character and after certain character
            « Reply #10 on: February 19, 2018, 03:44:17 PM »
            So what I am trying to extract is a chess code that determines position of the pieces as shown in the bolded characters (those bolded characters are chess position). I want to be able to extract the last position of the chess board which correlates with the bold text in the html code. And I do apologize, I should have been more clear on what I am trying to do. So I have posted the semi complete code below. I am trying to copy or extract the bolded characters into a new text file and delete all the other words and characters and after the script is done, the new text file should only have the last chess position in it or the bolded characters. Each of the lines that start with {"ply has a chess position in it marked after fen":"  and ends with "}.  So I just want the last postion which is in the bolded characters. These characters can change based on what moves on the chess board is played, but the over all code does not change. So as long as we can delete everything after "}]},  and before fen":" on the last line that would be good.


            {"id":20,"name":"started"},"createdAt":1519074805967,"lastMove":"d7b5","opening":{"eco":"A49","name":"Indian Game: Przepiorka Variation","ply":5}},"clock":{"running":true,"initial":180,"increment":0,"white":126.26,"black":120.08,"emerg":22,"moretime":15},"correspondence":null,"player":{"color":"white","name":null,"user":{"id":"chessbrahs","username":"chessbrahs","online":true,"perfs":{"blitz":{"games":422,"rating":2680,"rd":72,"prog":-15}},"title":"GM","language":"en-GB","profile":{"country":"CA"}},"rating":2680,"version":50,"spectator":true},"opponent":{"color":"black","name":null,"user":{"id":"norba94","username":"norba94","online":true,"perfs":{"blitz":{"games":26725,"rating":2302,"rd":58,"prog":21}},"language":"hu"},"rating":2302},"orientation":"white","url":{"socket":"/kpGSyZr1/white/socket","round":"/kpGSyZr1/white"},"pref":{"animationDuration":100,"coords":2,"replay":2,"clockTenths":1,"clockBar":true,"highlight":true,"destination":true,"rookCastle":true,"showCaptured":true},"evalPut":true,"userTv":{"ply":37,"uci":"c3d5","san":"Nd5","fen":"1qrr2k1/1p1bppbp/p2p2p1/P2Nn3/1n1NP3/6PP/1PP2PB1/R1BQR1K1 b - - 10 19"},{"ply":38,"uci":"b4d5","san":"Nxd5","fen":"1qrr2k1/1p1bppbp/p2p2p1/P2nn3/3NP3/6PP/1PP2PB1/R1BQR1K1 w - - 0 20"},{"ply":39,"uci":"e4d5","san":"exd5","fen":"1qrr2k1/1p1bppbp/p2p2p1/P2Pn3/3N4/6PP/1PP2PB1/R1BQR1K1 b - - 0 20"},{"ply":40,"uci":"d8e8","san":"Re8","fen":"1qr1r1k1/1p1bppbp/p2p2p1/P2Pn3/3N4/6PP/1PP2PB1/R1BQR1K1 w - - 1 21"},{"ply":41,"uci":"c2c3","san":"c3","fen":"1qr1r1k1/1p1bppbp/p2p2p1/P2Pn3/3N4/2P3PP/1P3PB1/R1BQR1K1 b - - 0 21"},{"ply":42,"uci":"e5c4","san":"Nc4","fen":"1qr1r1k1/1p1bppbp/p2p2p1/P2P4/2nN4/2P3PP/1P3PB1/R1BQR1K1 w - - 1 22"},{"ply":43,"uci":"g1h2","san":"Kh2","fen":"1qr1r1k1/1p1bppbp/p2p2p1/P2P4/2nN4/2P3PP/1P3PBK/R1BQR3 b - - 2 22"},{"ply":44,"uci":"b8c7","san":"Qc7","fen":"2r1r1k1/1pqbppbp/p2p2p1/P2P4/2nN4/2P3PP/1P3PBK/R1BQR3 w - - 3 23"},{"ply":45,"uci":"b2b4","san":"b4","fen":"2r1r1k1/1pqbppbp/p2p2p1/P2P4/1PnN4/2P3PP/5PBK/R1BQR3 b - - 0 23"},{"ply":46,"uci":"c4e5","san":"Ne5","fen":"2r1r1k1/1pqbppbp/p2p2p1/P2Pn3/1P1N4/2P3PP/5PBK/R1BQR3 w - - 1 24"},{"ply":47,"uci":"c1d2","san":"Bd2","fen":"2r1r1k1/1pqbppbp/p2p2p1/P2Pn3/1P1N4/2P3PP/3B1PBK/R2QR3 b - - 2 24"},{"ply":48,"uci":"e5c4","san":"Nc4","fen":"2r1r1k1/1pqbppbp/p2p2p1/P2P4/1PnN4/2P3PP/3B1PBK/R2QR3 w - - 3 25"},{"ply":49,"uci":"d2g5","san":"Bg5","fen":"2r1r1k1/1pqbppbp/p2p2p1/P2P2B1/1PnN4/2P3PP/5PBK/R2QR3 b - - 4 25"},  {"ply":50,"uci":"d7b5","san":"Bb5","fen":"2r1r1k1/1pq1ppbp/p2p2p1/Pb1P2B1/1PnN4/2P3PP/5PBK/R2QR3 w - - 5 26"}]}, <--just need the bolded stuff and delete everything before and everything after the bold i dont need any other position or characters
            i18n:
            {"flipBoard":"Flip board","aiNameLevelAiLevel":"%1$s level %2$s","yourTurn":"Your turn","abortGame":"Abort game","proposeATakeback":"Propose a takeback","offerDraw":"Offer draw","resign":"Resign","opponentLeftChoices":"The other player may have left the game. You can claim victory, call the game a draw, or wait.","forceResignation":"Claim victory","forceDraw":"Call draw","threefoldRepetition":"Threefold repetition","claimADraw":"Claim a draw","drawOfferSent":"Draw offer sent","cancel":"Cancel","yourOpponentOffersADraw":"Your opponent offers a draw","accept":"Accept","decline":"Decline","takebackPropositionSent":"Takeback sent","yourOpponentProposesATakeback":"Your opponent proposes a takeback","thisPlayerUsesChessComputerAssistance":"This player uses chess computer assistance","gameAborted":"Game aborted","checkmate":"Checkmate","whiteResigned":"White

            Hackoo



              Intermediate
            • Thanked: 12
            • Experience: Expert
            • OS: Windows 10
            Re: batch file to delete before character and after certain character
            « Reply #11 on: February 19, 2018, 05:06:41 PM »
            If your data looks like this one :
            Code: [Select]
            {
               "id":20,
               "name":"started"
            },
            "createdAt":1519074805967,
            "lastMove":"d7b5",
            "opening":{
               "eco":"A49",
               "name":"Indian Game: Przepiorka Variation",
               "ply":5
            }
            },
            "clock":{
            "running":true,
            "initial":180,
            "increment":0,
            "white":126.26,
            "black":120.08,
            "emerg":22,
            "moretime":15
            },
            "correspondence":null,
            "player":{
            "color":"white",
            "name":null,
            "user":{
               "id":"chessbrahs",
               "username":"chessbrahs",
               "online":true,
               "perfs":{
                  "blitz":{
                     "games":422,
                     "rating":2680,
                     "rd":72,
                     "prog":-15
                  }
               },
               "title":"GM",
               "language":"en-GB",
               "profile":{
                  "country":"CA"
               }
            },
            "rating":2680,
            "version":50,
            "spectator":true
            },
            "opponent":{
            "color":"black",
            "name":null,
            "user":{
               "id":"norba94",
               "username":"norba94",
               "online":true,
               "perfs":{
                  "blitz":{
                     "games":26725,
                     "rating":2302,
                     "rd":58,
                     "prog":21
                  }
               },
               "language":"hu"
            },
            "rating":2302
            },
            "orientation":"white",
            "url":{
            "socket":"/kpGSyZr1/white/socket",
            "round":"/kpGSyZr1/white"
            },
            "pref":{
            "animationDuration":100,
            "coords":2,
            "replay":2,
            "clockTenths":1,
            "clockBar":true,
            "highlight":true,
            "destination":true,
            "rookCastle":true,
            "showCaptured":true
            },
            "evalPut":true,
            "userTv":{
            "ply":37,
            "uci":"c3d5",
            "san":"Nd5",
            "fen":"1qrr2k1/1p1bppbp/p2p2p1/P2Nn3/1n1NP3/6PP/1PP2PB1/R1BQR1K1 b - - 10 19"
            },
            {
            "ply":38,
            "uci":"b4d5",
            "san":"Nxd5",
            "fen":"1qrr2k1/1p1bppbp/p2p2p1/P2nn3/3NP3/6PP/1PP2PB1/R1BQR1K1 w - - 0 20"
            },
            {
            "ply":39,
            "uci":"e4d5",
            "san":"exd5",
            "fen":"1qrr2k1/1p1bppbp/p2p2p1/P2Pn3/3N4/6PP/1PP2PB1/R1BQR1K1 b - - 0 20"
            },
            {
            "ply":40,
            "uci":"d8e8",
            "san":"Re8",
            "fen":"1qr1r1k1/1p1bppbp/p2p2p1/P2Pn3/3N4/6PP/1PP2PB1/R1BQR1K1 w - - 1 21"
            },
            {
            "ply":41,
            "uci":"c2c3",
            "san":"c3",
            "fen":"1qr1r1k1/1p1bppbp/p2p2p1/P2Pn3/3N4/2P3PP/1P3PB1/R1BQR1K1 b - - 0 21"
            },
            {
            "ply":42,
            "uci":"e5c4",
            "san":"Nc4",
            "fen":"1qr1r1k1/1p1bppbp/p2p2p1/P2P4/2nN4/2P3PP/1P3PB1/R1BQR1K1 w - - 1 22"
            },
            {
            "ply":43,
            "uci":"g1h2",
            "san":"Kh2",
            "fen":"1qr1r1k1/1p1bppbp/p2p2p1/P2P4/2nN4/2P3PP/1P3PBK/R1BQR3 b - - 2 22"
            },
            {
            "ply":44,
            "uci":"b8c7",
            "san":"Qc7",
            "fen":"2r1r1k1/1pqbppbp/p2p2p1/P2P4/2nN4/2P3PP/1P3PBK/R1BQR3 w - - 3 23"
            },
            {
            "ply":45,
            "uci":"b2b4",
            "san":"b4",
            "fen":"2r1r1k1/1pqbppbp/p2p2p1/P2P4/1PnN4/2P3PP/5PBK/R1BQR3 b - - 0 23"
            },
            {
            "ply":46,
            "uci":"c4e5",
            "san":"Ne5",
            "fen":"2r1r1k1/1pqbppbp/p2p2p1/P2Pn3/1P1N4/2P3PP/5PBK/R1BQR3 w - - 1 24"
            },
            {
            "ply":47,
            "uci":"c1d2",
            "san":"Bd2",
            "fen":"2r1r1k1/1pqbppbp/p2p2p1/P2Pn3/1P1N4/2P3PP/3B1PBK/R2QR3 b - - 2 24"
            },
            {
            "ply":48,
            "uci":"e5c4",
            "san":"Nc4",
            "fen":"2r1r1k1/1pqbppbp/p2p2p1/P2P4/1PnN4/2P3PP/3B1PBK/R2QR3 w - - 3 25"
            },
            {
            "ply":49,
            "uci":"d2g5",
            "san":"Bg5",
            "fen":"2r1r1k1/1pqbppbp/p2p2p1/P2P2B1/1PnN4/2P3PP/5PBK/R2QR3 b - - 4 25"
            },
            {
            "ply":50,
            "uci":"d7b5",
            "san":"Bb5",
            "fen":"2r1r1k1/1pq1ppbp/p2p2p1/Pb1P2B1/1PnN4/2P3PP/5PBK/R2QR3 w - - 5 26"
            }
            ]
            },
            i18n:{
            "flipBoard":"Flip board",
            "aiNameLevelAiLevel":"%1$s level %2$s",
            "yourTurn":"Your turn",
            "abortGame":"Abort game",
            "proposeATakeback":"Propose a takeback",
            "offerDraw":"Offer draw",
            "resign":"Resign",
            "opponentLeftChoices":"The other player may have left the game. You can claim victory, call the game a draw, or wait.",
            "forceResignation":"Claim victory",
            "forceDraw":"Call draw",
            "threefoldRepetition":"Threefold repetition",
            "claimADraw":"Claim a draw",
            "drawOfferSent":"Draw offer sent",
            "cancel":"Cancel",
            "yourOpponentOffersADraw":"Your opponent offers a draw",
            "accept":"Accept",
            "decline":"Decline",
            "takebackPropositionSent":"Takeback sent",
            "yourOpponentProposesATakeback":"Your opponent proposes a takeback",
            "thisPlayerUsesChessComputerAssistance":"This player uses chess computer assistance",
            "gameAborted":"Game aborted",
            "checkmate":"Checkmate",
            "whiteResigned":"White

            So, with this batch script, you can get this line : 2r1r1k1/1pq1ppbp/p2p2p1/Pb1P2B1/1PnN4/2P3PP/5PBK/R2QR3 w - - 5 26

            Code: [Select]
            @echo off
            Set "Tmpvbs=%temp%\Tmpvbs.vbs"
            Set "InputFile=file.txt"
            Set "TmpFile=TmpFile.tmp"
            Set "OutPutFile=file2.txt"
            Call :Extract "%InputFile%" "%TmpFile%"
            for /F "delims=" %%a in ('Type "%TmpFile%"') do (
               echo %%a
            )>"%OutPutFile%"
            Del "%TmpFile%"
            Start "" "%OutPutFile%" & exit
            ::****************************************************
            :Extract <InputData> <OutPutData>
            (
            echo Data = WScript.StdIn.ReadAll
            echo Data = Extract(Data,"\b[^""fen:](\w*)\/(\w*).*\b"^)
            echo WScript.StdOut.WriteLine Data
            echo '************************************************
            echo Function Extract(Data,Pattern^)
            echo    Dim oRE,oMatches,Match,Line
            echo    set oRE = New RegExp
            echo    oRE.IgnoreCase = True
            echo    oRE.Global = True
            echo    oRE.Pattern = Pattern
            echo    set oMatches = oRE.Execute(Data^)
            echo    If not isEmpty(oMatches^) then
            echo        For Each Match in oMatches 
            echo            Line = Line ^& Match.Value ^& vbcrlf
            echo        Next
            echo        Extract = Line
            echo    End if
            echo End Function
            echo '************************************************
            )>"%Tmpvbs%"
            cscript /nologo "%Tmpvbs%" < "%~1" > "%~2"
            If Exist "%Tmpvbs%" Del "%Tmpvbs%"
            exit /b
            ::****************************************************

            ironsurface17

              Topic Starter


              Greenhorn

              • Experience: Beginner
              • OS: Unknown
              Re: batch file to delete before character and after certain character
              « Reply #12 on: February 19, 2018, 06:01:14 PM »
              Thanks. That works only if the lines start with "fen":" ;however, the text file I got the fen is in the middle of the line.

              Squashman



                Specialist
              • Thanked: 119
              • Experience: Experienced
              • OS: Other
              Re: batch file to delete before character and after certain character
              « Reply #13 on: February 21, 2018, 11:49:47 AM »
              Any way you could provide a better real world example.  I am having a hard time understanding the format of the file itself.

              Salmon Trout



                Genius

                Thanked: 925
                • Yes
              • Computer: Specs
              • Experience: Experienced
              • OS: Other
              Re: batch file to delete before character and after certain character
              « Reply #14 on: February 21, 2018, 11:55:21 AM »
              ....
              « Last Edit: February 21, 2018, 12:08:34 PM by Salmon Trout »

              Geek-9pm


                Mastermind
              • Geek After Dark
              • Thanked: 972
                • Gekk9pm bnlog
              • Certifications: List
              • Computer: Specs
              • Experience: Expert
              • OS: Windows XP
              Re: batch file to delete before character and after certain character
              « Reply #15 on: February 21, 2018, 12:11:43 PM »
              Any way you could provide a better real world example.  I am having a hard time understanding the format of the file itself.
              ... me too  ???

              ironsurface17,
              Can you explain why this is important?
              An explanation wold make the idea clear and would motivate others to help.
              Sometimes explaining a task reveals a detail that was overlooked.  :)