Answered by:
Remove lines (header/footer) and keep only tabular lines

Question
-
Hello Experts,
I would like to know any script (preferably power shell) to remove header & footer lines from text file.
Just to give a detail. I have following content in text file.
----start----
Trace trace log for server: ServerA.Test.COM
Windows version: 6.2.9200 (Windows Server 2012)
Time zone: India Standard Time
Time difference to UTC: +5:30
Running in Terminal Services session...
Monitoring debug output from processes running in the console session
Monitoring debug output from processes running in the current Terminal Services session
7 16:57:27.497 [119916] (ProcessA) Work1
9 16:57:27.497 [119916] (ProcessA) Work2
10 16:57:27.497 [119916] (ProcessA) WORK3
11 16:57:27.497 [119916] (ProcessA) WORK4
Registry info
HKEY_LOCAL_MACHINE\SOFTWARE\YB\Aplication
Driver (SZ) SQL Server
DriverVersion (SZ) 3.50.0305
YAcloudExporterReady (DWORD) 1 [0x1]
Installed Version info [E:\Program Files (x86)\Aplication]
---------end----------
I would like to copy content only from line that is tab delimited (9 16:57:27.497 [119916] (ProcessA) Work2) till next 3 more lines have similar information to new file.
The reason of such formatting so I can upload new file to SQL DB (I know how to do that).Wednesday, September 27, 2017 8:00 AM
Answers
-
Hi Pradeep,
Are you familiar with Regular Expression? I suggest you to learn them. In this case you can do it differently.. if you want to remove the first 8 line and the last 7 is just a matter of reading the help of Get-Content cmdlet. In this case Get-Content result is an Array but with the casting you basically can use a String.Replace.
$message = [string] (Get-Content file.txt) $header = [string] (Get-Content file.txt -first 8) $footer = [string] (Get-Content file.txt -tail 7) $message = $message.replace($header,"").replace($footer,"")
Regards
- Proposed as answer by Albert LingMicrosoft contingent staff Thursday, September 28, 2017 1:34 AM
- Unproposed as answer by Albert LingMicrosoft contingent staff Thursday, September 28, 2017 1:34 AM
- Marked as answer by Pradeep-Papnai Saturday, March 24, 2018 11:14 AM
Wednesday, September 27, 2017 8:43 AM
All replies
-
Hi Pradeep,
Are you familiar with Regular Expression? I suggest you to learn them. In this case you can do it differently.. if you want to remove the first 8 line and the last 7 is just a matter of reading the help of Get-Content cmdlet. In this case Get-Content result is an Array but with the casting you basically can use a String.Replace.
$message = [string] (Get-Content file.txt) $header = [string] (Get-Content file.txt -first 8) $footer = [string] (Get-Content file.txt -tail 7) $message = $message.replace($header,"").replace($footer,"")
Regards
- Proposed as answer by Albert LingMicrosoft contingent staff Thursday, September 28, 2017 1:34 AM
- Unproposed as answer by Albert LingMicrosoft contingent staff Thursday, September 28, 2017 1:34 AM
- Marked as answer by Pradeep-Papnai Saturday, March 24, 2018 11:14 AM
Wednesday, September 27, 2017 8:43 AM -
Hi,
well ... where there is a will. there is a script. This looks like a total case for regular expression (which will look like magic - the demon summoning kind, not the Gandalf-"I save the world"-kind - at first). Here's something that worked for me after copy&pasting your text example:
Get-Content file.txt | Select-String "^(\d+)\s+(\d\d:\d\d:\d\d.\d+)\s+(\[\d+\])\s+(\(.+?\))\s+([^\s]+)$" | ForEach-Object { $_.Matches.Groups[1..4] -join "`t" }
Cheers,
FredThere's no place like 127.0.0.1
- Proposed as answer by Albert LingMicrosoft contingent staff Thursday, September 28, 2017 1:34 AM
Wednesday, September 27, 2017 8:54 AM -
You might use one of the techniques Dr. Tobias Weltner explains here in this video:
Sophisitcated Techniques of Plain Text Parsing 3.926 Aufrufe
Grüße - Best regards
PS:> (79,108,97,102|%{[char]$_})-join''Wednesday, September 27, 2017 9:31 AM