man grep
says
grep searches for PATTERNS in each FILE. PATTERNS is one or patterns separated by newline characters, and grep prints each line that matches a pattern.
-o, --only-matching
Print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
-P, --perl-regexp
Interpret PATTERNS as Perl-compatible regular expressions
(PCREs). This option is experimental when combined with the -z
(--null-data) option, and grep -P may warn of unimplemented features.
On ls
i had
'2180_PP AAA Radius Statistic-42005_04May2020_0900-04May2020_1000.csv'
'2180_SW Interface Flow(3GPP AAA)-53448_14May2020_0000-14May2020_0100.csv'
After running the below code, I got
ls | grep -oP '(?<=_).*(?=\-\d\d\d)'
PP AAA Radius Statistic
SW Interface Flow(3GPP AAA)
Explanation of REGEX
(?<= - Stands for a positive look-behind and will not include the words before it
. - Matches any characters except line break
(?= - Stands for positive look-ahead. Matches a group
after the main result without including it in the result.
\- - Matched character -
\d - Matched digit
Source of REGEX explanation is REGEXR
Why possibly you got a different result?
Was that there was another matching -
in the input (-14May). So I used \-\d\d\d
to counteract that.