Powershell导入时跳过txt文件的前2行 [英] Powershell skip first 2 lines of txt file when importing it

查看:24
本文介绍了Powershell导入时跳过txt文件的前2行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 powershell 脚本,旨在读取远程服务器上的 txt 文件并将其导入 SQL.

I have a powershell script designed to read a txt file on a remote server and import it into SQL.

我希望能够跳过 txt 文件的前 2 行.我目前正在使用下面的代码来导入文件.txt文件被分隔

I want to be able to skip the first 2 lines of the txt file. I am currently using the code below to import the file. The txt file is delimited

$datatable = new-object System.Data.DataTable
$reader = New-Object System.IO.StreamReader($empFile)     
$columns = (Get-Content $empfile -First 1).Split($empFileDelimiter) 

    if ($FirstRowColumnNames -eq $true) 

        { 
            $null = $reader.readLine() 

        } 

    foreach ($column in $columns) 

        {  
            $null = $datatable.Columns.Add() 
        } 

    # Read in the data, line by line, not column by column 
    while (($line = $reader.ReadLine()) -ne $null)  

        { 

            $null = $datatable.Rows.Add($line.Split($empFiledelimiter)) 

column 参数取 txt 文件的第一行,并为 PS 数据表创建列.

The column parameter takes the first line of the txt file and creates the columns for the PS datatable.

我遇到的问题是不需要 txt 文件的前两行,我需要跳过它们并使用 txt 文件的第三行作为列.我有以下代码行可以执行此操作,但我不确定如何将其集成到我的代码中.

The problem I have is the first two lines of the txt file are not needed and I need to skip them and use the third line of the txt file for the columns. I have the following line of code which will do this but I am uncertain how to integrate it into my code.

get-content $empFile | select-object -skip 2 

推荐答案

$empfile 创建一个没有前两行的数组,然后使用数组的第一项作为 Columns,如这个:

Create an array for the $empfile without the first two lines, then use the first item of the array for the Columns, like this:

$Content = Get-Content $empFile | Select-Object -Skip 2 
$columns = $Content[0].Split($empFileDelimiter)

这篇关于Powershell导入时跳过txt文件的前2行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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