1
s="/usr/share/fonts/X11/100dpi/ncenI12-ISO8859-1.pcf.gz: New Century Schoolbook:style=Itali"

The best way to extract three parts seperated by ":" is to use awk:

echo $s | awk -F: '{print $1,"\n",$2,"\n",$3}'
/usr/share/fonts/X11/100dpi/ncenI12-ISO8859-1.pcf.gz 
  New Century Schoolbook 
 style=Itali

To achieve same target with sed and group reference in Regex:

echo $s | sed 's/\([^:]\):\([^:]*\):\([^:]*\)/\1\n\2\n\3/'
/usr/share/fonts/X11/100dpi/ncenI12-ISO8859-1.pcf.gz
 New Century Schoolbook
style=Itali

Do with grep:

echo $s | grep -Po   '[^:]*'
/usr/share/fonts/X11/100dpi/ncenI12-ISO8859-1.pcf.gz
 New Century Schoolbook
style=Itali

There is no group reference concept here,how can print the second matched group with \2(the second matched group) in grep?
Such bash command can't be the answer without using Regex's group reference in grep.

echo $s | grep -Po   '[^:]*' | sed -n '2p'

Please write a grep expression with \2--the second matched group to extract New Century Schoolbook.

newview
  • 205
  • You cannot do that with grep (GNU) or otherwise, can you use ripgrep, or prcregrep that allows providing such a matching group? – Inian Nov 09 '22 at 04:42

0 Answers0