Display Files with Line Numbers - knl

This is a simple Korn shell version of the Unix nl command. It displays line-numbered output.

					#!/bin/ksh
					#
					#     knl - Korn Shell line-numbering filter
					#
					# Initialize line number counter
					integer LNUM=1
					# Check usage
					if (($# == 0))
					then
					print "Usage: $0 file . . ."
					exit 1
					fi
					# Process each file
					for FILE
					do
					# Make sure file exists
					if [[ ! -f $FILE ]]
					then
					print "$FILE: non-existent or not readable"
					exit 1
					else
					# Open file for reading
					exec 0<$FILE
					# Read each line, print out with line number
					while read -r LINE
					do
					print "$LNUM: $LINE"
					((LNUM+=1))
					done
					fi
					# Reset line number counter
					LNUM=1
					done
				

Get Korn Shell: Unix and Linux Programming Manual, Third Edition, The now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.