在Powershell中查找字符串的开头和结尾并写入CSV [英] Find the start and end of a string and write into CSV in Powershell

查看:328
本文介绍了在Powershell中查找字符串的开头和结尾并写入CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多行的巨大 txt 文件.

I have huge txt file with multiple lines in it.

我想创建一个数组来存储带有PROCEDURE"的行的开头.并以END;"结尾.

I want to create an array which stores the start of a line with "PROCEDURE" and ends with "END;".

然后我需要在数组中搜索 [Start with "PROCEDURE";&以END;"]结尾我是否有任何带有EXCEPTION"的行?作为字符串.

Then i need to search within the array [Start with "PROCEDURE" & Ends with "END;"] whether I have any lines with "EXCEPTION" as string.

如果我没有找到字符串EXCEPTION"在数组中,我必须将消息写入文件未找到异常",如果找到则将消息写入找到异常"

If i don't find the string "EXCEPTION" within the array I have to write a message to a file the "Exception is not found", if found then write message as "Exception is found"

参考文件格式

-------------------------------------------------------------------
CLEAR;
EXIT

PROCEDURE FP_XXXXXX

IS

---------
---------
EXCEPTION

---------
---------
ENDS;

FUNCTION FF_

AS

--------
--------


问候,马克


Regards, Marc

推荐答案

让我们把问题分解成:

  1. 从磁盘文件中读取行
  2. 遍历它们直到找到PROCEDURE
  3. 对于每一行:
    • 如果行以 END 开头,则返回/退出
    • 如果没有,检查它是否包含我们正在寻找的字符串


转换为 PowerShell,我们可能会想出这样的东西:


Translated to PowerShell, we might come up with something like this:

$started = $false
$exceptionFound = $false

# 1. Read the lines from the file
foreach($line in Get-Content .\path\to\file) {
  if(-not $started){
    # look for `PROCEDURE`
    $started = $line -clike 'PROCEDURE*'
    continue
  }

  # look for END
  if($line -clike 'END;*'){
    # we're done here
    break
  }

  # otherwise, look for the string
  $exceptionFound = $line -like '*exception*'
  if($exceptionFound){
    break
  }
}

# $exceptionFound will hold the answer at this point

这篇关于在Powershell中查找字符串的开头和结尾并写入CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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