简体   繁体   中英

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.

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. 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.

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.

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.

#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. 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)

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.

}

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]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM