waltforbes
asked on
How to remove sequential rows of text from a text file?
Points of My Scenario:
1. I have a single text file containing multiple lists like the illustration below:
2. Each list is separated by a single empty line.
3. I want to delete all "item" entries, so that only "LIST" entries remain
QUESTION: How can I remove all "item" rows from the text file, while retaining "LIST" rows?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It reads the input file, and sends each line to ForEach-Object (the Begin scriptblock will be run once when the first object comes in, the Process scriptblock will be run once per object).
In the loop, it basically only checks if the last line (the line before) was empty, and if so, it will write the line to the output, followed by an empty line. "last" will initially be empty, so the first real line is detected as having an empty line before as well.
In the loop, it basically only checks if the last line (the line before) was empty, and if so, it will write the line to the output, followed by an empty line. "last" will initially be empty, so the first real line is detected as having an empty line before as well.
I figured that all out, no problem, but this one gives me some headache (maybe this is just some shortcut I don't know yet):
{$_; ''}
That's just two outputs in a row: first the current loop element (the "LIST x" line), then an empty line.
Ok, thanks ;-)
Just as a side note: this will only work this way, if there will always be an empty line between the blocks. Otherwise, you'd need some "real parsing" ;-)
I've done some stuff with Powershell, but would you be so nice and explain, what the hell is going on here in this snippet?
Many thanks in advance :-))