I would like to extract cc.cc from
/aa/bb/cc.cc
either from
cc.cc
as well.
Solution:
$ echo "/aa/bb/cc.cc" | perl -pe "s|(?:.*/)?([^/]*)|\1|"
cc.cc
$ echo "/aa/cc.cc" | perl -pe "s|(?:.*/)?([^/]*)|\1|"
cc.cc
$ echo "cc.cc" | perl -pe "s|(?:.*/)?([^/]*)|\1|"
cc.cc
Some explanation:
- ?: start non capturing group
- [^/] any character except slash
Probably this won't need you anymore, but here is one (similar) way to do it:
ReplyDelete$ echo -e "/aa/bb/cc.cc\ncc.cc"|grep -oP "^(.*/)?\K[^/]+"
cc.cc
cc.cc
\K is a perl regex speciality, everything before \K was used only to positioning. It bypasses the limitations of the lookbehind assertions.
$ echo appletree|grep -oP "apple\K.*"
tree
Or with other similar aproach:
Delete$ echo -e "/aa/bb/cc.cc\ncc.cc"|grep -oP "/?\K[^/]+$"
cc.cc
cc.cc
Or with AWK:
Delete$ echo -e "/aa/bb/cc.cc\ncc.cc"|awk -F/ '{print $NF}'
cc.cc
cc.cc
Promise this will be the last one.
Delete$ echo -e "/aa/bb/cc.cc\n/aa/cc.cc\ncc.cc"|xargs -n1 -I{} echo var="{}" \&\& echo \${var##*/} |bash
cc.cc
cc.cc
cc.cc