2018-11-11 12:38:18 +00:00
|
|
|
---
|
|
|
|
layout: page
|
|
|
|
author: Domenico Votta
|
2018-11-11 16:47:25 +00:00
|
|
|
category-page: advanced
|
|
|
|
category-title: Advanced commands
|
|
|
|
tags: number lines count
|
2018-11-11 12:38:18 +00:00
|
|
|
title: nl
|
|
|
|
---
|
2018-11-14 08:56:17 +00:00
|
|
|
The <code>nl</code> is a command that permits to number the lines.
|
|
|
|
Through some flags you can decide how to filter this command.<br>
|
|
|
|
|
2018-11-11 12:38:18 +00:00
|
|
|
We have a file named <code>example.txt</code> that contains 5 elements, to test this command:
|
2018-11-14 08:56:17 +00:00
|
|
|
|
2018-11-18 17:39:51 +00:00
|
|
|
{% highlight bash %}
|
2018-11-11 12:38:18 +00:00
|
|
|
Car
|
|
|
|
Computer
|
|
|
|
Robot
|
|
|
|
Smartphone
|
|
|
|
Videogame
|
|
|
|
{% endhighlight %}
|
|
|
|
|
|
|
|
The syntax command is:
|
|
|
|
<pre> nl [flags][file] </pre>
|
|
|
|
|
2018-11-11 16:47:25 +00:00
|
|
|
<pre>
|
2018-11-11 12:38:18 +00:00
|
|
|
nl example.txt
|
2018-11-14 08:56:17 +00:00
|
|
|
1 Car
|
|
|
|
2 Computer
|
|
|
|
3 Robot
|
|
|
|
4 Smartphone
|
|
|
|
5 Videogame
|
2018-11-11 16:47:25 +00:00
|
|
|
</pre>
|
2018-11-14 08:56:17 +00:00
|
|
|
|
|
|
|
How you can see the command has numbered the lines.
|
2018-11-11 12:38:18 +00:00
|
|
|
|
|
|
|
<h2>Flags</h2>
|
|
|
|
|
|
|
|
<ul>
|
2018-11-14 08:56:17 +00:00
|
|
|
<li> <b>-b (regex)</b>: Specify the lines to be numered
|
2018-11-11 16:47:25 +00:00
|
|
|
<pre>
|
2018-11-11 12:38:18 +00:00
|
|
|
nl -b p^[cv] example.txt
|
2018-11-14 08:56:17 +00:00
|
|
|
1 Car
|
|
|
|
2 Computer
|
|
|
|
3 Videogame
|
|
|
|
Robot
|
|
|
|
Smartphone
|
2018-11-11 16:47:25 +00:00
|
|
|
</pre>
|
2018-11-18 17:39:51 +00:00
|
|
|
The part after the flag is used to number only the lines that start with c and v.
|
2018-11-14 08:56:17 +00:00
|
|
|
</li>
|
2018-11-11 12:38:18 +00:00
|
|
|
|
2018-11-14 08:56:17 +00:00
|
|
|
<li> -b<b>n</b>: The flag n goes with b, doesn't number any lines
|
2018-11-11 16:47:25 +00:00
|
|
|
<pre>
|
2018-11-11 12:38:18 +00:00
|
|
|
nl -bn example.txt
|
2018-11-14 08:56:17 +00:00
|
|
|
Car
|
|
|
|
Computer
|
|
|
|
Videogame
|
|
|
|
Robot
|
|
|
|
Smartphone
|
2018-11-11 16:47:25 +00:00
|
|
|
</pre>
|
2018-11-14 08:56:17 +00:00
|
|
|
</li>
|
2018-11-11 12:38:18 +00:00
|
|
|
|
2018-11-14 08:56:17 +00:00
|
|
|
<li> <b>-s</b>: Usually nl separes the number of the line from the text with a tab, with -s
|
|
|
|
flag you can choose another separator, in this example we will use "=".
|
2018-11-11 16:47:25 +00:00
|
|
|
<pre>
|
2018-11-11 12:38:18 +00:00
|
|
|
nl -s= example.txt
|
2018-11-14 08:56:17 +00:00
|
|
|
1=Car
|
|
|
|
2=Computer
|
|
|
|
3=Videogame
|
|
|
|
4=Robot
|
|
|
|
5=Smartphone
|
2018-11-11 16:47:25 +00:00
|
|
|
</pre>
|
2018-11-14 08:56:17 +00:00
|
|
|
</li>
|
2018-11-11 12:38:18 +00:00
|
|
|
|
|
|
|
</ul>
|