如果我rg
直接在 bash 中运行,则输出信息包括找到的文件名(在单独的行上),然后在每个匹配的单独行上包含该匹配的行号和内容:
11:38 (main *+) durl $ rg format
durl/__main__.py
35: print(util.format_output(repo.url, line))
38: print(util.format_output(repo.url, line, linenumber=n))
durl/util.py
15:def format_output(prefix, filename: str, linenumber: str=None) -> str:
但是如果我从 python subprocess
run 运行相同的命令,匹配结果的显示信息包括每行的文件名,不包括行号。为什么呢?(我在 MacOS 上运行)
11:38 (main *+) durl $ ipython
Python 3.9.5 (default, May 4 2021, 03:36:27)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.27.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import subprocess
In [2]: p = subprocess.run('rg format', text=True, shell=True, capture_output=True,)
In [3]: p.stdout
Out[3]: 'durl/__main__.py: print(util.format_output(repo.url, line))\ndurl/__main__.py: print(util.format_output(repo.url, line, linenumber=n))\ndurl/util.py:def format_output(prefix, filename: str, linenumber: str=None) -> str:\n'
In [4]: p.stdout.split('\n')
Out[4]:
['durl/__main__.py: print(util.format_output(repo.url, line))',
'durl/__main__.py: print(util.format_output(repo.url, line, linenumber=n))',
'durl/util.py:def format_output(prefix, filename: str, linenumber: str=None) -> str:',
'']