, ,
$ echo -e '#!/bin/bash\necho abracadabra' >/tmp/script
$ pattern=bash
$ sed -rn "0,/$pattern/ {s/^(.*)$pattern.*$/\1/p ;t exit; p; :exit }" /tmp/script \
| wc -c
8
, , 1.
NB 1: sed , , , , pattern, 7 ( β #!/bin/), wc -c
$ sed -rn "0,/$pattern/ {s/^(.*)$pattern.*$/\1/p ;t exit; p; :exit }" /tmp/script \
| hexdump -C
00000000 23 21 2f 62 69 6e 2f 0a |
00000008
, , EOF. , .
NB 2: , sed . , , .
NB 3: , pattern . pattern, .
Update.
Ive .
$ grep -bo bash <<< '#!/bin/bash'
7:bash
GNU grep :
-b, --byte-offset
Print the 0-based byte offset within the input file before each line of
output. If -o (--only-matching) is specified, print the offset of the
matching part itself.
Id grep, -F, .
$ grep -F '!@##$@#%%^%&*%^&*(^)((**%%^@#' <<<'!@##$@#%%^%&*%^&*(^)((**%%^@#'
!@#