Announcement

Collapse
No announcement yet.

Help: input data to a string/remove the string leaving only the data using batch file

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

  • Help: input data to a string/remove the string leaving only the data using batch file

    Hello, I'm completely new to all of this so forgive my ignorance. I have read through the forums and while I think I found a similar request it wasn't quite right and my lack of knowledge means I couldn't tweak the batch file to make it work.

    I hope this makes sense.

    I have a text file (Listing.txt) which lists data and I need to separate the data I need from the other text/data around it.

    For example I have something that looks like this;

    R,T743,DATA1,SOMETHING,SOMETHING_ELSE
    R,T743,DATA2,SOMETHING,SOMETHING_ELSE
    R,T743,DATA3,SOMETHING,SOMETHING_ELSE

    And what I would like is a batch file I can run that will strip away everything I don't need which would be "R,T743," and ",SOMETHING, SOMETHING_ELSE" and leave me with;

    DATA1
    DATA2
    DATA3

    And then I need a second batch file that is capable of doing the opposite so using a text file (Numeration.txt) that has;

    DATA1
    DATA2
    DATA3

    Input the data I need in to this string, replacing XXX in each case.

    R,T743,XXX,SOMETHING,SOMETHING_ELSE
    R,T743,XXX,SOMETHING,SOMETHING_ELSE
    R,T743,XXX,SOMETHING,SOMETHING_ELSE

    Both would need to work regardless of how many lines of data I have, it ranges from 1 up to 2000+. We're using XP Pro if this makes any difference.

    We're currently able to create this effect using excel to import the text file then create sections and then copy and paste the bits we need back in to a text file and remove any gaps that appear. I believe this to be an archaic way of doing things.

    Any help would be greatly appreciated.

  • #2
    Re: Help: input data to a string/remove the string leaving only the data using batch

    Originally posted by Slow View Post
    I have a text file Listing.txt which lists data;
    R,T743,DATA1,SOMETHING,SOMETHING_ELSE
    R,T743,DATA2,SOMETHING,SOMETHING_ELSE
    R,T743,DATA3,SOMETHING,SOMETHING_ELSE

    I need to separate the data from the other text/data around it.
    leave me with a file Numeration.txt;
    DATA1
    DATA2
    DATA3

    And then I need a second batch file that is capable of doing the opposite, so using the text file Numeration.txt for replacing XXX in each case;
    R,T743,XXX,SOMETHING,SOMETHING_ELSE
    R,T743,XXX,SOMETHING,SOMETHING_ELSE
    R,T743,XXX,SOMETHING,SOMETHING_ELSE
    You can try something like (can be split in two batches if you like):
    Code:
    @echo off
    
    call Set "in-File=Listing.txt"
    call Set "new-File=Listing2.txt"
    call Set "tmp-File=Numeration.txt"
    
    >"%tmp-File%" Call:strip
    rem pause
    >"%new-File%" Call:redo
    
    goto:EOF
    :strip
    For /f "tokens=3 delims=," %%* in ('Findstr /rc:"^.*,.*,.*,.*,.*$" "%in-File%"') do echo.%%*
    exit /b 0
    
    :redo
    For /f "usebackq tokens=*" %%* in ("%tmp-File%") do echo.R,T743,%%*,SOMETHING,SOMETHING_ELSE
    exit /b 0
    /Rems


    edit,
    However, if you meant replacing Datann in Listing.txt with a static value for data, then the 2nd batch would be like;
    Code:
    call Set "in-File=Listing.txt"
    call Set "new-File=Listing2.txt"
    
    >"%new-File%" Call:replace
    
    goto:EOF
    :replace
    For /f "tokens=1-5 delims=," %%^1 in ('Findstr /rc:"^.*,.*,.*,.*,.*$" "%in-File%"') do echo.%%^1,%%^2,newData2,%%^4,%%^5
    exit /b 0
    Last edited by Rems; 23rd September 2011, 13:28.

    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

    Working...
    X