Skip to content

Instantly share code, notes, and snippets.

@vyskocilm
Created September 12, 2017 08:32
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 vyskocilm/ca0dbd36cb07b6845ca90b22098ab73d to your computer and use it in GitHub Desktop.
Save vyskocilm/ca0dbd36cb07b6845ca90b22098ab73d to your computer and use it in GitHub Desktop.
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