Created
September 12, 2017 08:32
-
-
Save vyskocilm/ca0dbd36cb07b6845ca90b22098ab73d to your computer and use it in GitHub Desktop.
Show nth line with context 5 of each file
This file contains hidden or 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
#!/bin/bash | |
# | |
# Show nth line with context 5 of each file | |
# | |
show_lines () | |
{ | |
local file n ctx min max lineno star | |
file=${1} | |
n=${2} | |
ctx=${3} | |
min=$((n-ctx)) | |
if [[ ${min} -le 0 ]]; then | |
min=1 | |
fi | |
max=$((n+ctx)) | |
printf "${file}: \n" | |
lineno=${min} | |
sed -n -e "${min},${max}p" "${file}" | while read LINE; do | |
if [[ ${lineno} == ${n} ]]; then | |
star="*" | |
else | |
star=" " | |
fi | |
printf "%s%d:\t%s\n" "${star}" ${lineno} "${LINE}" | |
lineno=$((lineno+1)) | |
done | |
printf "\n" | |
} | |
N=${1} | |
shift 1 | |
while [[ -n "${1}" ]]; do | |
show_lines "${1}" ${N} 5 | |
shift 1 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment