grep -Po'... \ K ...'有什么作用?还能如何达到这种效果呢? [英] What does grep -Po '...\K...' do? How else can that effect be achieved?

查看:2266
本文介绍了grep -Po'... \ K ...'有什么作用?还能如何达到这种效果呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个脚本script.sh:

#!/bin/bash
file_path=$1
result=$(grep -Po 'value="\K.*?(?=")' $file_path)
echo $result

和此文件text.txt:

value="a"
value="b"
value="c"

当我运行./script.sh /file/directory/text.txt命令时,终端中的输出如下:

When I run ./script.sh /file/directory/text.txt command, the output in the terminal is the following:

a b c

我了解脚本的功能,但是我不了解它的工作原理,因此我需要对这部分命令进行详细的说明:

I understand what the script does, but I don't understand HOW it works, so I need a detailed explanation of this part of command:

-Po 'value="\K.*?(?=")'

如果我理解正确,那么\K是Perl命令.您能给我一个替代的shell方法吗(例如,使用awk命令)?

If I understood correctly, \K is a Perl command. Can you give me an alternative in shell (for example with awk command)?

谢谢.

推荐答案

  • grep -P启用PCRE语法. (这是一个非标准扩展-甚至不是所有的GNU grep构建都支持它,因为它取决于可选的libpcre库,是否将其链接是一个编译时选项).
  • grep -o在输出中仅发出匹配的文本,而不发出包含该文本的整行. (尽管它比-P更为广泛,但这也是非标准的.)
  • \K是对正则表达式语法的PCRE扩展,它丢弃了该点之前的内容以使其不包含在匹配输出中.
    • grep -P enables PCRE syntax. (This is a non-standard extension -- not even all builds of GNU grep support it, as it depends on the optional libpcre library, and whether to link this in is a compile-time option).
    • grep -o emits only matched text, and not the entire line containing said text, in output. (This too is nonstandard, though more widely available than -P).
    • \K is a PCRE extension to regex syntax discarding content prior to that point from being included in match output.
    • 由于您的外壳是bash,因此您内置了ERE支持.作为一种仅使用内置功能(不使用外部工具,grepawk或其他方式)的替代方法:

      Since your shell is bash, you have ERE support built in. As an alternative that uses only built-in functionality (no external tools, grep, awk or otherwise):

      #!/usr/bin/env bash
      regex='value="([^"]*)"'                    # store regex (w/ match group) in a variable
      results=( )                             # define an empty array to store results
      while IFS= read -r line; do             # iterate over lines on input
        if [[ $line =~ $regex ]]; then        # ...and, when one matches the regex...
          results+=( "${BASH_REMATCH[1]}" )   # ...put the group's contents in the array
        fi
      done <"$1"                              # with stdin coming from the file named in $1
      printf '%s\n' "${results[*]}"           # combine array results with spaces and print
      


      请参见 http://wiki.bash-hackers.org/syntax/ccmd/对=~ http://wiki进行讨论的conditional_expression . bash-hackers.org/syntax/shellvars#bash_rematch 中有关BASH_REMATCH的讨论.有关使用while read逐行读取文件的讨论,请参见 BashFAQ#1 .循环.


      See http://wiki.bash-hackers.org/syntax/ccmd/conditional_expression for a discussion of =~, and http://wiki.bash-hackers.org/syntax/shellvars#bash_rematch for a discussion of BASH_REMATCH. See BashFAQ #1 for a discussion of reading files line-by-line with a while read loop.

      这篇关于grep -Po'... \ K ...'有什么作用?还能如何达到这种效果呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