简体   繁体   English

如何使用PowerShell将txt文件中每行的第一个单词放入Excel工作表中?

[英]How do I take the first word from each line in txt file and put them into an Excel sheet using PowerShell?

I'm trying to use PowerShell to take the first word in each line of a .ctl file and put that word into an Excel sheet. 我正在尝试使用PowerShell在.ctl文件的每一行中采用第一个单词,并将该单词放入Excel工作表中。

I looked at this question How to Extract Strings from a text file and copy them to excel sheet using Powershell and it seems like it's what I need to do except that each word is unrelated to the others and I have no real knowledge of code. 我看了这个问题, 如何使用Powershell从文本文件中提取字符串并将其复制到excel表中 ,这似乎是我需要做的,除了每个单词与其他单词无关,并且我对代码没有真正的了解。 I've just been doing this manually, but there are hundreds of files. 我只是手动进行此操作,但是有数百个文件。

I found this, but I'm not sure what function would select the first word 我找到了,但是我不确定哪个函数会选择第一个单词

$lines = Get-Content "C:\ExampleFolder\Example.txt"

foreach ($line in $lines) {
    Write-Host ($line.Split(" "))[0]
}

An example line looks like this: 示例行如下所示:

branch_description NULLIF branch_description='    8]^NULL^[' "substr(:branch_description,6)",

I would just need the "branch_description" portion, the rest can be ignored. 我只需要“ branch_description”部分,其余的都可以忽略。

This is going to be performed on hundreds of files in a folder. 这将在一个文件夹中的数百个文件上执行。 How would I loop this? 我将如何循环播放?

Get-Content ".\file.txt" | %{ $_.Substring(0,$_.IndexOf(" "))} | Select-Object -First 10

Remove |Select-Object -First 10 to get the entire contents of the file. 除去|Select-Object -First 10以获取文件的全部内容。

This solution also assumes that you want to split on the space. 此解决方案还假定您要在空间上拆分。

If you add a comma to the end of the output you then you can just save the output as a .csv file that will open in excel. 如果在输出末尾添加逗号,则可以将输出另存为将在excel中打开的.csv文件。

#The default encoding for Out-File [pre v6] is 'Unicode'; in [ps6] it is 'UTF8NoBOM'.  In this example I chose ASCII, though you should choose what best fits your use-case. 
Get-Content ".\file.txt" | %{ $_.Substring(0,$_.IndexOf(" ")) + "," } | Out-file -Path ".\output.csv" -Encoding ASCII -Append

ref: Out-File 参考: 文件外

The complete code does it: 完整的代码可以做到:

$lines = Get-Content "C:\ExampleFolder\Example.txt"

Reads the File and stores an array of strings. 读取文件并存储字符串数组。 every line in the file is one element of the array 文件中的每一行都是数组的一个元素

foreach ($line in $lines) {

This part of the code is the beginning of a loop. 这部分代码是循环的开始。 It loops through the array of strings and does the following code in the brackets to every element in the array 它遍历字符串数组,并在方括号中对数组中的每个元素执行以下代码

  Write-Host ($line.Split(" "))[0]

Here comes the magic. 魔术来了。 Write-Host is just for the output on your powershell-window. Write-Host仅用于Powershell窗口中的输出。 You have to replace this command to something, that writes into your excel- or csv-file (or to store it in a array for later use) 您必须将此命令替换为写入excel-或csv文件的内容(或将其存储在数组中以备后用)

The rest of the command in the parathesis splits up the line (one line of your textfile) in an array of strings. 括号中的其余命令将行(文本文件的一行)拆分为字符串数组。 It uses space as symbol (" ") to split it up into another array of strings. 它使用空格作为符号(“”)将其拆分为另一个字符串数组。 The outer paranthesis are needed for the access identifier at the end of the codeline ([0]), which means, that you only want the first object. 代码行([0])末尾的访问标识符需要外部括号,这意味着您只需要第一个对象。

}

Closes the loop. 闭环。

Hope this helps 希望这可以帮助

edit: Examplecode: 编辑:范例代码:

Out-File -FilePath "myergfile.csv" -Encoding UTF8 -Force -InputObject "Value"
$lines = Get-Content "C:\ExampleFolder\Example.txt"
foreach ($line in $lines){
  Out-File -FilePath "myergfile.csv" -Encoding UTF8 -Append -InputObject ($line.Split(" "))[0]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Powershell从文本文件中提取字符串并将其复制到Excel工作表 - How to Extract Strings from a text file and copy them to excel sheet using Powershell 如何从 Excel 工作表中导出 txt 文件而末尾没有空行? - How can I export a txt file from an Excel sheet without having a blank line at the end? 如何从 a.txt 文件中获取文本并将其写入同一个 excel 文件的新工作表中 - How do I get the text from a .txt file and write it in a new sheet in the same excel file 如何使用 VBA 逐行读取文本文件并放入 excel 表格单元格? 读取线不工作 - How can i read a text file line by line and put into excel sheet cell using VBA? Read line is not working 如何将Word文件每个表格的第一行和最后一行复制到Excel工作表? - How to copy first and last row of each table of Word file to an Excel sheet? 如何为 Excel 文件中的每个工作表创建多个 DataFrame? - How do I create several DataFrames for each sheet in an Excel file? 使用apache POI读取Excel文件后,如何在txt文件中打印工作表? - Once I read a excel file with apache POI how do I print the sheet in a txt file? 从我的功率点图中获取数据点并将其放入Excel工作表 - Take data points from my power point graph and put them into an excel sheet 如何将 Excel 数据表作为分隔符冒号导出到 a.txt 文件? - How do I export an Excel data sheet to a .txt file as Delimiter colon? 如何从我的 Chrome 下载中获取 Excel 个文件并将它们同时组合为 Excel 工作簿中的选项卡? - How do I take Excel files from my Chrome Downloads and simultaneously combine them as tabs in an Excel Workbook?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM