Reputation: 3256
I'm trying to write a regex to get the last word of each line of a file that start with "A" and end with "/"
grep '^A.*/$' list_svn.txt
This give the whole line:A Java/SYSTEM_A/Sources/branch/developerA/src/org/hl7/v3/
but what I want is just "v3" string.
I tried awk but without success too.
grep '^A.*/$' list_svn.txt | awk -F '/' '{print $0, $(NF-1)}'
>> Java/SYSTEM_A/Sources/branch/developerA/src/org/hl7/v3/ v3
Upvotes: 1
Views: 2116
Reputation: 52102
With GNU grep and look-arounds:
$ grep -Po '^A.*/\K[^/]*(?=/$)' <<< 'A Java/SYSTEM_A/Sources/branch/developerA/src/org/hl7/v3/'
v3
-P
enables Perl regular expressions (required for look-ahead and look-behind), and -o
returns only the match.
The \K
acts as a variable length look-behind: "match everything up to here but don't include it in the match". The actual match is a sequence of characters other than /
, and the (?=/$)
bit stands for "followed by a /
and the end of the line" (but also not included in match).
Upvotes: 1
Reputation: 784898
You can avoid grep
altogether as using awk you can do:
awk -F/ '/^A.*\/$/ {print $(NF-1)}' list_svn.txt
v3
Upvotes: 2