本文共 5576 字,大约阅读时间需要 18 分钟。
Linux commands wc and nl will help you to identify the number of words, lines, bytes, etc, in a file. This tutorial explains how to use these two very useful command with various examples.
The basic text file that will be used in examples throughout this article is shown below :$ cat sort.txtUKAustraliaNewzealandBrazilAmerica
The nl utility in Linux is used to number lines of a file.
Here is the syntax and description from man page :
SYNOPSIS
nl [OPTION]… [FILE]…DESCRIPTION
Write each FILE to standard output, with line numbers added. With no FILE, or when FILE is -, read standard input.
Here is a basic example that explains how nl command can be used to number lines of a file.
$ cat sort.txtUKAustraliaNewzealandBrazilAmerica$ nl sort.txt 1 UK 2 Australia 3 Newzealand 4 Brazil 5 America
So we see that using nl command, all the lines of file sort.txt got numbered.
The option -i can be used to override the default increment of 1 in line numbers.
Here is an example where we have used -i to increase the line number increment to 5 :
$ nl -i5 sort.txt 1 UK 6 Australia 11 Newzealand 16 Brazil 21 America
Instead of the default 1,2,3… the line numbers are now displayed in increments of 5 (i.e 1,6,11…)
By default, the nl command adds only line numbers. But, through -s option, any string can be added that can act as a separator between line numbers and the line text.
Here is an example:
$ nl -s. sort.txt 1.UK 2.Australia 3.Newzealand 4.Brazil 5.America
So we see that the character ‘.’ was added after line numbers.
Columns for line number display can be changed using -w option.
Here is an example :
$ nl -w1 sort.txt1 UK2 Australia3 Newzealand4 Brazil5 America$ nl -w2 sort.txt 1 UK 2 Australia 3 Newzealand 4 Brazil 5 America$ nl -w3 sort.txt 1 UK 2 Australia 3 Newzealand 4 Brazil 5 America$ nl -w4 sort.txt 1 UK 2 Australia 3 Newzealand 4 Brazil 5 America$ nl -w5 sort.txt 1 UK 2 Australia 3 Newzealand 4 Brazil 5 America$ nl -w6 sort.txt 1 UK 2 Australia 3 Newzealand 4 Brazil 5 America
The exhaustive output above gives a good idea as to how the display column for line numbers can be changed.
Various STYLEs are available for line numbering. From the man page :
STYLE is one of:
- a – number all lines
- t – number only nonempty lines
- n – number no lines
- pBRE – number only lines that contain a match for the basic regular expression, BRE
In the example below, I have used a regular expression ‘pA’ as a STYLE with option -b. This regular expression matches the lines beginning with ‘A’ and so nl command numbers only those lines.
$ nl -bpA sort.txt UK 1 Australia Newzealand Brazil 2 America
So we see that only the lines beginning with ‘A’ were numbered.
There are various FORMATs available for inserting line numbers. From the man page :
FORMAT is one of:
- ln – left justified, no leading zeros
- rn – right justified, no leading zeros
- rz – right justified, leading zeros
Here is an example that demonstrated all the above formats:
$ nl -nln sort.txt1 UK2 Australia3 Newzealand4 Brazil5 America$ nl -nrn sort.txt 1 UK 2 Australia 3 Newzealand 4 Brazil 5 America$ nl -nrz sort.txt000001 UK000002 Australia000003 Newzealand000004 Brazil000005 America
Please note that you can also use .
The wc utility in Linux is used print information like number of newlines, words, byte counts of a file.
Here is the syntax and description from man page :
SYNOPSIS
wc [OPTION]… [FILE]… wc [OPTION]… –files0-from=FDESCRIPTION
Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. With no FILE, or when FILE is -, read standard input. A word is a non-zero-length sequence of characters delimited by white space.
Here is a basic example of Linux wc command :
$ cat sort.txtUKAustraliaNewzealandBrazilAmerica$ wc sort.txt 5 5 41 sort.txt
The three numbers produced in output correspond to number of lines, number of words and number of bytes. These three numbers are followed by name of the file.
The word count of a file can be displayed explicitly through -w option.
Here is an example :
$ wc -w sort.txt5 sort.txt
So we see that number of words were printed followed by the file name.
The wc command provides an option -L that can be used to display the length of longest line in the file.
Here is an example :
$ wc -L sort.txt10 sort.txt
So we see that length of the longest line (‘Newzealand’ in our case) was displayed in the output.
The wc command provides an option -l through which number of newlines can be displayed in the output.
Here is an example :
$ wc -l sort.txt5 sort.txt
So we see that there were 5 newlines in the file sort.txt
Total number of bytes in a file can be displayed by using -c option of the wc command.
Here is an example :
$ wc -c sort.txt41 sort.txt
转载地址:http://nhhbi.baihongyu.com/