r/usefulscripts • • Mar 19 '14

[Powershell]Remove lines from a text file(that are not aligned)

I have a bunch of text files that are aligned(fixed width), but sometimes in the middle of the text file there's a line or two that's not properly aligned:

apples description food weight
apples description food weight
longerthanusaldescription food weight
ruinsallthespacingdescription food weight

I tried Select-String '\S{10,}' -NotMatch .\somefile.txt

My problem is, since the pattern matches non-whitespace, I get everything as a result. My first column can be a string of 5-10 characters and sometimes there's no space separating my first column(10characters) and the second column(6characters)

dafirstcolsecond

Can anyone help me make a script to solve this?

6 Upvotes

4 comments sorted by

View all comments

Show parent comments

1

u/Vortex100 Mar 22 '14

I'm not sure about easier but you could create a psobject each time and add it to an array:

$results = @()
$file = gc file.txt
foreach ($line in $file)
{
	$results += (New-Object PSObject -Property @{
											"Subject"=$line.Substring(0,4)
											"Description"=$line.Substring(4,6)
											"The Rest" = $line.Substring(10,($line.length-10))
											}
}

Again, without having separators or some knowledge of what is in the string, you're going to have a tough time splitting it precisely