Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add windows support to the grep linter #75

Open
justinchuby opened this issue Aug 23, 2023 · 5 comments
Open

Add windows support to the grep linter #75

justinchuby opened this issue Aug 23, 2023 · 5 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@justinchuby
Copy link
Owner

justinchuby commented Aug 23, 2023

Potentially using Select-String and replace

Select-String -Pattern 'n' -AllMatches

@justinchuby
Copy link
Owner Author

ChatGPT:

In PowerShell, you can use the -replace operator to perform regular expression-based text replacements, which is somewhat similar to using sed with the -r option in Unix-like systems. However, PowerShell doesn't have an exact equivalent for the -r option in sed, which enables extended regular expressions.

Here's how you can use the -replace operator in PowerShell to perform regular expression-based text replacements:

# Basic usage of -replace without extended regular expressions
$text = "Hello, World!"
$newText = $text -replace "Hello", "Hi"

If you specifically need extended regular expressions, you can use the [regex]::Replace() method, which supports extended regular expressions:

# Using [regex]::Replace() with extended regular expressions
$text = "Hello, World!"
$newText = [regex]::Replace($text, "H.llo", "Hi")

In this example, we're using the . to match any character in place of the letter o, which is an example of extended regular expression syntax.

Keep in mind that while PowerShell's -replace operator and [regex]::Replace() method provide regular expression capabilities, they don't support all the features of extended regular expressions that -r does in sed. PowerShell's regular expressions are based on the .NET Framework's regular expression engine, which is different from the one used in sed.

@justinchuby
Copy link
Owner Author

In PowerShell, you can use the Select-String cmdlet to perform operations similar to grep in Unix-like systems. To achieve the equivalent of grep -nEHI, you can combine several parameters and options in Select-String as follows:

  • -Pattern: Specifies the regular expression pattern to search for.
  • -Path: Specifies the file(s) to search in.
  • -AllMatches: Returns all matches in each line (equivalent to -E in grep).
  • -IgnoreCase: Performs a case-insensitive search (equivalent to -i in grep).
  • -LineNumber: Shows line numbers for matched lines (equivalent to -n in grep).

Here's how you can use Select-String with these options to replicate the behavior of grep -nEHI:

Select-String -Pattern 'your_pattern' -Path 'your_files' -AllMatches -IgnoreCase | Select-Object -Property LineNumber,Line

Replace 'your_pattern' with the regular expression you want to search for, and 'your_files' with the path to the file(s) you want to search in.

Here's a breakdown of what each part does:

  • Select-String -Pattern 'your_pattern' -Path 'your_files' -AllMatches -IgnoreCase searches for the specified pattern in the specified file(s) with case-insensitivity and returns all matching occurrences.
  • Select-Object -Property LineNumber,Line then formats the output to include line numbers and the matched lines.

This PowerShell command should give you similar functionality to grep -nEHI in Unix-like systems.

@justinchuby justinchuby added good first issue Good for newcomers enhancement New feature or request labels Dec 6, 2023
@guotuofeng
Copy link

could we use Python to implement the grep like https://stackoverflow.com/questions/1921894/grep-and-python?

@guotuofeng
Copy link

Basically, the grep -nEHI '[[:blank:]]$' behaves differently in Windows, Linux and Windows WSL. From the time being, the search only works in Linux.

In Windows, I have to use [[:space:]] for the match. in WSL, [[:blank:]] doesn't match anything and [[:space:]] would print lots of empty lines.

Windows grep using https://gnuwin32.sourceforge.net/packages/grep.htm:
image

WSL:
image

@justinchuby
Copy link
Owner Author

could we use Python to implement the grep like stackoverflow.com/questions/1921894/grep-and-python?

I think so! There's going to be some performance penalties, but better for it to work than failing I guess

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants