如何在下划线模式后匹配一行 [英] How to match a line after a pattern of underscores

查看:53
本文介绍了如何在下划线模式后匹配一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大量数据的文件,如下所示:

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 钻机

1-9/16 TCS DRILL

1-11/32 TCS 钻机

1-11/32 TCS DRILL

1-11/32 TCS 钻机

1-11/32 TCS DRILL

.....

如何在 powershell 中指定模式以使用 get-content 或 get-childitem 匹配 LINE?

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

推荐答案

我会尝试使用您的示例数据(在文本文件 $file 中)

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}

只需使用 -Raw 立即读取 整个 文件,然后 -split 将文件放在下划线组上.使用 Where-Object 过滤掉空条目,因为文件开头会有一个.

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.

对于从该过程中获得的每个块,我们将其拆分为换行符.由于前 2 行是空白,我们使用 -Index 来获取包含您要查找的内容的第一行数据.

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.

示例输出

.         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+"}

这篇关于如何在下划线模式后匹配一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