Newb Powershell questions modifying text files with random file names in a Directory

I'm looking for a little help completing some tasks. I'm not really a coder but made it pretty far reading all these great posts. There are a lot more moving parts to this but I'm enjoying some of the reverse engineering. I'll appreciate any direction pointing or resources that will put me on the right track.

I have a directory (D:\CLASS_DATA\CLASS_WORKING) that receives update messages in the form of text files with the extension *.work file names are random.

Contents of file (D:\CLASS_DATA\CLASS_WORKING\18e-1-4313-92b8-6ac79bc.work) This data always appears in Line# 2:

STUD_ID|||M000001613||LAST^FIRST^||19800801^40.88^Years|Female||Caucasian|^^^^^|||||||V00000_5384|||||||||||||

These 3 lines appear on different lines in each file:

SRX|21|ST|Event_dive_Sat||TN^0^98.0^13.2^^17.6^TN^675-67-656-9-DE9361ED07A6||||||F|||20210618081105||||
SRX|22|ST|Event_dive_Sat||RA^0^77.2^13.2^^13.9^None^AD71-0E-47E-92-AA11771ED242||||||F|||20210618081137||||
SRX|23|ST|Event_dive_Sat||BN^0^78.4^13.2^^14.1^BN^922-6-4A-8A-AD46AFB5712E||||||F|||20210618081212||||

task #1: In line #2, 1 of 4 options (Female, Male, Unknown, None) appear for Student_Sex. These always appear in what I believe to be the 8th position on line #2. I have a character limitation for Student_Sex in this old software of 1 character. so we need to change (Female, Male, Unknown, None) to (F, M, U, N)

I started thinking i could just get on the line an make an update but quickly realized we have a student with the name Ismale so my idea would change his name to "IsM"..smh...

I haven't started working on the fact I'm getting random file names in my code yet. Just looking to better understand how to target data modify and append it.

Question #1 How can I better target the (Female, Male, Unknown, None) data on line #2 position #8 to be updated to (F, M, U, N)?

Powershell:

