iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
✨

I created purl as an alternative to sed and grep

に公開1件

I made this tool.

https://github.com/catatsuy/purl

It is written in Go.

I will briefly introduce why I created it and what features it has.

Challenges so far

sed and grep have the following issues:

  • Regular expression syntax is not Perl-compatible, making it difficult to use
  • sed is not compatible between macOS and Linux (BSD and GNU versions lack compatibility), making it easy to write OS-dependent code

To avoid these issues, I have been using Perl one-liners. Perl's regular expressions are easy to use and allow for flexible specifications. It is also less likely to be OS-dependent. However, Perl has the following problems:

  • It is not installed by default on recent macOS or Linux distributions, making it inconvenient to use
    • This is especially noticeable in container environments like Docker
  • Usage examples have been decreasing recently, so it tends to be avoided
  • Since it is not a dedicated one-liner tool, the option names are difficult to understand

Therefore, I believe there is a need for a tool that:

  • Uses easy-to-use, Perl-compatible regular expressions
  • Is OS-independent
  • Is lightweight and easy to install
  • Has clear and intuitive option names

That is why I created purl.

Features of purl

  • Can flexibly handle inputs from standard input, files, etc.
    • Can also flexibly choose between standard output and files for output
  • Provides flexible and easy-to-understand options
    • Clear option names like -replace, -filter, and -exclude
  • Can overwrite the file itself using the -overwrite option
  • The -filter option automatically applies coloring when outputting to the terminal
    • This can also be specified using the -color and -no-color options

Demo

I have posted a video on GitHub.

https://github.com/catatsuy/purl?tab=readme-ov-file#demo

Installation

Binaries for macOS and Linux are distributed. If a Go development environment is available, you can also install it via go install. Please refer to GitHub.

https://github.com/catatsuy/purl?tab=readme-ov-file#installation

Origin of the Name

The name "Purl" is inspired by knitting techniques and the sound of a purling stream. In knitting, a "purl" refers to a repetitive technique used to create smooth fabric. A stream's "purl" is characterized by its continuous and calm flow. "Purl" signifies that this tool performs text transformations smoothly and assists tasks without interruption, much like a repetition or a flow.

(I came up with this name after consulting with ChatGPT.)

Usage Examples

Previewing changes before applying

purl -replace "@search@replace@" yourfile.txt

This command searches for "search" within yourfile.txt and displays how it will be replaced with "replace", but it does not modify the file itself.

Modifying files directly

purl -overwrite -replace "@search@replace@" yourfile.txt

When using the -overwrite option, Purl replaces "search" with "replace" within yourfile.txt and saves the changes to the file.

Using standard input

Purl can receive input from other commands via pipes and use it flexibly:

cat yourfile.txt | purl -replace "@search@replace@"

This sends the contents of yourfile.txt to Purl and displays the text modified according to the specified replacement pattern.

Using multiple files

Purl supports processing multiple files at once, allowing you to apply operations across multiple documents. Simply list the files at the end of the command.

purl -replace "@search@replacement@" file1.txt file2.txt file3.txt

This command applies the replacement operation to file1.txt, file2.txt, and file3.txt.

Using -filter

purl -filter "error" yourlog.log

This command filters lines containing "error" in yourlog.log and displays them with coloring for better visibility.

Filtering input by multiple criteria

To filter lines that match multiple criteria, use the -filter option multiple times. This works both when reading from a file and when processing standard input.

purl -filter "error" -filter "warning" yourlog.log

Or via standard input:

cat yourlog.log | purl -filter "error" -filter "warning"

This will display lines from yourlog.log that contain either "error" or "warning".

Excluding lines that match multiple patterns

Similarly, to exclude lines that match multiple patterns, specify -exclude multiple times:

purl -exclude "debug" -exclude "info" yourlog.log

Or via pipe input:

cat yourlog.log | purl -exclude "debug" -exclude "info"

This method will exclude lines containing either "debug" or "info" from the output.

When using the -i option, Purl can match in filters and exclusions without distinguishing between uppercase and lowercase.

purl -i -filter "error" yourfile.txt

This command matches lines containing the word 'error' in yourfile.txt, including case variations such as 'Error', 'ERROR', or 'error'.

Combining git grep and xargs

To apply changes to multiple files in a Git repository, use the following procedure:

git grep -l 'search_pattern' | xargs purl -overwrite -replace "@search_pattern@replace_text@"

This finds all files containing 'search_pattern' and uses Purl to replace it with 'replace_text', applying the changes directly to the files.

Finally

It's convenient and intuitive to use, even for simply rewriting files, as shown below.

https://github.com/catatsuy/private-isu/pull/473/files

I think it's quite useful, so I'd be happy if you give it a try. Thank you.

Discussion

kkddkkdd

こんにちは。
grep には perl 拡張オプション付きなので、sed でも同様になるならば、相当改善されるだろうとも思いました(sed では改行出力にも苦労しますし)。本題から離れて失礼しました。