Skip to content

Instantly share code, notes, and snippets.

@anon-hui
Last active July 10, 2025 06:18
Show Gist options
  • Select an option

  • Save anon-hui/4a1a4a5952d12d7220d82a534d68176f to your computer and use it in GitHub Desktop.

Select an option

Save anon-hui/4a1a4a5952d12d7220d82a534d68176f to your computer and use it in GitHub Desktop.
find the position in 1st file where its binary match the head of 2nd file, and begin cmp at that position; Usage: $ . index_cmp.sh file1 file2 [offset_of_file2_head(default_0) [length_of_file2_head_to_search(default_1000) [offset_in_file1_to_start_search(default_0)]]]
#set -- "$@"
#. /dev/null
if : || [[ "$1" != "set "* ]] ; then
#echo '$ '
#echo "$1" ; eval "$1"
echo '$ ' >&2
#echo "$1" >&2 ; eval "$1"
#echo "$1" >&2 ; eval "shift ; . /dev/null ; $1"
# * sourcing make it not return $@, but no this effect when shift
#echo "$1" >&2 ; eval 'shift ; (( $# > 0 )) && set -- "$@" ; '"$1"
echo "$1" >&2 ; if (( $# > 1 )) ; then eval 'shift ; '"$1" ; set -- "$@"
else eval 'shift ; '"$1" ; fi
#. /dev/null
else
echo '$ ' >&2
echo "$1" >&2 ; eval "$1"
fi
# $ { head -1000c /home/sysadmin/tmp/f139.3 ; cat /home/sysadmin/tmp/f139.2 ; } | perl -le 'read(STDIN, $a, 1000); print index(join("",<STDIN>),$a)+1'
#{ head -1000c "$2" ; cat "$1" ; } | perl -le 'read(STDIN, $a, 1000); print index(join("",<STDIN>),$a)+1'
#set -- "$1" "$2" "`{ head -1000c "$2" ; cat "$1" ; } | perl -le 'read(STDIN, $a, 1000); print index(join("",<STDIN>),$a)+1'`"
#set -- "$1" "$2" "`perl -le 'open(F, "<", $ARGV[0]); read(F, $a, 1000); close(F); print index(join("",<STDIN>),$a)+1' "$2" < "$1"`"
#set -- "$1" "$2" `perl -le 'open(F, "<", $ARGV[0]); read(F, $a, 1000); close(F); read(STDIN,$b,2000000); for($o=0;;){ read(STDIN,$c,2000000); $i=index($b.substr($c,0,1000-1),$a); if($i>=0){print $o+$i+1;last;} if($c eq""){last;} $o+=length$b; $b=$c; }' "$2" < "$1"`
#. /dev/null
#while (( $# > 2 )) ; do
# (( "$3" > 0 )) && {
# #tail -c+"$3" "$1" | cmp "$2"
# #tail -c+"$3" "$1" | wc -c
# #. eval_1.sh "tail -c+$3 $1 | cmp $2"
# #. eval_1.sh "tail -c+$3 $1 | wc -c"
# . eval_1.sh "tail -c+$3 $1 | cmp $2" 2>&1| tee /dev/stderr | grep --color=always -Fx 'cmp: EOF on -'
# . eval_1.sh "tail -c+$3 $1 | wc -c" 2>&1| tee /dev/stderr | grep --color=always -Fx 0
# }
#set -- "$1" "$2" "${@:4}"; . /dev/null ; done
#set -- "$1" "$2" "$3" `perl -le 'open(F, "<", $ARGV[0]); seek(F,$ARGV[1],0); read(F,$a,1000); close(F); read(STDIN,$b,2000000); for($o=0;;){ read(STDIN,$c,2000000); $i=index($b.substr($c,0,1000-1),$a); if($i>=0){print $o+$i+1;last;} if($c eq""){last;} $o+=length$b; $b=$c; }' "$2" "$3" < "$1"`
#set -- "$1" "$2" "$3" `perl -le 'open(F, "<", $ARGV[0]); seek(F,$ARGV[1],0); BEGIN{scalar@ARGV<3 and $ARGV[2]=1000} read(F,$a,$ARGV[2]); close(F); read(STDIN,$b,2000000); for($o=0;;){ read(STDIN,$c,2000000); $i=index($b.substr($c,0,$ARGV[2]-1),$a); if($i>=0){print $o+$i+1;last;} if($c eq""){last;} $o+=length$b; $b=$c; }' "$2" "$3" "${@:4}" < "$1"`
#set -- "$1" "$2" "$3" `perl -le 'open(F, "<", $ARGV[0]); seek(F,$ARGV[1]+0,0); BEGIN{scalar@ARGV<3 and $ARGV[2]=1000} read(F,$a,$ARGV[2]); close(F); read(STDIN,$b,2000000); for($o=0;;){ read(STDIN,$c,2000000); $i=index($b.substr($c,0,$ARGV[2]-1),$a); if($i>=0){print $o+$i+1;last;} if($c eq""){last;} $o+=length$b; $b=$c; }' "$2" "$3" "${@:4}" < "$1"`
set -- "$1" "$2" "$3" `perl -le 'open(F, "<", $ARGV[0]); seek(F,$ARGV[1]+0,0); BEGIN{scalar@ARGV<3 and $ARGV[2]=1000; scalar@ARGV<4 and $ARGV[3]=0; } read(F,$a,$ARGV[2]); close(F); seek(STDIN,$ARGV[3]+0,0); read(STDIN,$b,2000000); for($o=0;;){ read(STDIN,$c,2000000); $i=index($b.substr($c,0,$ARGV[2]-1),$a); if($i>=0){print $ARGV[3]+$o+$i+1;last;} if($c eq""){last;} $o+=length$b; $b=$c; }' "$2" "$3" "${@:4}" < "$1"`
. /dev/null
# * escape file name
set -- "$1" "$2" "'\''" "${@:3}"
set -- \'"${1//\'/$3}"\' \'"${2//\'/$3}"\' "${@:4}"
. /dev/null
while (( $# >= 4 )) ; do
(( "$4" > 0 )) && {
#tail -c+"$4" "$1" | cmp "$2"
#tail -c+"$4" "$1" | wc -c
#. eval_1.sh "tail -c+$4 $1 | cmp $2"
#. eval_1.sh "tail -c+$4 $1 | wc -c"
#if [[ "$3" == "" ]] ; then
if [[ "$3" == "" ]] || (( "$3" == 0 )) ; then
#. eval_1.sh "tail -c +$4 $1 | cmp $2" 2>&1| tee /dev/stderr | grep --color=always -Fx 'cmp: EOF on -'
#. eval_1.sh "tail -c +$4 $1 | wc -c" 2>&1| tee /dev/stderr | grep --color=always -Fx 0
#. eval_1.sh "tail -c +$4 -- $1 | cmp -- $2" 2>&1| tee /dev/stderr | grep --color=always -Fx 'cmp: EOF on -'
#. eval_1.sh "cmp -i "$(($4-1))":0 -- $1 $2" 2>&1| tee /dev/stderr | eval grep --color=always -Fx "'cmp: EOF on '$1"
. eval_1.sh "cmp -i $(($4-1)):0 -- $1 $2" 2>&1| tee /dev/stderr | eval grep --color=always -Fx "'cmp: EOF on '$1"
. eval_1.sh "tail -c +$4 -- $1 | wc -c" 2>&1| tee /dev/stderr | grep --color=always -Fx 0
else
#. eval_1.sh "{ head -c $3 $2 ; tail -c +$4 $1 ; } | cmp $2" 2>&1| tee /dev/stderr | grep --color=always -Fx 'cmp: EOF on -'
##. eval_1.sh "{ head -c $3 $2 ; tail -c +$4 $1 ; } | wc -c" 2>&1| tee /dev/stderr | grep --color=always -Fx 0
#. eval_1.sh "tail -c +$4 $1 | wc -c" 2>&1| tee /dev/stderr | grep --color=always -Fx 0
#. eval_1.sh "{ head -c $3 -- $2 ; tail -c +$4 -- $1 ; } | cmp -- $2" 2>&1| tee /dev/stderr | grep --color=always -Fx 'cmp: EOF on -'
#. eval_1.sh "cmp -i "$(($4-1))":$3 -- $1 $2" 2>&1| tee /dev/stderr | eval grep --color=always -Fx "'cmp: EOF on '$1"
. eval_1.sh "cmp -i $(($4-1)):$3 -- $1 $2" 2>&1| tee /dev/stderr | eval grep --color=always -Fx "'cmp: EOF on '$1"
#. eval_1.sh "{ head -c $3 -- $2 ; tail -c +$4 -- $1 ; } | wc -c" 2>&1| tee /dev/stderr | grep --color=always -Fx 0
. eval_1.sh "tail -c +$4 -- $1 | wc -c" 2>&1| tee /dev/stderr | grep --color=always -Fx 0
fi
}
set -- "$1" "$2" "$3" "${@:5}" ; . /dev/null ; done
@anon-hui
Copy link
Author

Usage: $ . index_cmp.sh file1 file2 [offset_of_file2_head(default_0) [length_of_file2_head_to_search(default_1000) [offset_in_file1_to_start_search(default_0)]]]

Example: $ . index_cmp.sh file1 file2

Example: $ . index_cmp.sh file1 file2 0 188

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment