-1

I currently have two arrays full of names. One contains data read in from a txt file, and one read in from a CSV file. I'm trying to return lists of names that appear on both lists, and that only appear on individual lists. For the CSV file, PowerShell originally wanted to place "Name=" in front of each item, but I used $formattedName = $csvFile -replace "Name=","" to get rid of it. I should also mention that Name is the header for that column. At this point, if I call the variable $formattedName, it returns the list of names without Name= in front of them (aka exactly how the txt if formatted to compare).

Now when trying to use Compare-Object, I'm running into an issue. My code reads:

Compare-Object -ReferenceObject $txtFile -DifferenceObject $formattedName -IncludeEqual

Now, when running this command, PowerShell still places Name= in front of each item for the CSV file, regardless of the fact that I just formatted it to not do that. My end result is two lists, one containing all items from the txt file and one containing all items from the CSV file. There are no two matching values because Steven is not the same as Name=Steven. An example of the output is:

Steven =>
Name=Steven <=

How can I go about fixing this? Am I using this properly?

JK72
  • 149
  • 1
  • 8
  • 1
    Hi, and welcome to StackOverflow! Can you share some sample input? There's not really enough information/code/data/details in your post to reproduce what you're describing accurately. Please consult the [MCVE help page](https://stackoverflow.com/help/mcve) and [update your question](https://stackoverflow.com/posts/53782418/edit) :) – Mathias R. Jessen Dec 14 '18 at 15:33
  • please provide a sample [two or three lines] from each source. right now, there is no way to determine what is actually happening. [*grin*] – Lee_Dailey Dec 14 '18 at 15:33
  • Hello, I'll update my original post with more details – JK72 Dec 14 '18 at 17:09

1 Answers1

0

Without proper examples of both your CSV file and the text file, from your question I think I can make out that your CSV looks something like this:

"Surname","Name"
"Seagal","Steven"
"Rambo","John"
"Schwarzenegger","Arnold"

Looking at the output you've shown, I gather the textfile is just an bunch of (first) names, each on a separate line like this:

Steven
Sylvester
John

In order to compare them you need to filter out only the Name column from your CSV file, so it will match the array you read in from the text file. (You DO say "I should also mention that Name is the header for that column.")

Try this then:

# import the .csv file and extract only the 'Name' column from it as string array
$csv = (Import-Csv -Path "D:\blah.csv").Name

# read the .txt file which apparently contains only firstnames, each on a separate line
$txt = Get-Content -Path "D:\names.txt"

Compare-Object -ReferenceObject $txt -DifferenceObject $csv -IncludeEqual

Running this on the example files I gave, the output is:

InputObject SideIndicator
----------- -------------
Steven      ==           
John        ==           
Arnold      =>           
Sylvester   <=  

Where

  • 'Steven' and 'John' appear in both files,
  • 'Arnold' is only found in the CSV file and
  • 'Sylvester' is in the text file, but not in the CSV.
Theo
  • 57,719
  • 8
  • 24
  • 41