0

I have a text file with Names and a textfile with names and numbers it looks like this:

a.txt:

Anna
Edward
Markus
Jenny
Jens

b.txt:

23434Anna
65790569Edward
4343Johann
2432343Hannah
2458435Jens

So this is only a example. List a is like 500 lines and list b 2000.

So what i want to do is compare the two lists and everytime a line of "b.txt" contains a line from "a.txt" (like "Anna" is in "23434Anna") i want to replace the line in "b.txt" with the line of "a.txt". So this line should be changed to just "Anna".

i tried stuff like

ForEach($a in $b) {$a -replace $b}

So i know this is kinda nonense but i want to make clear what i am trien to do. I need ofc a way to seperate the specific lines to unique objects. I know that can be done throug indexing... But i am not sure how to loop through the index of a variable.

Greetings

2 Answers2

1

try this

#extract list value to search
$listval=gc "C:\temp\a.txt"

#For every row, search in list and export into same file
gc "C:\temp\b.txt" | %{
$row=$_

#get first element in list where current content of row like value
$founded=($listval | where {$row -like "*$_*"} | select -First 1)

#if number of element founded is great 0 , print founded element on output    
if ($founded.count -gt 0)
{
    $founded
}
#else print current element without modification
else
{
   $row 
}

} | 
#redirect result into new file
out-file "C:\temp\c.txt"

in your example you should be use '-clike' and not '-like' for case sensitive, because Hannah contain anna ;)

Esperento57
  • 16,521
  • 3
  • 39
  • 45
  • Its working. Can you explain what you did there? looks confusing to me since i am a beginner in powershell. – Darude Sandstorm Dec 20 '16 at 11:07
  • The Hannah Anna thing will not happen for me, it was just a bad example. – Darude Sandstorm Dec 20 '16 at 11:07
  • 1
    @Esperento57 please consider to accept my edit. It makes the code much clearer and easier to read and understand. – n01d Dec 20 '16 at 11:16
  • gc and get-content are equals commands, gc is an alias and beginner must learn it too – Esperento57 Dec 20 '16 at 11:25
  • I don't aggree with that. You shouldn't use aliases in your scripts at all. – Martin Brandl Dec 20 '16 at 11:46
  • Aliases have theire use when working with the PowerShell **prompt**. But they shouldn't been used in scripts for various reasons. There are plenty arcticles about that topics from PowerShell MVPs and even one from Ed Wilson. This is the second time you are not accepting a good mentioned advice. – Martin Brandl Dec 20 '16 at 12:26
  • And? I accept when i agree, i refuse when i dont agree... It's the principe of response for a proposition... in this post you use select (select is an alias of select-object) http://stackoverflow.com/questions/38891211/select-attributes-or-parameter-with-variable-in-powershell/38891234#38891234, in this where (=where-object alias) http://stackoverflow.com/questions/39517654/removing-a-single-line-from-csv/39517710#39517710 – Esperento57 Dec 20 '16 at 12:41
  • You can not condemn someone because he has a difference of point of view with you. What's more you made two weight two measure on the use of aliases. Do not use created alias I agree with you, but dont use the native alias I do not agree with you and it is my right – Esperento57 Dec 20 '16 at 12:41
  • if you read this, https://blogs.technet.microsoft.com/heyscriptingguy/2013/04/08/using-powershell-aliases-best-practices/, Ed Wilson use alias for write script and have a script for replace all alias by complet command when his script is ended. In any case it is good to learn the best known alias so do not be surprised if we reread a script that does not belong to us, these default aliases being part of the language properly spoken – Esperento57 Dec 20 '16 at 12:50
  • You even advised to use pwd by specifying that it meant get-location in this post http://stackoverflow.com/questions/31754931/how-do-i-get-the-current-directory-of-the-prompt / 31754938 # 31754938 Which is much more serious because pwd is a linux command and powershell becomes multi-platform – Esperento57 Dec 20 '16 at 12:55
  • I have close to 1k answers on questions with PowerShell tag. Of course you will find some questions where I am using aliases - especially on older posts, but I try not to use them. Your scripts are hard to read for everyone who don't have much PowerShell experience which probably applies to most of the users aksing questions here. And sadly it looks like you don't want to improve yourself. – Martin Brandl Dec 20 '16 at 13:45
  • Stop being bad, your use of the command 'pwd' date last July 31 so it's not that old. And it is not the number of questions answered that makes your arguments better than others. For my part, improving does not mean accepting without thinking all the proposals but weighing the pros and cons, let alone accepting critics with the sole argument that everyone should do like that or that beginners will read the code and Will not understand it. We do not level things down. – Esperento57 Dec 20 '16 at 14:44
  • I do not think I have overused the aliases in this code, stop chipping. Purists impose their thoughts by thinking them better and by thinking they are open-minded, this is well known. – Esperento57 Dec 20 '16 at 14:44
  • Note that I learned the aliases on this site, it is ironic no? – Esperento57 Dec 20 '16 at 14:48
1

other solution, better because it test equality and not like (for Hannah and anna)

#extract list value to search
$listval=gc "C:\temp\a.txt"

#template for describe cut for file
$template=@"
{Word*:{id:12}{Name:Abc}}
{Word*:{id:456}{Name:eFG}}
"@

#cut file with template and test for result
gc "C:\temp\b.txt" | ConvertFrom-String -TemplateContent $template | 
select @{N="Result";E={if ($listval -contains $_.Word.Name) {$_.Word.Name} else {$_.Word.ID+$_.Word.Name}}}
Esperento57
  • 16,521
  • 3
  • 39
  • 45