Link to home
Start Free TrialLog in
Avatar of Danny Kon
Danny KonFlag for Netherlands

asked on

I want to split a text file in how many names are in the text file (sorry for the bad explanation)

See attached file for the complete example

Datum                      Klant naam                                       ArtCode
-----                           ----------                                              -------
1-4-2015 0:00:00     Cafe Brakke                                        20206  
1-4-2015 0:00:00     Cafe Brakke                                        20110  
1-4-2015 0:00:00     Cafe Lime                                          50110  
1-4-2015 0:00:00     Cafe Lime                                          04355  
1-4-2015 0:00:00     Café Pollux                                        16368  
1-4-2015 0:00:00     Café Pollux                                        16356  

In this example i need 3 x a  text file, the first one called Cafe Brakke.txt and in the text file
Datum                      Klant naam                                        ArtCode
-----                           ----------                                               -------
1-4-2015 0:00:00     Cafe Brakke                                        20206  
1-4-2015 0:00:00     Cafe Brakke                                        20110  

2nd file  Cafe Lime.txt and in the file
Datum                      Klant naam                                       ArtCode
-----                           ----------                                              -------
1-4-2015 0:00:00     Cafe Lime                                          50110  
1-4-2015 0:00:00     Cafe Lime                                          04355  

3th file  Café Pollux.txt and in the file
Datum                      Klant naam                                       ArtCode
-----                           ----------                                              -------
1-4-2015 0:00:00     Café Pollux                                        16368  
1-4-2015 0:00:00     Café Pollux                                        16356  

If somebody also can help me to remove the 0:00:00 behind the date it would be perfect (not necessary:)
Thanks for helping

Danny
Avatar of Raheman M. Abdul
Raheman M. Abdul
Flag of United Kingdom of Great Britain and Northern Ireland image

Here it is: ( Output in .csv form for you)

save the example file in C:\Temp\DataFileSplit.txt    (Modify it as required)

Output File Location : "c:\temp\"   (Modify  $OutputLocation   as required)

$content = Get-Content C:\Temp\DataFileSplit.txt

$content = $content -replace "   "," "
$content = $content -replace " 0:00:00",""

$len = $content.Length
$content = $content[2..$len]

$items = @()
$Outputlocation = "c:\temp\"


foreach ($line in $content)
{
$line = $line -replace "   "," "
$line = $line -replace "  "," "

$datum = $line.Split(" ")[0]
$klantNaam = $line.Split(" ")[1] + " "  + $line.Split(" ")[2]
$artcode = $line.Split(" ")[-2]

$item = New-Object System.Object
$item | Add-Member -Type Noteproperty -Name Datum -Value $datum.Trim()
$item | Add-Member -Type Noteproperty -Name KlantNaam -Value $klantNaam
$item | Add-Member -Type Noteproperty -Name Artcode -Value $artcode

$items += $item

}
#$items | Sort-Object Artcode -Descending

foreach ($item in $items)
{
$filename = $Outputlocation + "$($item.KlantNaam).csv"
$item | Export-Csv $filename -NoTypeInformation -Append -Encoding BigEndianUnicode
}

Open in new window

Oh you missed to attach the file. I just knew it
Avatar of Bill Prew
Bill Prew

Need a sample of the input file, is it space delimited, or fixed column format (looks a bit like a report file), or are there tabs in it, etc?

~bp
Avatar of Danny Kon

ASKER

Sorry here is the attached file.

Thanks Danny
test.txt
Raheman

Almost there, 3 problems, 1 file is unreadable and does not have a name, a second file has the name ---------- .csv
and inside the file i see
þÿ"Datum","KlantNaam","Artcode"
-----,"---------- ",""
 
All the files showing quotation marks around the names
þÿ"Datum","KlantNaam","Artcode"
1-4-2015,"Black Tiger","01201"

And the text in the file always start with letters þÿ

Thanks Danny
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bill Prew,

It works like a charm but I can't get it to work in my powershell script "cscript.exe test.vbs"
But that has nothing to do with my question
Perfect solution

Thanks Danny