Skip to main content
added 399 characters in body
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

With perl:

perl -pe 's{^g(?:\d+/)*?(\d+)\K(?:/\1)+(?!\d)}{}' < your-file

It is strict in that it will remove the first sequence of repeating numbers only in the leading g/number/number[/number...]/number part and only if that part is not followed by a digit.

With sed, you could do the same with:

sed '
  \|^\(g\([[:digit:]]\{1,\}/\)*[[:digit:]]\{1,\}\).*| {
    h; # save a copy of original line
    s||\1:|; # remove all but the leading g/x/y/z
    s|\([g/]\)\([[:digit:]]\{1,\}\)\(/\2\)\{1,\}\([^[:digit:]]\)|\1\2\4|
    G; # append saved copy
    s|:\ng\([[:digit:]]\{1,\}/\)*[[:digit:]]\{1,\}||; # remove excess
  }' < your-file

With perl:

perl -pe 's{^g(?:\d+/)*?(\d+)\K(?:/\1)+(?!\d)}{}' < your-file

It is strict in that it will remove the first sequence of repeating numbers only in the leading g/number/number[/number...]/number part and only if that part is not followed by a digit.

With perl:

perl -pe 's{^g(?:\d+/)*?(\d+)\K(?:/\1)+(?!\d)}{}' < your-file

It is strict in that it will remove the first sequence of repeating numbers only in the leading g/number/number[/number...]/number part and only if that part is not followed by a digit.

With sed, you could do the same with:

sed '
  \|^\(g\([[:digit:]]\{1,\}/\)*[[:digit:]]\{1,\}\).*| {
    h; # save a copy of original line
    s||\1:|; # remove all but the leading g/x/y/z
    s|\([g/]\)\([[:digit:]]\{1,\}\)\(/\2\)\{1,\}\([^[:digit:]]\)|\1\2\4|
    G; # append saved copy
    s|:\ng\([[:digit:]]\{1,\}/\)*[[:digit:]]\{1,\}||; # remove excess
  }' < your-file
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

With perl:

perl -pe 's{^g(?:\d+/)*?(\d+)\K(?:/\1)+(?!\d)}{}' < your-file

It is strict in that it will remove the first sequence of repeating numbers only in the leading g/number/number[/number...]/number part and only if that part is not followed by a digit.