$working_file = 'D:\CLASS_DATA\CLASS_WORKING\18e-1-4313-92b8-6ac79bc.work'
$destination_file =  'D:\CLASS_DATA\CLASS_WORKING\18e-1-4313-92b8-6ac79bc.done'
$line = 1
(Get-Content -Path $working_file | Foreach-Object {
    if ($line++ -eq 2) {
    $_ -replace 'Female', 'F' `
       -replace 'Male', 'M' `
       -replace 'Unknown', 'U' `
       -replace 'None', 'N'
       } else { $_ } 
    }) | Set-Content $destination_file

task #2:

There are 3 lines that do not always appear on the same line and carry 3 vales representing (High,Medium,Low). Like:

SRX|21|ST|Event_dive_Sat||TN^0^98.0^13.2^^17.6^TN^675-67-656-9-DE9361ED07A6||||||F|||20210618081105|||| SRX|22|ST|Event_dive_Sat||RA^0^77.2^13.2^^13.9^None^AD71-0E-47E-92-AA11771ED242||||||F|||20210618081137|||| SRX|23|ST|Event_dive_Sat||BN^0^78.4^13.2^^14.1^BN^922-6-4A-8A-AD46AFB5712E||||||F|||20210618081212||||

It uses "Custom_Field" and a unique ID "^1cddec93" to target values (Custom_Field^1cddec93) I need to parse out each value to its own line at the end of the new file so the old software will see it. Mapping looks like this:

Custom_Field^1cddec93;Measurement;;PREHIGHSAT
Custom_Field^2cddec93;Measurement;;PREEMEDSAT
Custom_Field^3cddec93;Measurement;;PREHIGHSAT

Goal is to get it looking like below: I have no idea what all the other data is.

SR|61|ST|SRX|21|ST|Custom_Field^1cddec93-56-44-8b-d93^HIGHTN: (**98.0**)||245^ca5-e3-4a4-99-d1e5f4^Pre dive sat^0||||||F|||20210618075125||||

SR|61|ST|Custom_Field^2cddec93-56-44-8b-d93^MEDTN: (**13.2**)||245^ca5-e3-4a4-99-d1e5f4^Pre dive sat^0||||||F|||20210618075125||||

SR|61|ST|Custom_Field^3cddec930-56-44-8b-d93^LOWTN: (**17.6**)||245^ca5-e3-4a4-99-d1e5f4^Pre dive sat^0||||||F|||20210618075125||||

SR|61|ST|Custom_Field^4cddec930-56-44-8b-d93^HIGHRA: (**77.2**)||245^ca5-e3-4a4-99-d1e5f4^Live dive sat^0||||||F|||20210618075125||||

SR|61|ST|Custom_Field^5cddec930-56-44-8b-d93^MEDRA: (**13.2**)||245^ca5-e3-4a4-99-d1e5f4^Live dive sat^0||||||F|||20210618075125||||

SR|61|ST|Custom_Field^6cddec930-56-44-8b-d93^LOWRA: (**13.9**)||245^ca5-e3-4a4-99-d1e5f4^Live dive sat^0||||||F|||20210618075125||||

SR|61|ST|Custom_Field^7cddec930-56-44-8b-d93^HIGHRA: (**78.4**)||245^ca5-e3-4a4-99-d1e5f4^Post dive sat^0||||||F|||20210618075125||||

SR|61|ST|Custom_Field^8cddec930-56-44-8b-d93^MEDRA: (**13.2**)||245^ca5-e3-4a4-99-d1e5f4^Post dive sat^0||||||F|||20210618075125||||

SR|61|ST|Custom_Field^9cddec930-56-44-8b-d93^LOWRA: (**14.1**)||245^ca5-e3-4a4-99-d1e5f4^Post dive sat^0||||||F|||20210618075125||||


SR|61|ST|SRX|21|ST|Custom_Field^1cddec93-56-44-8b-d93^HIGHTN: (98.0)||245^ca5-e3-4a4-99-d1e5f4^Pre dive sat^0||||||F|||20210618075125||||
SR|61|ST|Custom_Field^2cddec93-56-44-8b-d93^MEDTN: (13.2)||245^ca5-e3-4a4-99-d1e5f4^Pre dive sat^0||||||F|||20210618075125||||
SR|61|ST|Custom_Field^3cddec930-56-44-8b-d93^LOWTN: (17.6)||245^ca5-e3-4a4-99-d1e5f4^Pre dive sat^0||||||F|||20210618075125||||

SR|61|ST|Custom_Field^4cddec930-56-44-8b-d93^HIGHRA: (77.2)||245^ca5-e3-4a4-99-d1e5f4^Live dive sat^0||||||F|||20210618075125||||
SR|61|ST|Custom_Field^5cddec930-56-44-8b-d93^MEDRA: (13.2)||245^ca5-e3-4a4-99-d1e5f4^Live dive sat^0||||||F|||20210618075125||||
SR|61|ST|Custom_Field^6cddec930-56-44-8b-d93^LOWRA: (13.9)||245^ca5-e3-4a4-99-d1e5f4^Live dive sat^0||||||F|||20210618075125||||

SR|61|ST|Custom_Field^7cddec930-56-44-8b-d93^HIGHRA: (78.4)||245^ca5-e3-4a4-99-d1e5f4^Post dive sat^0||||||F|||20210618075125||||
SR|61|ST|Custom_Field^8cddec930-56-44-8b-d93^MEDRA: (13.2)||245^ca5-e3-4a4-99-d1e5f4^Post dive sat^0||||||F|||20210618075125||||
SR|61|ST|Custom_Field^9cddec930-56-44-8b-d93^LOWRA: (14.1)||245^ca5-e3-4a4-99-d1e5f4^Post dive sat^0||||||F|||20210618075125||||

Question #2 What is the best way to target data in an array like this with all the other text and numbers around it? I could key in on "SRX|21|ST|Event_dive_Sat" but am kind of lost how to get over to the correct part of the array.
I watched a ton of videos read a bunch of threads but i think this is a bit more complicated than i thought.



from Recent Questions - Stack Overflow https://ift.tt/3vRhqSs
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)