I ran across the need to view a specific line of a file so I wrote this up. You can view a line or a range of lines for a file or compare two files. Check the command for the syntax. Drop it in /usr/bin/sln and enjoy.
Show/Compare Line Number Ranges
#!/bin/bash
quiet=0
if [ $# -le 1 ]
then
echo "[1m[4m[31mSyntax[0m: sln <start[-end]> < file > [ <file> ]"
echo "[1m[4mExamples[0m: sln 3 some_file.sh"
echo " sln 3-5 some_file.sh"
echo " sln 3-5 some_file.sh another_file.sh"
exit
fi
for i in $*
do
case "$i" in
"--quiet" | "-q" ) quiet=1;;
esac
done;
lines=$1
start=${lines%-*}
end=${lines#*-}
count=1
if [ $end -gt $start ]
then
count=$(($end - $start + 1))
fi
HEADER1="[32m[1m===[0m[37m Line(s) [1m$start[0m - [1m$end [0mof [1m[4m$2[0m[1m[32m ===[0m"
HEADER2="[32m[1m===[0m[37m Line(s) [1m$start[0m - [1m$end [0mof [1m[4m$3[0m[1m[32m ===[0m"
if [ $quiet -eq 0 ]
then
echo ""
fi
# Default output
echo $HEADER1
more +$start $2 | head -$count
if [ "$3" != "" ]
then
if [ $quiet -eq 0 ]
then
echo ""
fi
echo $HEADER2
more +$start $3 | head -$count
fi
if [ $quiet -eq 0 ]
then
echo ""
fi
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.