0

i have two files first.txt contains :

1  
2
3
4
6

Second.txt contains :

1
4
2

how i can delete all lines in first.txt that contains second.txt lines and save them to third file

Note: second.txt is part of first.txt

0

Verbose

$One = Get-Content First.txt
$Two = Get-Content Second.txt
Compare-Object -ReferenceObject $One -DifferenceObject $Two |
    select -expand InputObject |
Set Content Out.txt

Compact

( Compare (gc one.txt) (gc two.txt) ).InputObject | sc Out.txt

Get-Content retrieves the file contests as an array of lines.

Compare-Object does just what its name implies!

| improve this answer | |
0

here's another way to get the "in 1st, not in 2nd" listing. [grin]

what it does ...

  • fakes reading in two text files
    when ready to do this for real, remove the entire #region/#endregion block and use Get-Content to load the files.
  • uses the .Where() collection method on the 1st collection to filter out the items that are not in the 2nd collection
  • assigns the remaining items to $ThirdFile
  • displays that on screen

i presume you will have no problem saving the result to a file, so i leave that to you. [grin]

the code ...

#region >>> fake reading in a text file
#    in real life, use Get-Content
$FirstFile = @'
1
2
3
4
6
'@ -split [System.Environment]::NewLine
$SecondFile = @'
1
4
2
'@ -split [System.Environment]::NewLine
#region >>> fake reading in a text file

$ThirdFile = $FirstFile.Where({$_ -notin $SecondFile})

$ThirdFile

output ...

3
6
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.