使用PowerShell读取文件数据

We have some custom files that we receive from different providers and for these situations we are unable to use standard ETL programs without any customization. Since we’re expanding our ability to read these custom files with .NET, we’re looking for efficient ways to read files with PowerShell that we can use in SQL Server Job Agents, Windows Task Schedulers, or with our custom program, which can execute PowerShell scripts. We have many tools for parsing data and wanted to know efficient ways of reading the data for parsing, along with getting specific lines of data from files by number, or by the first or last line of the file. For reading files efficiently, what are some functions or libraries we can use?

我们有一些来自不同提供商的自定义文件,在这种情况下,如果不进行任何自定义,我们将无法使用标准ETL程序。 由于我们正在扩展使用.NET读取这些自定义文件的能力,因此,我们正在寻找一种使用PowerShell读取文件的有效方法,这些方法可以在SQL Server作业代理,Windows Task Scheduler或我们的自定义程序中使用,执行PowerShell脚本。 我们有许多用于解析数据的工具,并且想了解读取数据进行解析的有效方法,以及从文件中按编号或文件的第一行或最后一行获取特定的数据行。 为了有效地读取文件,我们可以使用哪些功能或库?

总览 (Overview)

For reading data from files, we generally want to focus on three major functions for completing these tasks along with some examples listed next to them of these in practice:

为了从文件中读取数据,我们通常希望专注于完成这些任务的三个主要功能,以及实践中紧随其后的一些示例:

  1. How to read an entire file, part of a file or skip around in a file. We may face a situation where we want to read every line except the first and last.

    如何读取整个文件,部分文件或在文件中跳过。 我们可能会遇到一种情况,我们希望读取除第一行和最后一行之外的每一行。
  2. How to read a file by using few system resources. We may have a 100GB file that we only want to read 108KB worth of data.

    如何使用很少的系统资源来读取文件。 我们可能有一个100GB的文件,我们只想读取108KB的数据。
  3. How to read a file in a manner that easily allows us to parse data we need or allows us to use functions or tools we use with other data. Since many developers have string parsing tools, moving data to a string format – if possible – allows us to re-use many string parsing tools.

    如何以轻松允许我们解析所需数据或允许我们使用与其他数据一起使用的功能或工具的方式读取文件。 由于许多开发人员都拥有字符串解析工具,因此将数据移动到字符串格式(如果可能的话)使我们能够重用许多字符串解析工具。

The above applies to most situations involved with parsing data from files. We’ll start by looking at a built-in PowerShell function for reading data, then look at a custom way of reading data from files using PowerShell.

以上适用于涉及从文件中解析数据的大多数情况。 我们将首先查看用于读取数据的内置PowerShell函数,然后查看使用PowerShell从文件读取数据的自定义方式。

PowerShell的Get-Content函数 (PowerShell’s Get-Content function)

The latest version of PowerShell (version 5) and many earlier versions of PowerShell come with the Get-Content function and this function allows us to quickly read a file’s data. In the below script, we output an entire file’s data on the PowerShell ISE screen – a screen which we’ll be using for demonstration purposes throughout this article:

最新版本的PowerShell(版本5ÿ

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值