Announcement

Collapse
No announcement yet.

Is it possible to write a batch file that deletes unwanted characters in a text file?

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

  • Is it possible to write a batch file that deletes unwanted characters in a text file?

    IF I HAVE A TEXT FILE....
    Code:
    xxxxxxxxxxxxxxxxxx        BETTY SUE IS A GIRL
    XXXXXXXXXXXXXXXX        SPIDER MAN HAS SPIDEY SENSE
    ZZZZZZZZZZZZZZZZ        THE CAT IN THE HAT.....
    ETCETCETCETCETCE        AND THIS IS THE IMPORTANT STUFF...
    AND I WANTED to write a DOS batch file to open the text file and delete the
    first part of the text file per each line...

    so that it now looks like this:
    Code:
    BETTY SUE IS A GIRL
    SPIDER MAN HAS SPIDEY SENSE
    THE CAT IN THE HAT.....
    AND THIS IS THE IMPORTANT STUFF...
    WITH THE BEGINNING TEXT OF EACH LINE ALL GONE.

    NOTE: THE TEXT I WANT TO KEEP ALWAYS STARTS AT THE SAME PLACE ON EACH LINE.


    HOW DO I GO ABOUT IT?
    Last edited by Rems; 22nd May 2012, 18:40.

  • #2
    Re: Is it possible to write a batch file that deletes unwanted characters in a text f

    It would be simpler to use VBA programming in Word to do what you want, I think.

    Simply loop through each line of your text file, select x-number of characters & spaces, delete, then repeat to end of file.

    If you record a macro, doing this manually for 2 or 3 lines, you'll get the VBA steps you need more than once. Wrap one set of lines in a DO UNTIL loop and you're done.
    *RicklesP*
    MSCA (2003/XP), Security+, CCNA

    ** Remember: credit where credit is due, and reputation points as appropriate **

    Comment


    • #3
      Re: Is it possible to write a batch file that deletes unwanted characters in a text f

      Originally posted by ninjawoman View Post

      <...>
      I WANTED to write a DOS batch file to open the text file and delete the first part of the text file per each line...

      <...>
      THE TEXT I WANT TO KEEP ALWAYS STARTS AT THE SAME PLACE ON EACH LINE.


      You want a batch that removes the first 24 characters of each line.


      Sample of a simple batch (can have problems with some special characters in a text file)
      Code:
      @echo off
      Set "InputFile=myfile.txt"
      Set "OutputFile=newfile.txt"
      
      setLocal EnableDelayedExpansion > "%OutputFile%"
      
      for /f "usebackq tokens=* delims= " %%a in ("%InputFile%") do (
      set s=%%a
      >> "%OutputFile%" echo.!s:~24!
      )

      Or, use SED (free GNU SED for Windows) if there could be special characters used in the text files. Download gnu sed (gsed) for Windows, free for download!

      Expand the download and copy the 4 files below from sed's 'bin' folder to the batch's folder
      sed.exe
      regex2.dll
      libiconv2.dll
      libintl3.dll

      SED script sample:
      Code:
      @echo off
      Set "InputFile=myfile.txt"
      Set "OutputFile=newfile.txt"
      
      >"%OutputFile%" sed.exe -e "/^.\{25\}/!d;s/^.\{24\}//g"  "%InputFile%"
      /Rems
      Last edited by Rems; 22nd May 2012, 21:59.

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        Re: Is it possible to write a batch file that deletes unwanted characters in a text f

        or for example with powershell, found it in a comment in this post:
        http://blogs.technet.com/b/heyscript...text-file.aspx

        Code:
        $lines = @()
        $text = Get-Content -Path .\chapter.txt
        $text | %{ $lines += $_.SubString(10,$_.Length-10) }
        Set-Content -Path .\NewChapter.txt -Value $lines
        Couldn't test it right now, but looks fine to me.
        Marcel
        Technical Consultant
        Netherlands
        http://www.phetios.com
        http://blog.nessus.nl

        MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
        "No matter how secure, there is always the human factor."

        "Enjoy life today, tomorrow may never come."
        "If you're going through hell, keep going. ~Winston Churchill"

        Comment


        • #5
          Re: Is it possible to write a batch file that deletes unwanted characters in a text f

          GREAT ONE THE NINJA WOMAN IS IN YOUR DEBT..........

          I TRIED THIS AND IT WORKED FASTER THAN NINJA STARS FLY

          THANK YOU Rems VERY MUCH FOR YOUR WISDOM

          IT'S EXACTLY WHAT I NEEDED

          Comment

          Working...
          X