I have a text file with several lines:
9 1/7/20 11:46:25 am PC-OCWIN0306 Device status is Critical. Protection is off.
10 1/7/20 11:10:16 am PC-OCWIN0277 Device status is Critical. Protection is off; Security application is not running.
11 1/7/20 2:47:19 pm LT-FRWIN0004 Device status is Critical. Security application is not installed.I would like to remove the first 20 characters from each line, so it looks something like:
PC-OCWIN0306 Device status is Critical. Protection is off.
PC-OCWIN0277 Device status is Critical. Protection is off; Security application is not running.
LT-FRWIN0004 Device status is Critical. Security application is not installed.I have been trying to use this:
$testfile = Get-Content -Path "Z:\IT Users\Username\test.txt"
foreach($line in $testfile) { $line.TrimStart(20) }
}
Out-File "Z:\IT Users\Username\trimtest.txt"
pausebut it's not doing what I want.
12 Answers
Windows 10 64-bit. PowerShell 5
Edit text file from the command line with PowerShell and regular expressions.
Remove, by replacing with nothing, from the beginning of line up to and including pattern "m ". Remove extra carriage return and extra line feed (change double line spacing to single line spacing).
$source = "$env:userprofile\Desktop\2.txt"
$trim = "$env:userprofile\Desktop\3.txt"
(Get-Content $source -Raw) -replace "^.|.*m " -replace "[`r`n]+", "`n" | Set-Content $trimRaw, carriage return and new line explanation at stackoverflow
- By default, get-content automatically splits a file into lines on newlines. You need to use the -raw parameter to read the file as a single block of text.
Go to to test your regular expressions and get explanations for how they work. For this method of replacement I found the Tool - Code Generator - Language - AutoIt to be informative.
Regular expression: ^.|.*m Do not forget the space after m. From the beginning of line match everything up to and including pattern "m ".
Regular expression: (?m) D.*.$ Match everything after " D" except carriage returns and line feeds.
Regular expression: [\r\n]+ Change double line spacing to single line spacing.
Test file / string:
9 1/7/20 11:46:25 am PC-OCWIN0306 Device status is Critical. Protection is off.
10 1/7/20 11:10:16 am PC-OCWIN0277 Device status is Critical. Protection is off; Security application is not running.
11 1/7/20 2:47:19 pm LT-FRWIN0004 Device status is Critical. Security application is not installed.Results:
PC-OCWIN0306 Device status is Critical. Protection is off.
PC-OCWIN0277 Device status is Critical. Protection is off; Security application is not running.
LT-FRWIN0004 Device status is Critical. Security application is not installed.Replace everything except the device name:
$source = "$env:userprofile\Desktop\2.txt"
$trim = "$env:userprofile\Desktop\3.txt"
(Get-Content $source -Raw) -replace "^.|.*m " -replace "(?m) D.*.$" -replace "[`r`n]+", "`n" | Set-Content $trimResults:
PC-OCWIN0306
PC-OCWIN0277
LT-FRWIN0004 Thanks to for the screenshots.
^.|.*m
- Match either the regular expression below (attempting the next alternative only if this one fails) «^.»
- Assert position at the beginning of the string «^»
- Match any single character that is not a line break character «.»
- Or match regular expression number 2 below (the entire match attempt fails if this one fails to match) «.*m »
- Match any single character that is not a line break character «.*»
- Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
- Match the characters "m " literally «m »
- Match any single character that is not a line break character «.*»
(?m) D.*.$
- Match the remainder of the regex with the options: ^ and $ match at line breaks (m)
- Match the characters " D" literally
- Match any single character that is not a line break character
- Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
- Match any single character that is not a line break character
- Assert position at the end of a line (at the end of the string or before a line break character)
[\r\n]+
- Match a single character present in the list below
- Between one and unlimited times, as many times as possible, giving back as needed (greedy)
- A carriage return character
- A line feed character (new line)
Edit text file with PS and Regex. Edit text file with PowerShell and Regex. Edit text file with PowerShell and regular expression.
8You want the Remove method.
$testfile = Get-Content -Path "Z:\IT Users\Username\test.txt"
foreach($line in $testfile) { $line.Remove(0,20) }
} | Out-File "Z:\IT Users\Username\trimtest.txt"or avoid intermediate variables:
Get-Content -Path "Z:\IT Users\Username\test.txt" | ForEach{ $_.Remove(0,20)
} | Out-File "Z:\IT Users\Username\trimtest.txt"Edit: I took you at your word regarding 20 characters, but see the length of the Date/Time can vary, and if so a regex is your best bet. Does your imput file actually contain blank lines between each line with text? If not, the following regex should do the trick, and it's easier to see what it's captruing:
Get-Content $source | ForEach{$_ -replace '^.+m ' -replace ' Device.+$'} 7