A few notes:
grep -P
uses libpcre, a compiled library (written in C, not perl) that is perl-compatible
- Your
awk
command won't work if there is a space in the file name
This sed
command will work a little better:
sed '/^.L......... /!d; s///; / -> .*/!d; s///'
This deletes all lines that do not match the first regex, then removes the matching portion of the remaining lines, then does the same for the second regex, leaving you with the text in the middle. Since regexps are greedy, the second regex will turn a -> b -> c
into a
even though you wanted a -> b
, so this isn't a perfect solution either.
Perhaps you can use perl
directly (I assume you're on a system for which grep
was compiled without the libpcre library and therefore can't do -P
but perhaps you still have access to perl
):
perl -ne '/^.L.........\s(.*)\s->\s/ and print "$1\n"'
This time, greediness works in our favor (regexps are evaluated left to right) and you'll correctly extract a file named a -> b
that is symlinked to c
.
If you have GNU sed
(nearly any Linux system), you can mimic that logic:
sed '/^.L.........\s\(.*\)\s->\s.*/!d; s//\1/'
But I haven't technically answered your question, which was about using grep
without -P
. grep
uses basic and extended POSIX regular expressions (BRE and ERE). Even ERE can't do what you're looking for, so the quick answer is that you cannot do this in a single grep
command without -P
. You might be able to do it with multiple grep
commands, but it would be extremely ugly and I'm leaning on saying you actually cannot do it without look-arounds or text replacement, neither of which are available in BRE or ERE.