{"id":2986,"date":"2018-03-07T17:45:37","date_gmt":"2018-03-07T14:45:37","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-nl-command\/"},"modified":"2018-03-07T17:45:37","modified_gmt":"2018-03-07T14:45:37","slug":"linux-nl-command-tutorial-for-beginners-7-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-nl-command-tutorial-for-beginners-7-examples\/","title":{"rendered":"Linux nl Command Tutorial for Beginners (7 Examples)"},"content":{"rendered":"<p>Linux offers a lot of text formatting command line tools, with each of them having a different purpose. One such utility is <strong>nl<\/strong>, which lets you number lines in files. In this tutorial, we will discuss the basics of this command using some easy to understand examples.<\/p>\n<p>But before we do that, it&#8217;s worth mentioning that all examples used in this tutorial have been tested on an Ubuntu 16.04 LTS system.<\/p>\n<h2 id=\"linux-nl-command\">Linux nl command<\/h2>\n<p>As already mentioned in the beginning, the nl command numbers lines in files. Following is its syntax:<\/p>\n<p class=\"command\">nl [OPTION]&#8230; [FILE]&#8230;<\/p>\n<p>And here&#8217;s how the man page explains it:<\/p>\n<pre>Write each FILE to standard output, with line numbers added. With no<br\/>FILE, or when FILE is -, read standard input.<\/pre>\n<p>Following are some Q&amp;A styled examples that should give you a better idea on how nl works.<\/p>\n<h2 id=\"q-how-to-use-nl-command\">Q1. How to use nl command?<\/h2>\n<p>Basic usage of nl is very easy &#8211; all you have to do is to pass as argument the name of the file whose lines you want to number.<\/p>\n<p class=\"command\">nl [filename]<\/p>\n<p>Here&#8217;s an example:<\/p>\n<p><a class=\"fancybox\" id=\"img-nl-basic-usage\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/nl-basic-usage.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/linux-nl-command-tutorial-for-beginners-7-examples.png\" alt=\"How to use nl command\" width=\"334\" height=\"200\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-number-emptynbsplines\">Q2. How to number empty\u00a0lines?<\/h2>\n<p>By default, the nl command doesn&#8217;t number empty lines:<\/p>\n<p><a class=\"fancybox\" id=\"img-nl-numbers-empty-lines\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/nl-numbers-empty-lines.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/linux-nl-command-tutorial-for-beginners-7-examples-1.png\" alt=\"How to number empty lines\" width=\"333\" height=\"234\" title=\"\"><\/a><\/p>\n<p>However, if you want, you can change this behavior by passing value &#8216;a&#8217; to the -b command line option.<\/p>\n<p class=\"command\">nl -b a [filename]<\/p>\n<p><a class=\"fancybox\" id=\"img-nl-b-a\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/nl-b-a.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/linux-nl-command-tutorial-for-beginners-7-examples-2.png\" alt=\"How to number empty lines - result\" width=\"382\" height=\"236\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-customize-the-number-increment-value\">Q3. How to customize the number increment value?<\/h2>\n<p>By default, the number increment value is 1. However, you can customize this using the -i command line option.<\/p>\n<p class=\"command\">nl -i [new-inc-val] [filename]<\/p>\n<p>For example:<\/p>\n<p><a class=\"fancybox\" id=\"img-nl-i-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/nl-i-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/linux-nl-command-tutorial-for-beginners-7-examples-3.png\" alt=\"How to customize the number increment value\" width=\"378\" height=\"232\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-make-nl-consider-multiple-empty-lines-as-one\">Q4. How to make nl consider multiple empty lines as one?<\/h2>\n<p>For this, use the -l command line option. Here&#8217;s how the man page explains it:<\/p>\n<pre> Consider NUMBER (default 1) consecutive empty lines to be one<br\/>logical line for numbering, and only number the last one. Where<br\/>fewer than NUMBER consecutive empty lines occur, do not number<br\/>them. An empty line is one that contains no characters, not even<br\/>spaces or tabs.<\/pre>\n<p>For example, let&#8217;s take the following file:<\/p>\n<p><a class=\"fancybox\" id=\"img-nl-basic-output\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/nl-basic-output.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/linux-nl-command-tutorial-for-beginners-7-examples-4.png\" alt=\"How to make nl consider multiple empty lines as one\" width=\"334\" height=\"522\" title=\"\"><\/a><\/p>\n<p>And suppose, we want nl to consider 8 consecutive empty lines to be one logical line for numbering. Then here&#8217;s the command we&#8217;d run:<\/p>\n<p class=\"command\">nl -b a -l 8\u00a0[filename]<\/p>\n<p><a class=\"fancybox\" id=\"img-nl-l-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/nl-l-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/linux-nl-command-tutorial-for-beginners-7-examples-5.png\" alt=\"we want nl to consider 8 consecutive empty lines\" width=\"424\" height=\"509\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-use-different-numbering-formats\">Q5. How to use different numbering formats?<\/h2>\n<p>You can use the -n command line option to specify numbering formats. Available option include left justified, right justified, and right justified with leading zeros. You can access these using the ln, rn, and rz values.<\/p>\n<p>Here&#8217;s an examples showing all these formats:<\/p>\n<p><a class=\"fancybox\" id=\"img-nl-n-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/nl-n-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/linux-nl-command-tutorial-for-beginners-7-examples-6.png\" alt=\"How to use different numbering formats\" width=\"360\" height=\"550\" title=\"\"><\/a><\/p>\n<h2 id=\"qhow-to-use-a-customized-numbering-separator\">Q6.How to use a customized numbering separator?<\/h2>\n<p>By default, the character that separates number from the line is a TAB. However, if you want, you can customize this using the -s command line option, which requires the new separator as input.<\/p>\n<p>For example, if we want to use a double colon (::) as the new separator, then here&#8217;s how we can do that:<\/p>\n<p class=\"command\">nl -s :: [filename]<\/p>\n<p><a class=\"fancybox\" id=\"img-nl-s-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/nl-s-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/linux-nl-command-tutorial-for-beginners-7-examples-7.png\" alt=\"How to use a customized numbering separator\" width=\"390\" height=\"199\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-make-nl-use-a-different-starting-line-number\">Q7. How to make nl use a different starting line number?<\/h2>\n<p>By default, numbering starts with 1. However, this you can change using the -v command line option. Here&#8217;s how:<\/p>\n<p class=\"command\">nl -v [new-start-number] [filename]<\/p>\n<p><a class=\"fancybox\" id=\"img-nl-v-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/nl-v-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/linux-nl-command-tutorial-for-beginners-7-examples-8.png\" alt=\"How to make nl use a different starting line number\" width=\"382\" height=\"196\" title=\"\"><\/a><\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>The nl command is easy to understand and use. Here, in this tutorial, we&#8217;ve covered majority of the command line options it offers. Once you are done practicing these, head to the tool&#8217;s <a href=\"https:\/\/linux.die.net\/man\/1\/nl\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a>\u00a0to learn more.<\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Linux offers a lot of text formatting command line tools, with each of them having a different purpose. One such utility is nl, which lets you number lines in files. In this tutorial, we will discuss the basics of this command using some easy to understand examples. But before we do that, it&#8217;s worth mentioning &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-2986","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=2986"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2986\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=2986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=2986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=2986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}