Last active
April 12, 2016 01:44
-
-
Save martinkersner/32e909a518d6983d37b524a4d3984dd4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Martin Kersner, m.kersner@gmail.com | |
# 2016/04/08 | |
# faster_grep $1 $2 | |
# $1 pattern file | |
# $2 input file | |
PATTERN_FILE="$1" | |
INPUT_FILE="$2" | |
TMP_FILE=$(mktemp /tmp/faster_grep.XXXXXX) | |
exec 3>"$TMP_FILE" | |
rm "$TMP_FILE" | |
cat "$INPUT_FILE" > "$TMP_FILE" | |
NUM_LINE_INPUT=0 | |
while IFS='' read -r LINE || [[ -n "$LINE" ]]; do | |
GREP_OUT=`grep -n "$LINE" "$TMP_FILE"` | |
if [ "$GREP_OUT" ]; then | |
NUM_LINE_PAT=`grep -o "^[0-9]*" <<< "$GREP_OUT"` | |
MATCH=`grep -o "/.*$" <<< "$GREP_OUT"` | |
echo "$MATCH" | |
NUM_LINE_INPUT=$((NUM_LINE_PAT+NUM_LINE_INPUT)) | |
tail -n +${NUM_LINE_INPUT} ${INPUT_FILE} > ${TMP_FILE} | |
fi | |
done < "$PATTERN_FILE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment