简体   繁体   中英

Why ripgrep results from python subprocess run differ from rg run in the shell

If I run rg in bash directly, the output information includes the found file names (on a separate line), and then on a separate line for each match the line number and content for that match:

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:

But if I run the same command from python subprocess run, the displayed information of the matched results includes the filename on each line and do not include a line number. Why is that so? (I am running on 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:',
 '']

Because ripgrep detects if an interactive tty exists on stdout, and if so, changes the output format to be more "friendly." It's the same thing that ls does for example.

They are the same. In your example, you are looking at how Python represents the string, but if you actually print it, you'll see that it's exactly the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM