Skip to content

Instantly share code, notes, and snippets.

@jclosure
Created January 10, 2018 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jclosure/592f2571aacbfd4eea5872fb97fe10a8 to your computer and use it in GitHub Desktop.
Save jclosure/592f2571aacbfd4eea5872fb97fe10a8 to your computer and use it in GitHub Desktop.
Bash read file for pattern matches and load into array
#!/bin/bash
FILE="some_file.txt"
PATTERN="some pattern"
RANGE=`grep -n $PATTERN $FILE | cut -d : -f 1`
while read -r line; do LINES+=("$line"); done <<<"$RANGE"
for line in "${LINES[@]}"
do
echo "-> $line"
done
FIRST=${LINES[0]}
LAST=${LINES[${#LINES[@]}-1]}
echo "first: $FIRST last: $LAST"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment