Results 1 to 10 of 10
  1. #1
    New Lounger
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Search & Replace a text file with batch script

    Hi,
    I am stuck with a problem,
    I need to search and replace a value in a text file every 24 hrs. I know how to do the scheduling but I don't know how to write a batch file to do it.

    This is a typical line in the text file:

    2014-06-30 01 M052013-N_1001-MX4501 95893,09

    The first group is DATE, the second is HOUR, the third is a KEY and the fourth is a measured VALUE. The groups are TAB delimited.
    What I need to do is to scan the HOUR group and if it is 00 it should be changed to 24 and then the file should be saved.

    Help very much appreciated!

    Regards
    Marcus

  2. Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Platinum Lounger
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    3,619
    Thanks
    7
    Thanked 231 Times in 219 Posts
    I assume you need to search the same file for the value "00" and change it to "24", but you only want to look in a particular location. To do this you really need to use a Regular Expression.

    If you have Windows 7 or above you can use PowerShell.
    You could use FNR and supply the Regex.
    You could also write you own in VBS.

    cheers, Paul

  4. #3
    New Lounger
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Paul,
    Thanks for the info and tip. Unfortunately my problem is that I am unable to create such script since I have no competence in this area.
    I have googled this and I have seen similar issues beeing solved with small batch script but I have been uable to adapt them due to my bad knowledege.

    Regards
    Marcus

  5. #4
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    2,827
    Thanks
    19
    Thanked 109 Times in 103 Posts
    Tab delimiting is probably going to be a problem, since BATch files dislike some special characters.
    Without the tabs it would be a fairly trivial task, using a technique rather like Basic Substr(inging).

    First my BATch file:
    Code:
    @echo off
    setlocal
    set source=$source.txt
    set target=$target.txt
    for /f "tokens=*" %%a in (%source%) do call :process "%%a"
    start "" notepad %target%
    endlocal
    goto :eof
    
    :process
    set line=%~1
    set front=%line:~0,11%
    set hours=%line:~11,2%
    set back=%line:~13%
    if "%hours%"=="00" set hours=24
    echo %front%%hours%%back%>>%target%
    goto :eof
    Then my test input file, where the fields are BLANK-separated:
    Code:
    2014-06-29 01 M052013-N_1001-MX4501 95893,09   this line had hours=01
    2014-06-30 24 M052013-N_1001-MX4501 95893,09   this line had hours=24
    2014-06-31 00 M052013-N_1001-MX4501 95893,09   this line had hours=00
    Finally the output file:
    Code:
    2014-06-29 01 M052013-N_1001-MX4501 95893,09   this line had hours=01
    2014-06-30 24 M052013-N_1001-MX4501 95893,09   this line had hours=24
    2014-06-31 24 M052013-N_1001-MX4501 95893,09   this line had hours=00
    (Interestingly, I don't think I've ever had to play with a tab-delimited text file, since the editor I used to use (IBM's PC-DOS 'E', of happy memory) would replace the tab by one or more blanks, as it thought fit!)

    The BATch file could be somewhat tidier, but what I'm doing should be visible...
    Last edited by BATcher; 2014-07-02 at 04:44. Reason: Reordered the code sections
    BATcher

    Dear Diary, today the Hundred Years War started ...

  6. #5
    Platinum Lounger
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    3,619
    Thanks
    7
    Thanked 231 Times in 219 Posts
    FNR lets you open the file and test your config, then it will generate the command line you need. All you need to do is confirm the changes.
    The find command would be "\t00\t" and the replace "\t24\t", without the quotes - this is equivalent to tab, double zero, tab and tab, twenty four, tab.
    Note: You need to tick the "Use escape characters" box and nothing else.

    cheers, Paul

  7. #6
    New Lounger
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    First of all a BIG Thank You for helping me here!

    BATcher,
    Your script works almoast perfect but there is something I don't understand. It doesn't accept this line:

    2014-06-30 03 M052013-N_1001-MX4501 95900,8

    Do you know why?

    Paul T,
    Thanks, I'll try this as well.

    Regards
    Marcus

  8. #7
    Platinum Lounger
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    3,619
    Thanks
    7
    Thanked 231 Times in 219 Posts
    What do you mean doesn't accept? The line looks OK, so we need to know what happens when it's encountered?
    Are you sure it's not the line following the one you've posted?

    cheers, Paul

  9. #8
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    2,827
    Thanks
    19
    Thanked 109 Times in 103 Posts
    I agree with Paul. Unless there's a very unusual character in that line, it should work OK.
    BATcher

    Dear Diary, today the Hundred Years War started ...

  10. #9
    New Lounger
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,
    Well I can't understand it either but it just doesn't work.
    Even if I type the line into a textfile myself and then parse the file it fails.

    I have prepared a BAT file with BATcher's code and a TEXT file with some sample data to be parsed.

    The file 00224.txt needs to be changed to 00224.BAT since I was not allowed to attach a BAT file.

    Again, I am greatfull for your help.

    Paul, I tried FNR and I got it to work so I actually have a solution to my problem.

    I just would like to understand WHY it doesn't work with BATcher's code.

    Regards
    Marcus
    Attached Files Attached Files

  11. #10
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    2,827
    Thanks
    19
    Thanked 109 Times in 103 Posts
    You haven't explained what you mean by "it doesn't accept this line". Do you mean the line gets ignored? Or the BATch script crashes? It seems very unusual, because you'd possibly expect problems on the first or last input lines.

    I'd suggest that to debug you run the BATch file again, having:
    • changed @echo off to @echo on (this means that everything gets displayed on the console)
    • added a PAUSE statement just before the final goto :eof (so you can see what happened for the current input line, press Enter to continue execution)
    BATcher

    Dear Diary, today the Hundred Years War started ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •