by bspammer · 4y ago · 0 comments
I think this is more comprehensible, and is also more robust because it actually specifies the field you're looking for rather than just any line with quotes:
gawk -F'"' '/^ name:/ {print $2}' appVersion.gradle
(hacker news is clobbering the spaces after ^)
Indent with 2 spaces to get code formatting: https://news.ycombinator.com/formatdoc
Personally, I’d probably rather use sed here:
sed -nE 's/^\s*name:\s*"([^"]*)"\s*$/\1/p"
While that regex is more complex, it is also safer and more explicit. grep -oP -m1 'name:\s*"\K[^"]+'Explanations for the beginner and intermediate regex and grep user:
`-o`: Only return the match, instead of the entire line
`-P`: use Perl compatible regex
`-m` max-count, Stop reading a file after NUM matching lines.
And now for the regex:
`name:`: find the exact match
`\s*"`: Zero or more spaces leading up to and including an double quote
`\K`: This was the kicker for me. "resets the starting point of the reported match. Any previously consumed characters are no longer included in the final match" - basically tells the regex engine that the characters _before_ `\K` needs to be there in order to form a match, but it should only return the characters _after_ `\K` as the match. This is super handy! Is there a "reverse \K"?
`[^"]+`: One or more characters that are not a double quote. This basically means "Find the line that has a key called "name" and return all the characters after the first double quote and until the last double quote"
What do you mean by "reverse \K"? Are you aware of lookarounds? Perhaps you meant positive lookahead?
# match digits only if there is a semicolon afterwards
$ echo '12; 42,31;100' | grep -oP '\d+(?=;)'
12
31
[0] https://learnbyexample.github.io/learn_gnugrep_ripgrep/intro...Look up lookahead and lookbehind (https://www.regular-expressions.info/lookaround.html, https://perldoc.perl.org/perlre#Extended-Patterns)
Until the next double quote, not necessarily the last one.
The word "often" in the parent comment implies there are times when GNU grep is not better suited than sed with ERE. (I often use flex instead of sed.)
For example,
https://debbugs.gnu.org/cgi/bugreport.cgi?bug=44754
Or from the 3.8 manual:
6.1 Known Bugs
Large repetition counts in the `{n,m}' construct may cause grep to use lots of memory. In addition, certain other obscure regular expressions require exponential time and space, and may cause grep to run out of memory.
Back-references can greatly slow down matching, as they can generate exponentially many matching possibilities that can consume both time and memory to explore. Also, the POSIX specification for back-references is at times unclear. Furthermore, many regular expression implementations have back-reference bugs that can cause programs to return incorrect answers or even crash, and fixing these bugs has often been low-priority: for example, as of 2021 the GNU C library bug database contained back-reference bugs 52, 10844, 11053, 24269 and 25322, with little sign of forthcoming fixes. Luckily, back-references are rarely useful and it should be little trouble to avoid them in practical applications.
Because, among other things, sed has filtering features like based on line numbers, address range, etc that aren't present in grep.
Regarding known bugs, that probably extends to GNU sed/awk as well. An issue I filed (https://debbugs.gnu.org/cgi/bugreport.cgi?bug=26864) led to the manual update about back-reference bugs.
match($0, /...(...).../, arr) { ...refer to arr[i]... }
But you can't write: /...(...).../ { ...refer to a capture group somehow... }The key to legible awk I’ve found is to not rely on the defaults that awk assumes.
(but it’s quite the mental exercise to decipher it). Feels like regex but less painful
On a tiny file the speed won't matter at all - but on a big file (let's say, you're looking thru log files with hundreds of gigs in size).
The $0=$2 solution has to find the second field and test it for truthiness for every line, while the regex one only has to do that for those matching the regex, and matching that regex will be fast for most lines (skipping spaces and then testing for a fixed string ‘name’ is easy)
Regardless, the regex solution is more robust and maintainable.
Depending on how this get used, I might add code to detect the ‘appVersion’ start and end lines, set/clear a flag there and only match the ‘name’ lines when that flag is set to make it even more robust (who knows what other lines might contain ‘name’ now or in the future?)