Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides

FAQ
It is currently Tue Jun 06, 2023 7:54 am


Tips & Tricks, Questions regarding shell scripts, awk, perl, sed and much more.

Author Message
mandrei99
Post  Post subject: How to add line numbers in unix shell output  |  Posted: Fri Oct 31, 2014 5:31 am

Joined: Tue Aug 04, 2009 9:16 am
Posts: 250

Offline
 

How to add line numbers in unix shell output

How to add line numbers in unix shell output

To display line numbers easily on a shell output, pipe it to "nl" command - line number filter.

Quote:
NL(1) FreeBSD General Commands Manual NL(1)

NAME
nl -- line numbering filter

SYNOPSIS
nl [-p] [-b type] [-d delim] [-f type] [-h type] [-i incr] [-l num]
[-n format] [-s sep] [-v startnum] [-w width] [file]

DESCRIPTION
The nl utility reads lines from the named file or the standard input if
the file argument is omitted, applies a configurable line numbering fil-
ter operation and writes the result to the standard output.

The nl utility treats the text it reads in terms of logical pages.
Unless specified otherwise, line numbering is reset at the start of each
logical page. A logical page consists of a header, a body and a footer
section; empty sections are valid. Different line numbering options are
independently available for header, body and footer sections.

The starts of logical page sections are signalled by input lines contain-
ing nothing but one of the following sequences of delimiter characters:

Line Start of
\:\:\: header
\:\: body
\: footer

If the input does not contain any logical page section signalling direc-
tives, the text being read is assumed to consist of a single logical page
body.


This can be done with awk also, but I find this easier:
Code:
# ls
file1   file10  file2   file3   file4   file5   file6   file7   file8   file9
# ls | nl
     1  file1
     2  file10
     3  file2
     4  file3
     5  file4
     6  file5
     7  file6
     8  file7
     9  file8
    10  file9





Top
Display posts from previous:  Sort by  
E-mail friendPrint view

Topics related to - "How to add line numbers in unix shell output"
 Topics   Author   Replies   Views   Last post 
There are no new unread posts for this topic. Unix shell - using TR to replace new lines with spaces

debuser

0

8832

Thu Feb 25, 2010 8:08 am

debuser View the latest post

There are no new unread posts for this topic. How to echo newline in Linux/Unix shell

mandrei99

0

79808

Tue Jun 25, 2013 5:58 am

mandrei99 View the latest post

There are no new unread posts for this topic. Echo new line in linux - supress new line or echo multiple new lines

mandrei99

0

6020

Wed Jan 14, 2015 5:26 pm

mandrei99 View the latest post

There are no new unread posts for this topic. SED uncomment line - How to uncomment a line or all lines with SED

mandrei99

0

7766

Wed Jan 14, 2015 5:08 pm

mandrei99 View the latest post

There are no new unread posts for this topic. Shell scripting

Diana

0

1784

Thu Aug 24, 2017 3:10 am

Diana View the latest post

There are no new unread posts for this topic. How to replace new line in perl script

mandrei99

1

4929

Wed Jun 26, 2013 8:18 am

admin View the latest post

There are no new unread posts for this topic. How to convert a text into a list of words, one per line

LaR3

0

8966

Wed Aug 05, 2009 7:02 am

LaR3 View the latest post

There are no new unread posts for this topic. How to replace \w SED multiple matches on the same line without piping

mandrei99

0

3448

Tue Oct 08, 2013 6:29 am

mandrei99 View the latest post

There are no new unread posts for this topic. Removing new line / EoL characters in a PERL script

mandrei99

0

4405

Mon Nov 04, 2013 6:47 am

mandrei99 View the latest post

There are no new unread posts for this topic. BASH shell script to mointor a directory and move file without overwriting destination

admin

0

2525

Wed Nov 15, 2017 7:48 am

admin View the latest post

 

Who is online
Users browsing this forum: No registered users and 0 guests
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum
Jump to:  
News News Site map Site map SitemapIndex SitemapIndex RSS Feed RSS Feed Channel list Channel list


Delete all board cookies | The team | All times are UTC - 5 hours [ DST ]



phpBB SEO