How to split a file into multiple files using powerhell based on the values in a fixed position of each row of data in the file


i have file split multiple files.  the data in file fixed width data file.  i split seperate files record code 3 characters stored in first 3 characters in each line.  the data file this:

maindatafile.txt

001datadatadatadatadatadatadatadatadatadatadatadatadatadatadata

001datadatadatadatadatadatadatadatadatadatadatadatadatadatadata

002datadatadatadatadatadatadatadatadatadatadatadatadatadatadata

003datadatadatadatadatadatadatadatadatadatadatadatadatadatadata

003datadatadatadatadatadatadatadatadatadatadatadatadatadatadata

004datadatadatadatadatadatadatadatadatadatadatadatadatadatadata

i record code 001 rows in file called 001.txt, 002 rows in file called 002.txt, etc.  would able pass the maindatafile.txt file name script when run if possible.

fwiw

$filename = 'maindatafile.txt'      get-content $filename |    foreach {    $_ | add-member -membertype noteproperty -name code -value $_.substring(0,3) -passthru    } | group code |    foreach { $_.group | set-content "$($_.name).txt" }


[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

directory stack

After enabling Windows Server 2012 R2 DHCP Failover Getting Packet dropped because of Client ID hash mismatch

WMI Repository 4GB limit - Win 2003 Ent Question