Skip navigation

JSI Tip 4805. How do I use the output of a program as input, if it displays extraneous information?


If a program displays data that you would like pipe to a file, and use it for subsequent input, you may need to strip extraneous information and/or skip the first n lines.

I have scripted SkipStrip.bat to perform this task. The syntax is:


SkipStrip InputFile OutputFile \[-\]Lines_to_Skip ExtractPosition1 ExtractLength1
                                               \[ExtractPosition2 ExtractLength2 ... ExtractPositionn ExtractLengthn\]
where:

InputFile   The input file.
OutputFile   The output file.
-   Optional negative sign to cause the output file to be in CSV (Comma Separated Values) format. Each extracted field will be quoted.
Lines_to_Skip   The number of leading records to skip. The range is 0-number.
ExtractPosition1   The position, starting at 0, of the first field to be extracted.
ExtractLength1   The length of the first field to be extracted.
ExtractPosition2   The position, starting at 0, of the second field to be extracted.
ExtractLength2   The length of the second field to be extracted.
ExtractPosition1   The position, starting at 0, of the nth field to be extracted.
ExtractLength1   The length of the nth field to be extracted.

NOTE: The 'length' fields may NOT be zero (0).

NOTE: Due to limitation of the CMD processor, control symbols (!>), and un-paired " marks, in the input file causes problems. The script attempts to report these lines, as well as lines that contain nul data at the extraction positions.

Example:

To generate a regular file from an input file that contains:

** Program.exe output **
Version 2.5
User name=test1                    User Full Name=John Doe
User name=test name with space     User Full Name=Jane Smith

SkipStrip C:\Folder1\inputfile.txt C:\folder2\outputfile.txt 2 10 25 50 256

The output file would contain:

test1                    John Doe
test name with space     Jane Smith

To generate a CSV file:

SkipStrip C:\Folder1\inputfile.txt C:\folder2\outputfile.csv -2 10 25 50 999

The output file would contain:

"test1","John Doe"
"test name with space","Jane Smith"
SkipStrip.bat contains:
@echo off
setlocal ENABLEDELAYEDEXPANSION
if \{%5\}

\{\} goto syntax if not exist %1 goto syntax set of=%2 if /i %1 EQU %of% goto syntax set /a skip=%3 if not %skip% EQU %3 goto syntax set wrk=%3 set csv=N if "%wrk:~0,1%" EQU "-" set csv=Y&set /a skip=0 - %skip% set /a stripp=%4 if not %stripp% EQU %4 goto syntax set /a stripl=%5 if not %stripl% EQU %5 goto syntax if %stripl% EQU 0 goto syntax if exist %of% del /q /f %of% set /a cnt=%skip% set fl=%1 if %skip% EQU 0 ( for /f "tokens=*" %%a in ('type %fl%') do set ln=%%a&call :str %* ) ELSE ( for /f "tokens=* skip=%skip%" %%a in ('type %fl%') do set ln=%%a&call :str %* ) if not exist %of% @echo.>%of% endlocal goto :EOF :syntax @echo Syntax: SkipStrip input output skip strippos striplen \[strippos2 striplen2 .....stripposn striplenn\] endlocal goto :EOF :str set /a cnt=%cnt% + 1 set ou= :str1 call :str3 %4 %5>nul 2>&1 if \{!xx!\}

\{\} @echo command symbols or nul characters in line %cnt% if "%csv%" EQU "N" goto nocsv set yy=!xx!#@# :space set zz=%yy% set yy=%yy: #@#=#@#% set yy=%yy: #@#=#@#% set yy=%yy: #@#=#@#% if not "%yy%" EQU "%zz%" goto space set xx=%yy:#@#=% if not \{!ou!\}

\{\} set ou=!ou!, set ou=!ou!"!xx!" goto common :nocsv set ou=!ou!!xx! :common set xx= shift shift if \{%4\}

\{\} goto str2 set /a stripp=%4 if not %stripp% EQU %4 goto syntax set /a stripl=%5 if not %stripl% EQU %5 goto syntax if %stripl% EQU 0 goto syntax goto str1 :str2 @echo !ou!>>%of% goto :EOF :str3 for /f "Tokens=*" %%b in ('@echo !!ln:~%1^,%2!!') do set xx=%%b


Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish