0

I'm losing my mind trying to recreate the output of the following Linux command in a Windows Environment:

diff --old-line-format='%L' --new-line-format='' --unchanged-line-format='' file1 file2 | grep -v '^$'

I can't install anything, not even Notepad++ plugins, since it's a corporate server. All I got to work with is CMD or PowerShell (as far as I can think of)

Closest I've gotten is the following Powershell 'script':

$file1 = Get-Content "file1.txt"
$file2 = Get-Content "file2.txt"

$only_in_file1 = $file1 | Where-Object {
    $_ -notin $file2
}

$only_in_file1

But it fails to show the exact same output I'd get in Linux. Let me explain,

file1:

#AK.70.1.1.5
ServerTokens Prod
Header always unset "X-Powered-By"
Header unset "X-Powered-By"
#AK.70.1.1.6
ServerSignature Off
AddServerHeader Off

#ServerTokens Prod
#ServerSignature Off
#AddServerHeader Off

file2:

blah blah blah
durr durr durr
ServerTokens Prod
blah blah blah
durr durr durr
ServerSignature Off
AddServerHeader Off

Expected output (what I'd get in Linux):

#AK.70.1.1.5
ServerTokens Prod
Header always unset "X-Powered-By"
Header unset "X-Powered-By"
#AK.70.1.1.6
ServerSignature Off
AddServerHeader Off
#ServerTokens Prod
#ServerSignature Off
#AddServerHeader Off

Actual output (what I get from PowerShell)

#AK.70.1.1.5
Header always unset "X-Powered-By"
Header unset "X-Powered-By"
#AK.70.1.1.6
#ServerTokens Prod
#ServerSignature Off
#AddServerHeader Off

You see, although I commented file2 lines in file1, and pasted them on a different place, it stills considers them as if they weren't different.

I really cant Wrap my head around this topic. It's been like 4 hours now. Not kidding.

Any idea is welcome!

7
  • I quess, it has to do with different (OS) line terminitions where Get-Content reads multiple (or all) lines as one item. What returns: $file1.Count in either case? Commented Dec 3, 2024 at 8:17
  • Exactly your guess, both counts return '1': imgur.com/a/OfmidE2 Commented Dec 3, 2024 at 22:56
  • If I do split them, it does count the accurate number of lines: imgur.com/a/oNBWDCA Commented Dec 3, 2024 at 22:59
  • Buuuut if I do the 'diff' with the lines, it stills doesn't return the expected output: imgur.com/a/6ioy3Ze Commented Dec 3, 2024 at 23:04
  • @rubénruiz could you put the first 10 lines of both files? and an example of result you want? Maybe can help us :) Commented Dec 4, 2024 at 10:44

1 Answer 1

0
  • Your PowerShell code yields the same result as your diff (+ grep) command, because both report only those lines from file1.txt that don't also exist in file2.txt and aren't empty.

  • In other words: the actual output is the expected result of this logic, and it's unclear why you expect different output:

    • Your expected output suggests a different logic, which is: retrieve all lines from file1.txt that aren't empty; in other words: the content of file2.txt doesn't matter.

    • Implementing this logic would simply be the following, i.e. to retrieve all non-empty lines from file1.txt:

      @(Get-Content file1.txt) -ne ''
      
Sign up to request clarification or add additional context in comments.

Comments

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.