Lệnh nl trong linux

Lệnh nl trong linux

Show

Trên hệ điều hành giống Unix, lệnh nl đánh số các dòng trong tệp.

Trang này mô tả phiên bản GNU \/Linux của nl.

Cú pháp

nl [OPTION]... [FILE]...

Tùy chọn

-NS,
–body-numbering = STYLE

sử dụng STYLE để đánh số các dòng nội dung

-NS,
–section-delimiter = CC

sử dụng CC để tách các trang logic

-NS,
–footer-numbering = STYLE

sử dụng STYLE để đánh số các dòng chân trang

-NS,
–header-numbering = STYLE

sử dụng STYLE để đánh số các dòng tiêu đề

-tôi,
–line-increment = NUMBER

tăng số dòng ở mỗi dòng

-l,
–join-blank-lines = NUMBER

nhóm NUMBER dòng trống được tính là một

-n,
–number-format = FORMAT

chèn số dòng theo FORMAT

-P,
–no-renumber

không đặt lại số dòng tại các trang logic

-NS,
– dấu phân tách số = STRING

thêm STRING sau số dòng (có thể)

-v,
–starting-line-number = NUMBER

số dòng đầu tiên trên mỗi trang logic

-w,
–number-width = NUMBER

sử dụng NUMBER cột cho số dòng
–Cứu giúp hiển thị trợ giúp và thoát
–phiên bản thông tin phiên bản màn hình và thoát

Theo mặc định, nl chọn -v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn. CC là hai ký tự phân cách để phân tách các trang logic. Một ký tự thứ hai bị thiếu có nghĩa là dấu hai chấm (:).

Đối với một dấu gạch chéo ngược (), hai dấu gạch chéo ngược (\).

STYLE là một trong số:

Một đánh số tất cả các dòng
NS chỉ đánh số dòng trống
n số không có dòng

pBRE

chỉ đánh số các dòng chứa kết quả phù hợp với biểu thức chính quy cơ bản, BRE

ĐỊNH DẠNG là một trong số:

ln căn trái, không có số 0 ở đầu
rn hợp lý đúng, không có số 0 ở đầu
rz hợp lý bên phải, số 0 ở đầu

Các ví dụ

cat list.txt
apples
oranges
potatoes
lemons
garlic
nl list.txt
     1	apples
     2	oranges
     3	potatoes
     4	lemons
     5	garlic

Trong ví dụ trên, chúng ta sử dụng lệnh cat để hiển thị nội dung của list.txt. Sau đó, chúng tôi sử dụng nl để đánh số mỗi dòng và hiển thị kết quả ra đầu ra tiêu chuẩn.

nl list.txt > nlist.txt
cat nlist.txt
     1	apples
     2	oranges
     3	potatoes
     4	lemons
     5	garlic

Trong ví dụ trên, chúng tôi chạy cùng một lệnh nl, nhưng chuyển hướng đầu ra đến một tệp mới, nlist.txt. Sau đó, chúng tôi sử dụng cat để hiển thị kết quả.

wc – Hiển thị số dòng, từ và ký tự trong một tệp.

Linux offers a wide range of commands for text formatting and editing.While editing a text file, you might want to display the lines with line numbers appended before them, and here comes the role-play of the nl command in Linux nl command is a Unix/Linux utility that is used for numbering lines, accepting input either from a file or from STDIN. It copies each specified file to STDOUT, with line numbers appended before the lines. 

Syntax:

nl [OPTION]... [FILE]...

Options:

-b NUMBER or -bNUMBER : used for numbering body lines
-i NUMBER or -iNUMBER : line number increment at each line
-n FORMAT or -nNUMBER : insert line numbers according to FORMAT
-v NUMBER or -vNUMBER : change first line number of the given input 
-l NUMBER or -lNUMBER : group of NUMBER empty lines are counted as one
-s STRING or -sSTRING : add any STRING after every logical line number
-w NUMBER or -wNUMBER : use different NUMBER columns for line numbers

Examples:

Note: Consider the following file as input in all the examples.

1. To display a file with line numbers: Numbers all non-empty lines. 

$ nl geekfile.txt

Lệnh nl trong linux

2. To number all lines (including empty lines also): -b a option is used to number all lines whether empty or non-empty.

$ nl -b a geekfile.txt

3. Count multiple, consecutive, non-empty lines as one: -l optionis used to count all non-empty lines as a single logical line to nl command.Linux considers NUMBER consecutive empty lines as a single logical line for numbering, and only numbers the last one. If any empty consecutive lines less than NUMBER occur, it will discard them.

$ nl -l 1 geekfile.txt 
## is same as 
## $ nl geekfile.txt

The example below considers 3 consecutive empty lines as a single logical line. 

$ nl -b a -l 3 geekfile.txt

Here, -b a option is used to consider all logical lines (whether empty or non-empty) as input to nl command.

4. Override default increment: The default increment pattern in Linux is 1. This can be changed using the -i option. The first line number is 1 and cannot be changed using -i.

$ nl -i 3 geekfile.txt
or
$ nl -i3 geekfile.txt

5. To make the starting line number different: The default line number is 1. This can be changed using the -v option.

nl -v 4 geekfile.txt
or
nl -v4 geekfile.txt

6. Add a string literal after line numbers: Any STRING literal can be added after a line number using the -s option.

$ nl -s "..." geekfile.txt

7. Change column for line numbers: Different columns can be used to display the file output using the -w option. The default column number is 1.

$ nl -w2 geekfile.txt
$ nl -w4 geekfile.txt
$ nl -w6 geekfile.txt
or
$ nl -w 2 geekfile.txt
$ nl -w 4 geekfile.txt
$ nl -w 6 geekfile.txt

Notice the change in column number (increase in indentation from left) in the example illustrated below.

8. To number all logical lines that match the specified REGEX: -b pREGEXP option can be used to append line numbers before those lines only that match the given pattern. The following command will number those lines that begin with F.

$ nl -b pF geekfile.txt

9. To print the lines using a different number format: The numbering formats can be specified using the -n option. The default numbering format is rn. The available options are:

  • ln -> left-justified, no leading zeros
  • rn -> right-justified, no leading zeros
  • rz -> right-justified with leading zeros
$ nl -n ln geekfile.txt
$ nl -n rn geekfile.txt
$ nl -n rz geekfile.txt