19

GNU のlessユーティリティのmanページには、検索について次のように書かれています。

/pattern
    Search forward in the file for the N-th line containing the pattern.  N
    defaults to 1.  The pattern is a regular expression, as recognized by the
    regular expression library supplied by your system.

個人の Ubuntu ラップトップ、CentOS クラウド サーバー、職場の Cygwin など、あらゆる種類のシステムであまり使用しません。使用する構文。どうすればわかりますか?

4

3 に答える 3

11

It is a compile time parameter. The ./configure script of less knows the with-regex=LIB param.

This is a quote from README of the upstream package:

--with-regex=lib

     Specifies the regular expression library used by less for pattern
     matching.  The default is "auto", which means the configure program 
     finds a regular expression library automatically.  Other values are:
        posix          Use the POSIX-compatible regcomp.
        pcre           Use the PCRE library.
        regcmp         Use the regcmp library.
        re_comp        Use the re_comp library.
        regcomp        Use the V8-compatible regcomp.
        regcomp-local  Use Henry Spencer's V8-compatible regcomp
                       (source is supplied with less).

So you would need to know how less was './configured'. I have investigated this on Debian / Ubuntu. They use the POSIX regex lib.

I'm still searching for a way to detect it dynamically by a script... :)


Update: The only thing I've managed so far was to detect whether less uses pcre regexes or not. If less was configured using --with-regex=pcre it is linked against the libpcre.so shared library:

#!/bin/bash

# ldd prints out the shared libraries a binary is linked to.
# This can be used to check if less is linked against libpcre
if ldd "$(which less)" | grep 'libpcre\.so' ; then   
    echo "less uses pcre regex syntax"
else 
    echo "less uses non pcre regex syntax"
    # ... more checks should follow. currently trying to find a way
fi
于 2013-02-05T00:54:07.030 に答える
5

これがすべての場合(古いバージョン/異なるシステム)で機能するかどうかはわかりませんが、次を使用してこの情報を見つけることができましたless --version

less 458 (GNU regular expressions)
Copyright (C) 1984-2012 Mark Nudelman

less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less

つまり、GNU正規表現構文です...

そして、新しいバージョンをコンパイルした後、--with-regex=pcre私は得ました

less 481 (PCRE regular expressions)
...

アップデート

crw さん、チェックありがとうございます。この解決策はバージョン固有のようです。greenwoodsoftware (Linux)で入手可能なソース コードをコンパイルした後、バージョン 436 (2009 年 7 月 25 日リリース) 以前では動作しないことがわかりました。少なくとも 451 (2012 年 9 月 4 日リリース) 以降で動作を開始します。(これらの間のバージョンはダウンロードできませんでした)。

于 2016-07-28T23:33:25.563 に答える