简体   繁体   English

将目录中的TXT文件合并为一个文件,并在文件末尾添加一列作为文件名

[英]Combine TXT files in a directory to one file with column added at end with for file name

I have got a set of txt files in a directory that I want to merge together. 我要合并在一起的目录中有一组txt文件。

The contents of all the txt files are in the same format as follows: 所有txt文件的内容均采用以下相同格式:

IPAddress   Description             DNSDomain
---------   -----------             ---------
{192.168.1.2}   Microsoft Hyper-V   Network Adapter     
{192.168.1.30}  Microsoft Hyper-V   Network Adapter #2

I have the below code that combines all the txt files in to one txt file called all.txt. 我有以下代码,将所有txt文件组合到一个名为all.txt的txt文件中。

copy *.txt all.txt

From this all.txt I can't see what lines came from what txt file. 从这个all.txt文件中,我看不到什么行来自什么txt文件。 Any ideas on any bits of code that would add an extra column to the end txt file with the file name the rows come from? 关于将在行文本来自的文件名添加到最终txt文件中的额外列的任何代码中的任何想法?

As per the comments above, you've put the output of Format-Table into a text file. 根据上面的注释,您已将Format-Table的输出放入文本文件中。 Note that Format-Table might be visually structured on screen, but is just lines of text. 请注意, Format-Table可能在屏幕上具有视觉上的结构,但仅仅是文本行。 By doing that you have made it harder to work with the data. 这样一来,您将很难处理数据。

If you just want a few properties from the results of the Get-WMIObject cmdlet, use Select-Object which (in the use given here) will effectively filter the data for just the properties you want. 如果只想从Get-WMIObject cmdlet的结果中Get-WMIObject一些属性,请使用Select-Object (在此处给定的用法),该Select-Object将有效地仅过滤所需属性的数据。

Instead of writing text to a simple file, you can preserve the tabular nature of the data by writing to a structured file (ie CSV): 您可以通过写入结构化文件(即CSV)来保留数据的表格性质,而不是将文本写入简单文件:

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName SERVERNAMEHERE |
  Select-Object PSComputerName, IPAddress, Description, DNSDomain |
  Export-Csv 'C:\temp\server.csv'

Note that we were able to include the PScomputerName property in each line of data, effectively giving you the extra column of data you wanted. 请注意,我们能够在每行数据中包含PScomputerName属性,从而有效地为您提供了所需的额外数据列。

So much for getting the raw data. 获取原始数据就这么多。 One way you could read in all the CSV data and write it out again might look like this: 您可以读取所有CSV数据并再次将其写出的一种方式可能如下所示:

Get-ChildItem *.csv -Exclude all.csv |
    Foreach-Object {Import-Csv $_} |
    Export-Csv all.csv

Note that we exclude the output file in the initial cmdlet to avoid reading and writing form/to the same file endlessly. 请注意,我们在初始cmdlet中排除了输出文件,以避免不断地将form /写入同一文件。

If you don't have the luxury to collect the data again you'll need to spool the files together. 如果您没有足够的精力再次收集数据,则需要将文件后台处理在一起。 Spooling files together is done with Get-Content , something like this: 将文件后台处理在一起是通过Get-Content ,如下所示:

Get-ChildItem *.txt -Exclude all.txt |
    Foreach-Object {Get-Content $_ -Raw} |
    Out-File all.txt

In your case, you wanted to suffix each line, which tricker as you need to process the files line-by-line: 在您的情况下,您想在每行后缀,当您需要逐行处理文件时,这会更麻烦:

$files = Get-ChildItem *.txt

foreach($file in $files) {
    $lines = Get-Content $file
    foreach($line in $lines) {
        "$line $($file.Name)" | Out-File all.txt -Append
    }
}

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

相关问题 将一个目录中的多个文件合并为一个文件 - Combine multiple files in a directory into one file 如何将多个文本文件(特定行+元数据)组合成一个.txt或.csv文件和PowerShell? - How can I combine multiple text files (specific line + metadata) into one .txt or .csv file with PowerShell? Powershell帮助,如何将目录中的文本文件合并为一个文件? - Powershell help, how can I combine text files in a directory to one file? 按文件名末尾的数字对文件进行排序 - Sort files by number in the end of a file name Powershell 搜索目录有文本匹配的代码文件输入一个txt文件 - Powershell search directory for code files with text matching input a txt file 将目录中的文件复制到与文件同名的文件夹中 - Copy files in a directory, to folders with same name as file Powershell - 根据文件名将文件分类到目录中 - Powershell - Sort files into directory based on file name 将子文件夹文件合并为一个文件(不包括当前文件夹文件) - Combine subfolder files into one file (excluding current folder files) 将.txt文件合并到一个.doc文件中,添加文件名和分页符 - Merge .txt files in one .doc, adding file names and page breaks 将文本文件复制到其他位置,然后将它们合并为一个文本文件 - copy text files in different location and combine them to one text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM