Intro To 'nl' Command In Linux
2023-06-14 - By Robert Elder
I use the 'nl' command to number the lines in a file:
nl main.c
1 #include <stdio.h>
2 int main() {
3 printf("Hello, World!");
4 return 0;
5 }
By default, the 'nl' command doesn't count empty lines:
nl main.py
1 import sys
2 class HelloWorld(object):
3 def __init__(self, s):
4 self.msg = s
5 def do_print(self):
6 sys.stdout.write(self.msg)
7 HelloWorld("Hello World!\n").do_print()
Numbering Empty Lines With 'nl' Command
You can add the '-b' flag with the letter 'a' to cause all of the lines to be numbered:
nl -b a main.py
1 import sys
2
3 class HelloWorld(object):
4 def __init__(self, s):
5 self.msg = s
6
7 def do_print(self):
8 sys.stdout.write(self.msg)
9
10 HelloWorld("Hello World!\n").do_print()
An Unusual Line Numbering Tool
The 'nl' command supports a surprisingly complex and unusual model for numbering lines in a file. Let's review a couple examples of more complicated uses of the 'nl' command on the following file, 'gnu_linux.txt':
\:\:\:
An Ode To GNU
\:\:
I'd just like to interject for a moment. What you're referring
to as Linux, is in fact, GNU/Linux, or as I've recently taken
to calling it, GNU plus Linux. Linux is not an operating
system unto itself, but rather another free component of a
fully functioning GNU system made useful by the GNU corelibs,
shell utilities and vital system components comprising a full
OS as defined by POSIX.
\:
Copyright 2023
Advanced Formatting Example
First, let's review the output of this command:
cat gnu_linux.txt | nl -b a -h n -f n -s ' ---> ' -n rz -w 12 -v 9373
An Ode To GNU
000000009373 ---> I'd just like to interject for a moment. What you're referring
000000009374 ---> to as Linux, is in fact, GNU/Linux, or as I've recently taken
000000009375 ---> to calling it, GNU plus Linux. Linux is not an operating
000000009376 ---> system unto itself, but rather another free component of a
000000009377 ---> fully functioning GNU system made useful by the GNU corelibs,
000000009378 ---> shell utilities and vital system components comprising a full
000000009379 ---> OS as defined by POSIX.
Copyright 2023
In the above command, the '-b' flag indicates that the body of the file should have all lines numbered, and the '-h' and '-f' flags indicate that the header and footer should have no lines numbered. The header, body, and footer are delimited using the special separators '\:' repeated several times in the 'gnu_linux.txt' file above.
The '-s' flag provides a separator to add after the line number. The '-n' flag with the value 'rz' causes right justified line numbers with leading zeros, and the '-w' flag indicates how many characters the line numbers should be. The '-v' flag provides an explicit starting line number.
Highlighted Lines Matching Pattern
Here's another example use of the 'nl' command that will highlight any line that matches the regex pattern 'GNU', using the '-b' flag. The letter 'p' is not part of the regex pattern. It's purpose is to act as a switch to the '-b' flag:
cat gnu_linux.txt | nl -b pGNU -s ' ==> ' -n rz
An Ode To GNU
I'd just like to interject for a moment. What you're referring
000001 ==> to as Linux, is in fact, GNU/Linux, or as I've recently taken
000002 ==> to calling it, GNU plus Linux. Linux is not an operating
system unto itself, but rather another free component of a
000003 ==> fully functioning GNU system made useful by the GNU corelibs,
shell utilities and vital system components comprising a full
OS as defined by POSIX.
Copyright 2023
And that's why the 'nl' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|