Skip to main content
1 of 2
guest_7
  • 5.8k
  • 1
  • 8
  • 13
## input variables
n=3
s='hku\test\user\software\microsoft\windows\currentversion\runonce\delete cached update binary'

sed Change the n-th backslash to a newline, a character known to not be present, we then strip away everything till the newline.

printf "$s" |
sed -e '
  s/\\/\n/'"$n"'
  s/.*\n//
' -
software\microsoft\windows\currentversion\runonce\delete cached update binary

linux command line Convert to one-field per line, chop off the first n fields, and then join them back.

printf "$s" | tr '\\' '\n' | tail -n+"$((n+1))" | paste -sd '\\' -

bash builtins

set -f;IFS=\\;
declare -a a=( $s )
printf '%s\n' "${a[*]:$n}"

awk

printf "$s" |
awk -F '\' -v n="$n" '
NF>n {
  for (i=p=1; i<=n; i++) 
    p += 1+length($i)
  $0 = substr($0,p)
}1' -

printf "$s" |
perl -pals -F'/\\/,$_,$n+1' -e '
  $_=$F[-1];
' -- -n="$n"  -

python3 -c 'import sys
p,(s,n) = -1,sys.argv[1:]
for i in range(1+int(n)):
  p = 1+s.find("\\",p)
print(s[p:])
' "$s" "$n"
guest_7
  • 5.8k
  • 1
  • 8
  • 13