What does grep -Po '... \ K ...' do? How else can this be achieved?

I have this script script.sh

:

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

      

and this file text.txt

:

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

      

When I run the command ./script.sh /file/directory/text.txt

, the output in the terminal is as follows:

a b c

      

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

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

      

If I understood correctly, \K

this is the Perl command. Can you give me a shell alternative (using a command for example awk

)?


Thanks in advance.

+3


source to share


1 answer


  • grep -P

    allows the syntax PCRE. (This is a non-standard extension - not even all GNU grep builds support it, as it depends on the additional libpcre library and whether or not to reference it in a compile-time option.)
  • grep -o

    selects only the text and not the entire line containing the specified text in the output. (This is also non-standard, although more widely available than -P

    ).
  • \K

    is a PCRE extension to the regex syntax that discards content until it is included in the match output.

Since your shell is bash, you have ERE support built in. Alternatively, only the built-in functionality is used (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 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

      




See http://wiki.bash-hackers.org/syntax/ccmd/conditional_expression for discussion =~

and http://wiki.bash-hackers.org/syntax/shellvars#bash_rematch for discussion BASH_REMATCH

. See BashFAQ # 1 for a discussion of reading files in turn with a loop while read

.

+3


source







All Articles