0

For example with n being 3, p1ath/p2ath/p3ath would become p1a/p2a/p3?

I don't understand how to:

  1. Truncate a capture group (\1, \2, etc..) using sed.
  2. How to make the capture groups 'dynamic' rather being a set static number of capture groups?

So far I have 's/\(.*\)\//\1\//g'

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286

1 Answers1

2

With GNU sed:

echo "p1ath/p2ath/p3ath" | sed -r 's|([^/]{3})[^/]*|\1|g'

Output:

p1a/p2a/p3a

See: The Stack Overflow Regular Expressions FAQ

Inian
  • 80,270
  • 14
  • 142
  • 161
Cyrus
  • 84,225
  • 14
  • 89
  • 153