GwydionFR
GwydionFR

Reputation: 787

txt file with splitted lines in batch

I am working on a batch script which extract some datas of some lines.

I curently read my file using:

for /l %%x in (1, 1, !number_of_equipements!) do (
    set x=%%i
    ...processing...
)

But sometimes in my file, I have splitted lines like this one:

Cisco Web Usage Controls - Web Categorization Prefix Filters: 1438235190 (Thu 
Jul 30 06:00:38 2015)

And I need to grab the date to do some calculations and unfortunately my x variable will contain the first line and the second line in separated iterations.

What is the best to do ? I have thought about re-writting the entire file with a certain condition, or do exceptions depending on the line my script is reading, but it is all dirty. Is there any good way to do this ? Thank you in advance for your propositions.

Upvotes: 0

Views: 39

Answers (2)

GwydionFR
GwydionFR

Reputation: 787

I have modified like this to handle lines with no parenthesis:

>fixed.log (
for /f "usebackq delims=" %%I in ("%file%") do (
    if defined prev (
        set "line=!prev! %%I"
        set "prev="
        echo(!line!
    ) else (
        set "line=%%I"
        if "!line!" neq "!line:(=!" (
            if "!line!" equ "!line:)=!" (
                set "prev=!line!"
            ) else (
                echo(%%I
            )
        ) else (
            echo(%%I
        )
    )
)
)

Therefore, the copied line still remains and i can't figure out how to avoid that.... But it works like a charm, thank you a lot !

Upvotes: 0

rojo
rojo

Reputation: 24466

You could check whether a line contains ( but does not contain ); and if both conditions are true, join the second line with the previous. Something like:

@echo off
setlocal enabledelayedexpansion

set "file=whatever.log"

>fixed.log (
    for /f "usebackq delims=" %%I in ("%whatever%") do (
        if defined prev (
            set "line=!prev! %%I"
            set "prev="
            echo(!line!
        ) else (
            set "line=%%I"
            if "!line!" neq "!line:(=!" if "!line!" equ "!line:)=!" (
                set "prev=!line!"
            ) else (
                echo(%%I
            )
        )
    )
)

... and then scrape the dates out of the fixed text file as you would have if the line wraps weren't messing things up.

I haven't tested this. Your mileage may vary.

Upvotes: 2

Related Questions