linux代码替换工具,comby:高效代码批量正则替换工具

comby

68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4170616368652d626c75652e73766768747470733a2f2f7472617669732d63692e636f6d2f636f6d62792d746f6f6c732f636f6d62792e7376673f6272616e63683d6d617374657268747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6769746875622f636f6d62792d746f6f6c732f636f6d627968747470733a2f2f696d672e736869656c64732e696f2f6769746875622f646f776e6c6f6164732f636f6d62792d746f6f6c732f636f6d62792f746f74616c2e7376673f636f6c6f723d6f72616e676568747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f636f6d62792d746f6f6c732f636f6d62792e73766768747470733a2f2f696d672e736869656c64732e696f2f6769747465722f726f6f6d2f636f6d62792d746f6f6c732f636f6d62792e7376673f636f6c6f723d7465616c

940c77eab9a35e526999440203f672b8.gif

A short example below shows how comby simplifies matching and rewriting compared to regex approaches like sed.

Need help writing patterns or have other problems? Consider posting in Gitter.

Install (pre-built binaries)

Mac OS X

brew install comby

Ubuntu Linux

bash

Arch and other Linux: The PCRE library is dynamically linked in the Ubuntu binary. For other distributions, like Arch, a fixup is needed: ln -s /usr/lib/libpcre.so /usr/lib/libpcre.so.3. Alternatively, consider building from source.

Windows

Install the Windows Subsystem for Linux and install Ubuntu. Then run bash

Docker

docker pull comby/comby

click to expand an example invocation for the docker image

Running with docker on stdin:

echo '(👋 hi)' | docker run -a stdin -a stdout -i comby/comby '(:[emoji] hi)' 'bye :[emoji]' lisp -stdin

6618f12159bc609bedc3788fd29dd217.png

Isn't a regex approach like sed good enough?

Sometimes, yes. But often, small changes and refactorings are complicated by nested expressions, comments, or strings. Consider the following C-like snippet. Say the challenge is to rewrite the two if conditions to the value 1. Can you write a regular expression that matches the contents of the two if condition expressions, and only those two? Feel free to share your pattern with @rvtond on Twitter.

if (fgets(line, 128, file_pointer) == Null) // 1) if (...) returns 0

return 0;

...

if (scanf("%d) %d", &x, &y) == 2) // 2) if (scanf("%d) %d", &x, &y) == 2) returns 0

return 0;

To match these with comby, all you need to write is if (:[condition]), and specify one flag that this language is C-like. The replacement is if (1). See the live example.

Build from source

Install opam

Create a new switch if you don't have OCaml installed:

opam init

opam switch create 4.09.0 4.09.0

Linux: sudo apt-get install pkg-config libpcre3-dev

Mac: brew install pkg-config pcre

Then install the library dependencies:

git clone https://github.com/comby-tools/comby

cd comby && opam install . --deps-only -y

Build and test

make

make test

If you want to install comby on your PATH, run

make install

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值