Powershell to Clean up txt file

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
aarthidiv
Posts: 2
Last visit: Thu Dec 09, 2021 2:10 pm

Powershell to Clean up txt file

Post by aarthidiv »

Hi there,

Input file contains few lines which are broken into 2 lines. The below logic works fine and checks if a line contains [" but does not contain "] then it concatenates line 1 & 2.

Existing working logic

Code: Select all

$file = Get-Content -Path "C:\temp\sinput.txt" -Raw
$file = $file -ireplace '(?<match1>\[\"[^\]]*)\r\n(?<match2>[^\]]*\"\])','${match1}${match2}'
$file
For example: Input
["ef
gh"],

gets corrected as in output

["efgh"] which is good.

Q1:
How to change the above logic to check if a line contains [" but does not contain ], then it concatenates line 1 & 2.

I tried: efgh works as expected so far but misses out the next line after "point of care"

Code: Select all

$file = Get-Content -Path "C:\temp\sinput.txt" -Raw
$file #show input file
$file = $file -ireplace '(?<match1>\[\"[^\]]*)\r\n(?<match2>[^\]]*\]\,)','${match1}${match2}'
$file
Q2:
How to remove leading spaces (if any) on line 2 before concatination.

I tried: trim function but i'm new to ps and im sure i made some mistake

Code: Select all

$file = Get-Content -Path "C:\temp\sinput.txt" -Raw
$file = $file -ireplace '(?<match1>\[\"[^\]]*)\r\n(?<match2>[^\]]*\"\])','${match1}${match2}.trim()'
$file
Q3:
Line 2 has encoding characters. Team Lead's (single quotes does not work as expected)

I know this works.
get-content C:\temp\pinput.txt -Encoding UTF8 | Set-Content c:\temp\poutput.txt

But when i try the below it doesnt work. i know im new and i think im not doing it right.

Code: Select all

$file = Get-Content -Path "C:\temp\sinput.txt" -Raw
$file = $file -ireplace '(?<match1>\[\"[^\]]*)\r\n(?<match2>[^\]]*\"\])','${match1}${match2}.trim()'
$file = $file -Encoding UTF8 
$file
Any help is appreciated. Thanks.
Attachments
sinput.txt
Sample input file
(324 Bytes) Downloaded 96 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell to Clean up txt file

Post by jvierra »

The complexity and number of question as you are asking is beyond the scope of a basic tech forum. First you are asking about how to use RegEx. This is not a PowerShell question but a specific issue about how to use RegEx.

My first suggestion is to take it in pieces then use the following linked site to learn how to use RegEx to parse the document. Given the complexity of your issue I suspect that a RegEx replacement function might be the easiest approach. Isolate the line and then use a function to adjust the line's contents.


https://www.regular-expressions.info/

https://en.wikipedia.org/wiki/Regular_expression
aarthidiv
Posts: 2
Last visit: Thu Dec 09, 2021 2:10 pm

Re: Powershell to Clean up txt file

Post by aarthidiv »

@jvierra. Thanks for your links. I wasnt aware reg expression is a concept that is not specific to powershell. I ll go over the link. However my Q2 is related to trim function on powershell. Unless you say other wise. I couldnt find anything about leading spaces in reg expression.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell to Clean up txt file

Post by jvierra »

All of your questions are about regular expr3essions. Post in a RegEx forum to get answers. We cannot design complex regular expressions for you. If you have a specific PowerShell related question, then please ask it as a specific PS question.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell to Clean up txt file

Post by jvierra »

All of your questions are about regular expr3essions. Post in a RegEx forum to get answers. We cannot design complex regular expressions for you. If you have a specific PowerShell related question, then please ask it as a specific PS question.
This topic is 2 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked