Linux(Centos 7.6)命令详解:wc

1.命令作用

打印文件的行数、单词数、字节数,如果指定了多个文件,还会打印以上三种数据的总和(Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified)

2.命令语法

Usage: wc [OPTION]... [FILE]...
or: wc [OPTION]... --files0-from=F

3.参数详解

OPTION:

  • -c, --bytes,打印字节数
  • -m, --chars,打印字符数
  • -l, --lines,打印行数
  • --files0-from=F/-,F代表一个文件名,文件名内容为多个文件的名称,用于获取多个文件的行数、单词数、字节数,多个文件以一个零字节(ASCII NUL)结束;当使用 - 时,以其他输出为输入,如find使用-print0参数
  • -L, --max-line-length,打印最长行的长度
  • -w, --words,打印单词数

4.常用用例

4.1.常见的用例

bash 复制代码
## 不加参数,会依次打印行数、单词数、字节数
[root@node2 Desktop]# wc test.txt 
  5  44 245 test.txt

## 多个文件时,最后会打印total总计数据
[root@node2 Desktop]# wc test.txt test1.txt 
  5  44 245 test.txt
  8   9  57 test1.txt
 13  53 302 total

## 常用参数-l 用于查看文件行数
[root@node2 Desktop]# wc -l test.txt
5 test.txt

## 常用参数-L 用于查看文件中最长的行的长度
[root@node2 Desktop]# wc -L test.txt
72 test.txt

4.2.有用但不常见的用例

bash 复制代码
## --files0-from=F 参数使用
## vi打开文件如下,"^@"是一个字符,vi中输入方法为Ctrl+v,然后接着 Ctrl+@ 
[root@node2 Desktop]# vi file
test1.txt^@test2.txt^@test3.txt^@
~                                                                                                              
~                                                                                                              
~                                                                                                              
~                                                                                                              
~                                                                                                              
~                                                                                                              
~                                                                                                              
~                                                                                                                                                                                                                         
"file" [noeol] 1L, 30C
[root@node2 Desktop]# cat file               ## cat查看如下
test1.txttest2.txttest3.txt
[root@node2 Desktop]# 
[root@node2 Desktop]# truncate -s -1 file    ## 删除文件最后一个字符(换行符)
[root@node2 Desktop]# 
[root@node2 Desktop]# cat file               ## 再次查看与上面对比
test1.txttest2.txttest3.txt[root@node2 Desktop]#
[root@node2 Desktop]# 
[root@node2 Desktop]# wc --files0-from=file  ## 可正常指定文件列表进行统计
  8   9  57 test1.txt
  9   9  63 test2.txt
  4   4  28 test3.txt
 21  22 148 total
[root@node2 Desktop]# 

#########################################################################
#########################################################################
## --files0-from=- 参数使用
## find中-print0参数的作用是在输出文件名时使用null字符(\0)作为分隔符,而不是换行符
[root@node2 Desktop]# find . -name '*.txt' -print0 | wc -l --files0-from=-
4 ./test3.txt
9 ./test2.txt
8 ./test1.txt
5 ./test.txt
26 total
相关推荐
茗创科技2 分钟前
Nature Neuroscience | 如何在大规模自动化MRI分析中规避伪影陷阱?
运维·自动化
__Smile°22 分钟前
Gitlab+Jenkins+K8S+Registry 建立 CI/CD 流水线
linux·ci/cd·docker·kubernetes·gitlab·jenkins
千里镜宵烛41 分钟前
互斥锁与条件变量
linux·开发语言·c++·算法·系统架构
爱喝水的鱼丶1 小时前
SAP-ABAP: Open SQL集合函数COUNT(统计行数)、SUM(数值求和)、AVG(平均值)、MAX/MIN(极值)深度指南
运维·数据库·sql·sap·报表·abap·程序
AI 嗯啦2 小时前
linux的用户操作(详细介绍)
linux·运维·服务器
AOwhisky2 小时前
云计算一阶段Ⅱ——12. SELinux 加固 Linux 安全
linux·安全·云计算
Ronin3052 小时前
【Linux系统】进程间通信:命名管道
linux·服务器·命名管道
东东今天敲代码了吗2 小时前
Ubuntu20.04 离线安装 FFmpeg 静态编译包
linux·运维·服务器·ubuntu·ffmpeg
The god of big data2 小时前
最新教程 | CentOS 7 下 MySQL 8 离线部署完整手册(含自动部署脚本)
linux·mysql·centos
LH_R2 小时前
OneTerm 开源堡垒机 | 历时三个月重构大更新
运维·后端·安全