Solved Extract lines between two specified lines

August 2, 2013 at 15:47:54
Specs: Windows server 2008 R2
Hi,

I have a text file and would like to extract all the lines between two specified lines using windows batch scripting.

Line1: !FILE_FORMAT=ADS
Line2: !VERSION=1.0
.
.
LineX: 'Parent|Child|IsPrimary|********** (the line starts with ' and is long)
.
.
LineY: !PropertyArray=Cost Center (The lines starts with !)
.
.
LineZ.

I want to extract all the lines between LineX and LineY and output it to another file.

Regards,
Ragav.

message edited by ragavhere


See More: Extract lines between two specified lines

Report •


✔ Best Answer
August 6, 2013 at 11:32:28
Ok, I had to drop back and punt, but this seemed to work on your test set:
::======== begin batchscript
@echo off & setlocal enabledelayedexpansion
for /f "tokens=1 delims=[]" %%a in ('find /n "'Parent|Child"^<testzz') do @(
more +%%a testzz)>x
(for /f "tokens=*" %%a in (x) do (
set test=%%a
if "!test:~0,13!" equ "!PropertyArray" goto :eof
echo %%a
))>xout
::========= end batchscript


#1
August 2, 2013 at 17:54:18
What scripting language do want to use?

Report •

#2
August 2, 2013 at 18:27:38
I want windows batch scripting.

Regards,
Ragav.

message edited by ragavhere


Report •

#3
August 2, 2013 at 18:29:59
Yeh, that was also my first reaction. I went ahead with vbscript since the programming forum has been in the doldrums:
'======== begin vbscript
set fso=createobject("scripting.filesystemobject")
z=fso.opentextfile("testfile",1).readall
p=instr(z,vbcrlf&"'Parent|Child")
p0=instr(p+1,z,vbcrlf)
p2=instr(p0+1,z,vbcrlf&"!PropertyArray")
z=mid(z,p0,p2-p0)
wscript.echo z

'===== end vbscript
Oops! crossed in the mail, Ha ha!

message edited by nbrane


Report •

Related Solutions

#4
August 2, 2013 at 18:35:03
Thanks for your response. Sorry for not specifying what script I wanted. I want using windows batch scripting.

Regards,
Ragav.


Report •

#5
August 2, 2013 at 19:06:51
Ok, no problem. Here's one to try:
::========= begin batchscript
@for /f "tokens=1 delims=[]" %%a in ('find /n "'Parent|Child"^<testfile') do @(
more +%%a testfile |find /v "!PropertyArray" || goto :eof)>testout
::====== end batch

Report •

#6
August 2, 2013 at 19:07:27
Sorry, I don't do batch scripting. If you want to try Perl, I can probably do this in 1 line of code.

Report •

#7
August 2, 2013 at 21:41:44
We need to see a relevant chunk of the fuke.

=====================
M2 Golden-Triangle


Report •

#8
August 5, 2013 at 12:09:38
Hi nbrane,

