1

I have a .txt file, listing some user names.

Like:

User1 
User2 
User3

I also have a .txt file, containing some of the same user names, along side other information:

User1 - user1@txt.com - 12341515 - yes
User2 - user2@txt.com - 13134141 - no

How do I search from the first file, the users in the second file, then output the complete row of the second file in a new txt file.

I tried, Get-content and |where-object, but I don't know how to search exactly.

The files do not have a header.


Update

The file1, where are the user that i want to find is like this

u16096

The original file2, where all the user info are have this order.

u16096      Y   NAME SURNAME    name.surname@what.com           12345678
Mr Printer
  • 29
  • 6
  • In `powershell.exe`, I would use the built-in `findstr.exe` utility, _in exactly the same way as I would in `cmd.exe`_: `findstr.exe /IG:"L:\ocation\first.txt" "P:\athTo\second.txt"` – Compo Oct 26 '20 at 16:25
  • Thank you for your comment also, i tried this , but it´s not working with my two files – Mr Printer Oct 26 '20 at 19:26
  • Well it does work perfectly in both `cmd.exe` and `powershell.exe` using exactly the content you've provided. Without seeing your actual files, it's impossible for me to determine the specific reason. With your commented example under the existing answer, it would look more like this `findstr.exe /IG:"C:\temp\test2\results5.csv" "C:\temp\test2\userstotal.txt"`. This would take each line of `C:\temp\test2\results5.csv` which should be a list of users, one per line as in your first text file example, and output each line of `C:\temp\test2\userstotal.txt` which matches, in part, those strings. – Compo Oct 26 '20 at 19:47

1 Answers1

0

This is how I'd do it. Explanation in comments. I cannot exactly test it, I do not have your original files, but it should get you started.

# store the values of the 2nd text file in a lookup-map
$userInfos = @{}
Get-Content "file2.txt" | foreach {
    # use the username as lookup key
    $username = $_ -replace '^(\S+).*', '$1'
    $userInfos[$username] = $_
}

# iterate the 1st file, lookup the user infos, and output to 3rd file
Get-Content "file1.txt" | foreach {
    $userInfos[$_]
} | Out-File "file3.txt"
marsze
  • 15,079
  • 5
  • 45
  • 61
  • Thank you for the fast reply ,but I must be doing something wrong, cause , the 3rd file is empty. Please fill me an example. – Mr Printer Oct 26 '20 at 19:20
  • This is how i put it. # store the values of the 2nd text file in a lookup-map $userInfos = @{} Get-Content "C:\temp\test2\userstotal.txt" | foreach { # use the username as lookup key $username = $_ -replace '^(\S+).*', '$1' $userInfos[$username] = $_ } # iterate the 1st file, lookup the user infos, and output to 3rd file Get-Content "C:\temp\test2\results5.csv" | foreach { $userInfos[$_.Trim()] } | Out-File "C:\temp\test2\usertotal_result.txt" – Mr Printer Oct 26 '20 at 19:20
  • ,ok, i took the out file. Nothing came out, and i use the same example. In this part, $userInfos = @{what do you put here}? – Mr Printer Oct 26 '20 at 21:56
  • ok it´s working. I found what was wrong. It works with my example, but not with the original files :( Obviously the original files contains private data and i cannot show up here,. Let me ask you, it might fail if the size of the txt 2 file is bigger than 2 MB? Or maybe it fails because instead of - in the original file , are spaces. I will update my question so you can see. – Mr Printer Oct 27 '20 at 07:32
  • ok so it is the size, with less user its working good. Can i make it work with a 2mb txt file? – Mr Printer Oct 27 '20 at 07:42