extract data from batch file
-
30 Mei 2012 9:19for example,
=====================================================================================================================================
* *
* * * * * * * * *
User information . . . . . : : User name . . . . . . . . . : ABAX * *
User text . . . . . . . . . : Beatrice Raffaele *
Begin menu . . . . . . . . : *NONE
Group jobs . . . . . . . . : 15
End group jobs . . . . . . : *NO
Command line on menus . . . : *NO
Attention-key program . . . : *ACTIVE
Reference user . . . . . . : OPERATOR
====================================================================================================================================
XXXXXXXX |
XXXXXXXXX | --- other information not relevant
XXXXXXXXXX |=====================================================================================================================================
* *
* * * * * * * * *
User information . . . . . : : User name . . . . . . . . . : ABCG * *
User text . . . . . . . . . : Barchiesi Giancarlo *
Begin menu . . . . . . . . : *NONE
Group jobs . . . . . . . . : 15
End group jobs . . . . . . : *NO
Command line on menus . . . : *NO
Attention-key program . . . : *ACTIVE
Reference user . . . . . . : OPERATOR
====================================================================================================================================XXXXXXXX |
XXXXXXXXX | --- other information not relevant
XXXXXXXXXX |
=====================================================================================================================================
* *
* * * * * * * * *
User information . . . . . : : User name . . . . . . . . . : ABEF * *
User text . . . . . . . . . : Fabio Besomi*
Begin menu . . . . . . . . : *NONE
Group jobs . . . . . . . . : 15
End group jobs . . . . . . : *NO
Command line on menus . . . : *NO
Attention-key program . . . : *ACTIVE
ference user . . . . . . : OPERATOR
====================================================================================================================================
Semua Balasan
-
30 Mei 2012 9:23
continue from previous post..
I have the source file like above (there has over 1k of entry with similar structure)
I need to extract the data out and put into the a csv format.
the end result should be like following:
User name ,User text, Begin menu, Group jobs ,End group jobs,Command line on menus ,Attention-key ....
ABAX ,Beatrice Raffaele, *NONE, 15,NO,NO,ACTIVE....
ABCG, Barchiesi Giancarlo, *NONE,...............so basically this is the intention..
@echo off>%temp%\input.txt
cls
setlocal enabledelayedexpansion
for /f "tokens=*" %%1 in (input.txt) do (
set inline=%%1& set inline=!inline:.=!
if not "!inline:~0,3!" equ "* *" (
if not "!inline:~0,3!" equ "===" (
echo !inline!>>%temp%\input.txt
)
)
)
i got into this and kinda of stuck...
thanks for helping!!
-
30 Mei 2012 11:20
You need a clear structure in order to do this. Here is a method that will extract the strings you need.
@echo off
SetLocal EnableDelayedExpansion
set counter=0
if exist output.txt del output.txtfor /F "delims=" %%a in ('type test.txt') do (
set Line=%%a
if /i "!Line:~0,16!"=="User Information" (set counter=1) else (set /a counter=!counter!+1)
if !counter! LEQ 8 call :ProcessLine
)
goto :eof
:ProcessLine
if !counter!==1 for /F "tokens=4 delims=:" %%x in ('echo !Line!') do set Name=%%x
if !counter!==2 for /F "tokens=2 delims=:" %%x in ('echo !Line!') do set Text=%%x
etc.
if !counter!==8 for /F "tokens=2 delims=:" %%x in ('echo !Line!') do set Operator=%%x
if !counter!==8 echo "%Name%","%Text%", etc. >> output.txt- Ditandai sebagai Jawaban oleh IamMredMicrosoft Employee, Owner 07 Juni 2012 7:13
-
30 Mei 2012 21:18Moderator
Just for grins, I worked up this alternate approach ...
@echo off set Matches=User.name User.text Begin.menu Group.jobs End.group.jobs set Matches=%matches% Command.line.on.menus Attention-key.program Reference.user for /f "tokens=1-4 delims=:*" %%a in ('findstr "%Matches%"') do ( if not [%%d]==[] (call :ekho %%c %%d ) else (call :Loop %%b %%c) ) echo.%_out:~0,-1%&set "_out=" exit /b 0 :Ekho if defined _out echo.%_out:~0,-1%&set "_out=" shift & shift :Loop if [%1]==[.] (shift & goto :Loop) if not [%2]==[] (set "_out=%_out%%1 "< nul & shift) set "_out=%_out%%1,"< nul if not [%2]==[] shift & goto :Loop----------
It filters out unwanted lines and then loops through what is left to build the comma separated output records.
It is used at the command line by redirecting the input file into it and redirecting the output to another file, something like this ...
C:\somefolder>filter.cmd < input.txt > output.txt
Tom Lavedas
- Diedit oleh Tom LavedasModerator 30 Mei 2012 21:22 removed the need for a temp file
- Ditandai sebagai Jawaban oleh IamMredMicrosoft Employee, Owner 07 Juni 2012 7:13
-
30 Mei 2012 22:23
I do not believe the sample data. It looks like it came from a computer source but there are mistakes in it the cannot be explained.
Assuming it is computer generated and that teh representative text is absolutely correct teh this is a PowerShell solution:
cat junktext.txt | ForEach-Object -Begin {rv obj} -end {$obj} -process{ if($_ -match 'User Name\s\..*:(?<username>.*)'){ if($obj){$obj} $obj=New-Object PSObject -Property @{Username=$matches['username']} } if($_ -match 'User text\s\..*:(?<usertext>.*)'){ $obj|Add-Member -Name UserText -Value $matches['usertext'] -MemberType NoteProperty } if($_ -match '\s\sGroup jobs\s\..*:(?<groupjobs>.*)'){ $obj|Add-Member -Name Groupjobs -Value $matches['groupjobs'] -MemberType NoteProperty } if($_ -match 'End group jobs\s\..*:(?<endgroupjobs>.*)'){ $obj|Add-Member -Name EndGroupjobs -Value $matches['endgroupjobs'] -MemberType NoteProperty } if($_ -match 'Command line on menus\s\..*:(?<commandlineonmenus>.*)'){ $obj|Add-Member -Name CommandlineOnMenus -Value $matches['commandlineonmenus'] -MemberType NoteProperty } if($_ -match 'Reference user\s\..*:(?<referenceuser>.*)'){ $obj|Add-Member -Name ReferenceUser -Value $matches['referenceuser'] -MemberType NoteProperty } if($_ -match 'Reference user\s\..*:(?<AttentionKey>.*)'){ $obj|Add-Member -Name AttentionKey -Value $matches['AttentionKey'] -MemberType NoteProperty } }The plus of using PowerShell is that it is more flexible and much easier to change/edit. Using Regex makes the selection process extremely granular. Using capture groups makes text extraction and modification a breeze.
The above is very linear. It is posssible to state this all in one Regex with or [match1|match2|match3] which can be integrated with referential capture groups to generate a capture group named by what is detected thus allowing us to just use one Add-Member generalization that can generate all of the members as they are found.
I did this linearly because it is probably never going to be used.
I prefer reading PowerShell to 'batch'. 'batch is a '*****'!
¯\_(ツ)_/¯
- Diedit oleh jrvMicrosoft Community Contributor 30 Mei 2012 22:26
- Ditandai sebagai Jawaban oleh IamMredMicrosoft Employee, Owner 07 Juni 2012 7:13
-
31 Mei 2012 4:34
Hi guys,
Many thanks for the answer.. i understand the source file i provide has some problem...
hence i make a print-screen on desired input and output..
the input is like that
-
31 Mei 2012 4:39
i have circle it out the required output..
then the output shall be in csv format..
which is like this..
for header column part i am thinking whether we can do this..
echo User name,User text,Begin menu,Group jobs,End group jobs,Command ^ line on menus,Attention-key program,Reference user, Group I, Group I Description,Group II, Group II Description,Group III, Group III Description, Group IV, Group IV Description,Group V, Group V Description,Group VI, Group VI Description>outputfile.csv
Since i was told that it will have maximum 6 group..
However, for the record where reference user is "multiple" it will have extra column like i shown on previous post first picture... and the keyword is 'grp'.... , security info
meaning anything that is start from 'GRP'SECINFO put under group && 'anyword behind' put under group description
for example
Thanks for helping !!! appreciate a lot
- Diedit oleh cheeseng1229 31 Mei 2012 4:52
-
31 Mei 2012 5:37
Your print screens are absolutely useless. the best thing to do is to post a copy of your file to a shared ser5ver and then post the link,.
We will not build this for you. From the pictures it is clear you are not a technician and do not really understand what it is you are asking.
We all know what a CSV file is. The code I posted is a way to very easily create objects that can be output to CSV. It is your responsibility to understand this or any other code posted here. If you are not knowledgeable enough about WIndows to do this then I recommend finding a consultant to help you. We do not write scripts on demand. All code is intended to show you how you might proceed.
Start by reading the posting guidelines. http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/a0def745-4831-4de0-a040-63b63e7be7ae
¯\_(ツ)_/¯
- Ditandai sebagai Jawaban oleh IamMredMicrosoft Employee, Owner 07 Juni 2012 7:13
-
31 Mei 2012 5:46
It also appears fromyour notepad view that the file has many graphicas characters which you did not tell us about. This will make it nearly impossibe to use for data extraction.
Look closely at the line drawing boxes. They are random and will create a very big issue for extraction.
Look closely at the line drawing:
The pictures you posted are alos impossible to read because they are to compact. Pictures are no help when you need data to understand an issue.
¯\_(ツ)_/¯
- Ditandai sebagai Jawaban oleh IamMredMicrosoft Employee, Owner 07 Juni 2012 7:13
-
31 Mei 2012 6:37
Sorry, my friend, you blew it. Posting bad data when asking for help with your scripting project suggests you are not aware that respondents actually spend their time in order to help you solve your problem. Your best course of action might be to sit down and develop your own solution based on the replies you have received.- Ditandai sebagai Jawaban oleh IamMredMicrosoft Employee, Owner 07 Juni 2012 7:13