0

I have this sample text file contains the following line

group1,name1
group2,name2
group3,name3

How to pass the value of first string to $group variable and second string to $name variable so I can use it in loop in the following script?

get-content data.csv -ReadCount 1000 | foreach { $_ -match "$group" } | Out-File $name.txt -encoding Utf8
7
  • What do you want to match it with Commented May 23, 2020 at 5:35
  • First string in every line of the text file. Eg group1, group2 etc Commented May 23, 2020 at 5:39
  • The title of your question seems unrelated to the contents of your question. Can you clarify your goal? Commented May 23, 2020 at 6:32
  • Updated title and question. Hopefully it's clear up the confusion. Commented May 23, 2020 at 6:44
  • This is unclear to me.. Why not simpy Import-Csv -Path 'data.csv' -Header Group, Name | ForEach-Object { $group = $_.Group ; $name = $_.Name; <# Do what needs to be done with these variabes #> } ?? Commented May 23, 2020 at 9:12

2 Answers 2

1

You can use:

$file = get-content data.csv
$group = @()
$name = @()
foreach($line in $file){
$line = $line -split ","
$group += $line[0]
$name += $line[1]
}
$name > name.txt 
$group > group.txt
# Use ">>" if name or group.txt already exist

This will take every line in the file and split it up using , as a delimiter then assign the first value into the $group array and the same with $name

Tested with exact csv file, working powershell version 5.1.18362.752

Update: If you want to pass the array line by line, you can use:

$file = get-content data.csv
$group = @()
$name = @()
foreach($line in $file){
$line = $line -split ","
$group += $line[0]
$name += $line[1]
}
for($i=0;$i -lt group.length;$i++){
$group[$i] >> group.txt
}
for($i=0;$i -lt name.length;$i++){
$name[$i] >> name.txt
}

With the added for loop, it passes the arrays line by line

Sign up to request clarification or add additional context in comments.

4 Comments

Appreciate that. Do you have any idea how to iterate those arrays to my script? It works if just printing to console using command such as Write-Output but if I'm using foreach or forloop to iterate it fails because it passing whole array. I need to pass the array line by line to this script get-content data.csv -ReadCount 1000 | foreach { $_ -match "$group" } | Out-File $name.txt -encoding Utf8
$line -split "," will fail if any of the fields in the csv contains a comma..
@Theo How will it fail?
@NekoMusume Because if a field contains a comma, like "Doe, John", splitting on comma will create two fields out of that, meaning the csv will not be valid anymore. This is why fields like that are quoted. Import-Csv can handle commas embedded in quotes and parses correctly.
1

as it is a csv why not use import-csv instead of get-content? furthermore, foreach is not necesary, you can filter with a simple where-object.

import-csv data.csv -delimiter "," -header group,name|where{$_.group -match "group1"}

1 Comment

group1 is just a placeholder. I want to replace it with variable of the value of first string in every line of the text file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.