I tried this code. It doesnt work as required. It just deletes the line starting with !PropertyArray=Cost Center and outputs the rest of the file. However it picks the starting('Parent|Child) correctly. I need to extract only the lines between the given two lines.

::========= begin batchscript
@for /f "tokens=1 delims=[]" %%a in ('find /n "'Parent|Child"^<D:\DEV\Test\Cost_Center.txt') do @(
more +%%a D:\DEV\Test\Cost_Center.txt |find /v "!PropertyArray=Cost Center" || goto :eof)>D:\DEV\Test\Cost_Center_Out.txt
::====== end batch

message edited by ragavhere


Report •

#9
August 5, 2013 at 18:02:23
Well, since it worked on my test file, obviously your file or your OS are not equivalant to what I tested with. So, back to M2's comment, please post some sample data to work with. I tested with winXP, from command-prompt not GUI.

Report •

#10
August 5, 2013 at 19:07:14
Hi,

I am running the batch script on windows server 2008 R2. Here is a piece of text from the file.

!FILE_FORMAT=ADS
!VERSION=1.0

!Section=Dimensions
'Name|DimensionClass|AllowDuplicatesInDimension|ApplySecurity|ASODimensionDataStorage|ASODimensionFormula|ASOMemberFormulaDuplicate|AttributeDataType|AutoGenId|BSODimensionDataStorage|BSODimensionFormula|BSOMemberFormulaDuplicate|CapexDensity|CapexPerfOrder|CommentDuplicate|Compression|ConsolidationDuplicate|CurrencyCategoryDuplicate|CurrencyConversionDuplicate|CurrencyNameDuplicate|DataStorageDuplicate|DefaultMember|DimDataStorage|DimensionAlias|DimensionDescription|DimensionHierarchyType|DimensionOwner|DimensionReaders|DimensionSize|DimensionSolveOrder|DimensionSortOrder|DimensionStorageType|DimensionWriters|DimTwoPassCalc|DimValidForCapex|DimValidForPlan1|DimValidForPlan2|DimValidForPlan3|DimValidForProject|DimValidForWorkforce|DisplayOption|DisplayOrder|EnumOrder1|EnumOrder2|EnumOrder3|EnumOrderCapex|EnumOrderProject|EnumOrderWF|FlowMetricDuplicate|FormatStringDuplicate|GridMissingLabelType|Increment|IsCurrencyPartition|IsPOVDimension|LevelUsageOfAggregationDuplicate|MaxAllowedPerApp|MemberSolveOrderDuplicate|MinAllowedPerApp|MissingLabel|NamedGeneration|NamedLevel|OutOfRangeName|Plan1Density|Plan1PerfOrder|Plan2Density|Plan2PerfOrder|Plan3Density|Plan3PerfOrder|POVDisplayOrder|PrimaryLevelWeightingDim|ProjectDensity|ProjectPerfOrder|SecondaryLevelWeightingDim|ShortName|SkipValueDuplicate|SmartListLabel|StartValue|SupportsNonUniqueMembers|SupportsSharedMembers|TimeBalanceDuplicate|TwoPassCalcDuplicate|UDADuplicate|UseForCurrency|UseForMapping|VarianceReportingDuplicate|WorkforceDensity|WorkforcePerfOrder
Cost Center|Entity|True|True|LabelOnly|||Text|False|LabelOnly|||Sparse|7||False|+||None||StoreData||NeverShare|Entity|Cost Center|Enabled|||Large|0|4|Sparse||False|True|True|True|True|True|True|Name|ID|0|0|0|0|0|0|False||DropDown|1|False|False||1|0|1|||||Sparse|7|Sparse|7|Sparse|7|0|Default||0|Default||None||1|False|True|None|False||None|True||Sparse|7

!Section=DimensionAssociations
'BaseDimension|Property|TargetDimension
Cost Center|Alias|Alias
Cost Center|BudgetOwnerAttribute|Budget Owner
Cost Center|UDA|UDA

!Members=Cost Center
'Name|AllowAdjFromChildren|AllowAdjs|ASOMemberFormula|BSOMemberFormula|BudgetOwnerAttribute|Comment|CurrencyName|DataType|Description|FormatString|HierarchyType|IsICP|LevelUsageOfAggregation|MemberSolveOrder|PrimaryLevelWeighting|SmartList|TwoPassCalc|UserDefined1|UserDefined2|UserDefined3
[None]|False|False||||||Unspecified|None|||N|Default|0|Default||False|||
NoMember|False|False||||||Unspecified|None|||N|Default|0|Default||False|||
Corporate Division|False|False||||||Unspecified||||N|Default|0|Default||False|||
Real Estate Division|False|False||||||Unspecified||||N|Default|0|Default||False|||

!Hierarchies=Cost Center
'Parent|Child|IsPrimary|ASOMemberDataStorage|BSOMemberDataStorage|CapexAggregation|Consolidation|DataStorage|FullyQualifiedSharedMember|MemberValidForCapex|MemberValidForPlan1|MemberValidForPlan2|MemberValidForPlan3|MemberValidForProject|MemberValidForWorkforce|Plan1Aggregation|Plan2Aggregation|Plan3Aggregation|ProjectAggregation|WorkforceAggregation|AllowAdjFromChildren|AllowAdjs|ASOMemberFormula|BSOMemberFormula|BudgetOwnerAttribute|Comment|CurrencyName|DataType|Description|FormatString|HierarchyType|IsICP|LevelUsageOfAggregation|MemberSolveOrder|PrimaryLevelWeighting|SmartList|TwoPassCalc|UserDefined1|UserDefined2|UserDefined3
#root|[None]|True|StoreData|StoreData|+|+|StoreData||True|True|True|True|True|True|+|+|+|+|+|False|False||||||Unspecified|None|||N|Default|0|Default||False|||
#root|NoMember|True|StoreData|StoreData|+|~|StoreData||True|True|True|True|True|True|+|+|+|+|+|False|False||||||Unspecified|None|||N|Default|0|Default||False|||
#root|Department|True|StoreData|StoreData|+|+|StoreData||True|True|True|True|True|True|+|+|+|+|+|False|False||||||Unspecified||||N|Default|0|Default||False|||
Department|ALL_DEPTS|True|StoreData|StoreData|+|+|StoreData||True|True|True|True|True|True|+|+|+|+|+|False|False||||||Unspecified||||N|Default|0|Default||False|||
ALL_DEPTS|No Cost Center|True|StoreData|StoreData|+|+|StoreData||True|True|True|True|True|True|+|+|+|+|+|False|False||||||Unspecified||||N|Default|0|Default||False|||
Total Portfolio Project Divisions|Corporate Division|True|StoreData|StoreData|+|+|StoreData||True|True|True|True|True|True|+|+|+|+|+|False|False||||||Unspecified||||N|Default|0|Default||False|||
Total Portfolio Project Divisions|Real Estate Division|True|StoreData|StoreData|+|+|StoreData||True|True|True|True|True|True|+|+|+|+|+|False|False||||||Unspecified||||N|Default|0|Default||False|||

!PropertyArray=Cost Center
'Property|Name|Key|Value
Alias|ALL_DEPTS|Default|All Departments
Alias|ACAHC|Default|XYZ Club Affiliates
Alias|ACPHC|Default|XYZ Club Partners
Alias|H006-565|Default|H006-565 ACPHC Adminstration Treat Towe
Alias|H008-565|Default|H008-565 ACPHC Strategy & Development T

The file is huge and I just picked few lines under each section to give an idea.

Thanks,
Ragav.

message edited by ragavhere


Report •

#11
August 5, 2013 at 20:37:24
Multiple occurances of the subsets in this file? (I assumed only one. my mistake). That's not the flaw however, it's in my code.

message edited by nbrane


Report •

#12
August 5, 2013 at 20:54:12
Hmmm. But there are only one instance of "'Parent|Child|IsPrimary*****" and "!PropertyArray=Cost Center"...

Regards,
Ragav.


Report •

#13
August 6, 2013 at 11:32:28
✔ Best Answer
Ok, I had to drop back and punt, but this seemed to work on your test set:
::======== begin batchscript
@echo off & setlocal enabledelayedexpansion
for /f "tokens=1 delims=[]" %%a in ('find /n "'Parent|Child"^<testzz') do @(
more +%%a testzz)>x
(for /f "tokens=*" %%a in (x) do (
set test=%%a
if "!test:~0,13!" equ "!PropertyArray" goto :eof
echo %%a
))>xout
::========= end batchscript

Report •

#14
August 6, 2013 at 12:22:24
nbrane,

Works as needed. Thanks..


Report •

Ask Question