Skip to content

Instantly share code, notes, and snippets.

@vyskocilm
Created Sep 12, 2017
Embed
What would you like to do?
Show nth line with context 5 of each file
#!/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