Skip to main content
2 of 2
added 206 characters in body
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k

With zsh (the only shell that can store arbitrary byte sequences in its variables), assuming data and delimiter are regular (or at least mmap()able) files, you can do:

zmodload zsh/mapfile set +o multibyte # necessary so sequences of bytes that # happen to form valid characters may be # broken in the middle if necessary. firstpart=${mapfile[data]%%$mapfile[delimiter]*} 

Or:

zmodload zsh/mapfile set +o multibyte # necessary so sequences of bytes that # happen to form valid characters may be # broken in the middle if necessary. delimiter=$mapfile[delimiter] parts=( ${(ps[$delimiter])mapfile[data]} ) firstpart=$parts[1] 

(don't expect it to be very efficient nor to scale well to files larger than a few hundred megabytes).

To print that part verbatim, use:

print -rn -- $firstpart 

Or

printf %s $firstpart 
Stéphane Chazelas
  • 586.2k
  • 96
  • 1.1k
  • 1.7k