count lines in a file - wc & nl

本文介绍如何利用Linux中的wc和nl命令来统计文件中的行数、单词数及字节数等信息,并通过多个实例详细说明这些命令的用法。

How to Count Number of Lines in a File in Linux (wc and nl Command Examples)

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.txt
UK
Australia
Newzealand
Brazil
America

Linux nl Command Examples

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.

1. A basic example

Here is a basic example that explains how nl command can be used to number lines of a file.

$ cat sort.txt
UK
Australia
Newzealand
Brazil
America

$ 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.

2. Increment line numbers with any value using -i option

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…)

3. Add string after line numbers using -s option

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.

4. Use a different column for line numbers using -w option

Columns for line number display can be changed using -w option.

Here is an example :

$ nl -w1 sort.txt
1	UK
2	Australia
3	Newzealand
4	Brazil
5	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.

5. Use STYLE for numbering lines using -b option

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.

6. Use different FORMAT for inserting line numbers using -n options

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.txt
1     	UK
2     	Australia
3     	Newzealand
4     	Brazil
5     	America

$ nl -nrn sort.txt
     1	UK
     2	Australia
     3	Newzealand
     4	Brazil
     5	America

$ nl -nrz sort.txt
000001	UK
000002	Australia
000003	Newzealand
000004	Brazil
000005	America

Please note that you can also use sed command to count the number of lines in a file .

Linux wc Command Examples

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=F

DESCRIPTION 
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.

1. A basic example

Here is a basic example of Linux wc command :

$ cat sort.txt
UK
Australia
Newzealand
Brazil
America

$ 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.

2. Display word count through -w option

The word count of a file can be displayed explicitly through -w option.

Here is an example :

$ wc -w sort.txt
5 sort.txt

So we see that number of words were printed followed by the file name.

3. Display length of longest line through -L option

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.txt
10 sort.txt

So we see that length of the longest line (‘Newzealand’ in our case) was displayed in the output.

4. Display number of newlines through -l option

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.txt
5 sort.txt

So we see that there were 5 newlines in the file sort.txt

5. Display number of bytes through -c option

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.txt
41 sort.txt
### Shell 脚本中 `line_count=$(wc -l < /etc/passwd)` 和 `echo "The file /etc/passwd has $line_count lines."` 的执行过程与语法符号解析 在 Shell 脚本中,`line_count=$(wc -l < /etc/passwd)` 与 `echo "The file /etc/passwd has $line_count lines."` 是两个典型的语句,分别涉及**子命令替换**和**变量引用**机制。 #### 子命令替换:`line_count=$(wc -l < /etc/passwd)` 该语句使用了子命令替换功能,其结构为: ```bash line_count=$(wc -l < /etc/passwd) ``` - **`$(...)`**:这是子命令替换的语法形式,表示先执行括号内的命令,并将命令的标准输出结果作为值赋给变量 `line_count`。这种写法比反引号(`` `...` ``)更清晰易读,且支持嵌套使用。 - **`wc -l`**:这是一个文本统计工具,用于统计行数、字节数等信息;其中 `-l` 表示仅输出文件的行数。 - **`< /etc/passwd`**:这是输入重定向操作符,表示将 `/etc/passwd` 文件的内容作为 `wc -l` 命令的输入源。 - **`line_count=`**:将子命令的输出结果赋值给变量 `line_count`。 整个语句的执行流程如下: 1. 执行 `wc -l < /etc/passwd`,统计 `/etc/passwd` 文件的总行数; 2. 将该行数作为字符串值赋给变量 `line_count`。 例如,如果 `/etc/passwd` 包含 45 行,则 `line_count` 变量的值变为 `"45"`。 #### 变量引用与输出:`echo "The file /etc/passwd has $line_count lines."` 该语句使用了变量引用功能,用于将变量值插入到输出字符串中: ```bash echo "The file /etc/passwd has $line_count lines." ``` - **`$line_count`**:这是变量引用语法,表示将变量 `line_count` 的当前值插入到双引号包裹的字符串中。 - **双引号 `"`**:用于保持字符串的整体性,防止空格或特殊字符导致意外的词拆分(word splitting)。 - **`echo`**:是 Shell 中常用的输出命令,用于将指定字符串打印到终端或日志文件中。 当 `line_count` 的值为 `"45"` 时,该语句的输出结果为: ``` The file /etc/passwd has 45 lines. ``` #### 示例代码总结 以下是一个完整的脚本示例,展示了上述语句的实际应用: ```bash #!/bin/bash line_count=$(wc -l < /etc/passwd) echo "The file /etc/passwd has $line_count lines." ``` 此脚本会动态获取 `/etc/passwd` 文件的行数,并将其嵌入到提示信息中进行输出。 --- ### 注意事项 - 子命令替换中的命令会在子 Shell 中执行,因此不会影响当前 Shell 的环境状态。 - 如果 `/etc/passwd` 文件不存在或无法访问,`wc -l` 将返回错误信息,此时 `line_count` 的值为空字符串。 - 使用 `$()` 替换机制可以嵌套多个命令,实现复杂的数据处理逻辑[^1]。 --- ### 相关问题 1. 如何在 Shell 脚本中处理子命令替换失败的情况? 2. 在 Shell 中,为什么推荐使用 `$()` 而不是反引号进行子命令替换? 3. 双引号在 Shell 脚本中对变量引用的作用是什么?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值