News for hackers. Stuff that matters.
Re:Master the command line, in one page by Calzifer on Thursday November 24, 2022 @09:52AM (#33731914)
Since the list mentions grep -o (--only-matching) and regex. Here is my preferred trick to extract a specific part of a line and circumvent the lack of capture groups in grep.

Imagine you have a line containing

  prefix 1234 suffix
and want to only grep the number but need to match prefix and/or suffix to not grep another unwanted number.

Can be solved with

  grep --only-matching --perl-regexp 'prefix \K\d+(?= suffix)'
The magic is '\K' which kind of resets the 'matched buffer'. [1] So anything before \K is matched but not outputted with -o (or without -o anything before \K would not be highlighted).

And for the suffix: (?=) is a positive lookahead [2] which checks if, in this case, ' suffix' is found after the number but also does not include it in the 'matched buffer'.

So the output for

  echo "prefix 1234 suffix" | grep --only-matching --perl-regexp 'prefix \K\d+(?= suffix)'
is only

  1234
PS: instead of \K the prefix could also be wrapped in a positive lookbehind (?<=prefix )

[1] https://perldoc.perl.org/perlrebackslash#%5CK

[2] https://www.rexegg.com/regex-lookarounds.html

Re:Master the command line, in one page by tatref on Thursday November 24, 2022 @10:20AM (#33732276)
Isn't simpler to use perl?

Perl -pe 's/.(capture this)./$1/'

That way, you can capture multiple groups, and change the format in the output. You can also do simple calculations if you capture numbers

Re:Master the command line, in one page by tryauuum on Thursday November 24, 2022 @10:54AM (#33732733)
for me no, anything perl-related is not simple

I also tried putting this question in GPT3:

> Isn't simpler to use perl?

> It is simpler to use Perl, but it is not as efficient as using C or C++.

Re:Master the command line, in one page by jraph on Thursday November 24, 2022 @11:02AM (#33732829)
Nice one, I'll try to counter this garbage GPT3 answer:

- I don't think the regex engine in Perl is implemented in Perl. It's probably implemented in C/C++, like grep. libpcre is in C anyway.

- Even if grep is/were more efficient, You might have consumed more time and energy thinking, typing and running "grep --only-matching --perl-regexp 'prefix \K\d+(?= suffix)'" than the suggested perl solution

- I might have consumed even more energy typing this reply. My computer is there, waiting for me typing, doing not much.

Re:Master the command line, in one page by tryauuum on Thursday November 24, 2022 @11:04AM (#33732862)
shorter version of the grep command is

    grep -oP 'prefix \K\d+(?= suffix)'
but yeah, I know it's easier to me because I did read man page at some point (while carefully avoiding perl manuals)

actually even that is not true, perldoc on regular expressions really helps even grep users

Re:Master the command line, in one page by jraph on Thursday November 24, 2022 @11:21AM (#33733070)
In all honesty, I'm more likely to remember this form than the Perl one, and once it's in the shell history, any will do anyway :-)
Re:Master the command line, in one page by melagonster on Thursday November 24, 2022 @08:16PM (#33738042)
grep have perl regex mode.