Root Loop
Root Loop

Reputation: 3162

How to match a line after a pattern of underscores

I have a file containing huge mount of data like this:

______________________________________________________________________

.         1-9/16 TCS DRILL                  

.          CUT = +2.685 / O/A = -2.685
.                +2.685 /       -2.685
.                +3.935 /       -3.935
______________________________________________________________________

.         1-11/32 TCS DRILL                  

.          CUT = +0.9 / O/A = -3.237
.                +0.9 /       -3.237
.                +0.9 /       -4.487
___________________________________________________________________

.         1-11/32 TCS DRILL                  

.          CUT = +5.699 / O/A = -5.699
__________________________________________________________________

.         1-1/8 TCS DRILL                  

.          CUT = +1.553 / O/A = -1.553
.                +2.338 /       -2.338
.                +2.513 /       -2.513 

What I need to do is to grab the first line of text after each "_______" (underscores),

1-9/16 TCS DRILL

1-11/32 TCS DRILL

1-11/32 TCS DRILL

.....

how can I specify a pattern in powershell to match LINE using get-content or get-childitem?

I could use to match the lines without any of those characters "=" , "+", "-", but this pattern is not accurate and not working...

$file = 'C:\test\001.txt'

Get-Childitem $file | select-string -pattern '=','+','-' -notmatch

Upvotes: 0

Views: 601

Answers (2)

Matt
Matt

Reputation: 46710

I would have tried something like this with your sample data ( in the text file $file)

(Get-Content -Raw $file) -split "_+" | 
    Where-Object{$_} | 
    ForEach-Object{$_ -split "`r`n" | Select -Index 2}

Simply reads the whole file at once using -Raw then -splits the file on the group of underscores. Use Where-Object to filter out the empty entries as there would be one at the beginning of the file.

With each chunk that is gained from that process we take it a split it up on newlines. Since the first 2 lines are empty space we use -Index to grab the first line of data that contains what you are looking for.

Sample Ouput

.         1-9/16 TCS DRILL                  
.         1-11/32 TCS DRILL                  
.         1-11/32 TCS DRILL                  
.         1-1/8 TCS DRILL 

To remove the leading period and following spaces we can do a small update to the loop

ForEach-Object{($_ -split "`r`n" | Select -Index 2) -replace "^\.\s+"}

Upvotes: 1

ComputerDruid
ComputerDruid

Reputation: 17586

The way you've pasted it, at least, the lines are underscores _, not hyphens -. Try adding '_' to the list of things to not match

Upvotes: 0

Related Questions