so basically I have a series of .txt files following the same nomenclature, such as:
1111210803_3CE_080977851__006908818__21110300013442021110420211105_20211110_120447_35418862_820
1111210933_3CE_006908818__2111040001442021110520211108_20211110_120447_35418860_820
The naming convention on these all files always starts with the date, ie 111121
. Inside these files, you have lines of strings. I am interested in pulling a specific string in the first line from each of these files. Here is an example of the first line:
123456789012345678901234567890123 I 696969CCHKCTX 12345678901 DA 22758287
In particular, I am interested in the 696969CCHKCTX
string. All files will have some numbers followed by the CCHKCTX
value. I want to pull the 696969
part of the 696969CCHKCTX
string from each .txt file and append them all into a new file.
If possible, I would like to sum those strings and add the appropriate decimal place, as they are to actually be dollar values ie 696969
is actually representing 6969.69
and the last two numbers in that string always represent the cent amount. This rule applies to all the .txt files. I want to be able to apply this to all files that are the same date (ie all files starting with 111121
)
How would I go about this?
CodePudding user response:
Try the following, which combines Get-ChildItem
, Group-Object
, and ForEach-Object
, as well as the -replace
operator:
Get-ChildItem -File | # get files of interest; add path / filter as needed.
Group-Object { $_.Name.Substring(0, 6) } | # group by shared date prefix
ForEach-Object {
$firstLines = $_.Group | Get-Content -First 1 # get all 1st lines
# Extract the cents amounts and sum them.
$sumCents = 0.0
$firstLines.ForEach({
$sumCents = [double] ($_ -replace '. \b(\d )CCHKCTX\b. ', '$1')
})
# Output an object with the date prefix and the sum dollar amount.
[pscustomobject] @{
Date = $_.Name
Sum = $sumCents / 100
}
}
The above outputs a table-formatted representation to the display. You can save it to a file with >
/ Out-File
, for instance, though it's better to use a structured text format for later processing, such as Export-Csv
.