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
  • 25,295
  • 38
  • 135
  • 245

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
  • 71,145
  • 9
  • 121
  • 139
Cyrus
  • 77,979
  • 13
  • 71
  • 125