zoukankan      html  css  js  c++  java
  • Linux

    一 wc简单介绍

           wc命令用来打印文件的文本行数、单词数、字节数等(print the number of newlines, words, and bytes in files)。在Windows的Word中有个“字数统计”的工具,能够帮我们把选中范围的字数、字符数统计出来。Linux下的wc命令能够实现这个 功能。使用vi打开文件的时候。底下的信息也会显示行数和字节数。

    二 经常使用參数

    格式:wc -l <file>

    打印指定文件的文本行数。(l=小写L)

    下面參数可组合使用。

    參数:-c, --bytes
    打印字节数(print the byte counts)

    參数:-m, --chars
    打印字符数(print the character counts)

    參数:-l, --lines
    打印行数(print the newline counts)

    參数:-L, --max-line-length
    打印最长行的长度(print the length of the longest line)

    參数:-w, --words
    打印单词数(print the word counts)

     

    三 使用演示样例

    演示样例 一

    [root@jfht ~]# wc /etc/passwd 
      46   66 2027 /etc/passwd
    行数 单词数 字节数 文件名称 

    [root@jfht ~]# wc -l /etc/passwd 
    46 /etc/passwd
    [root@jfht ~]# wc -cmlwL /etc/passwd 
     46   66 2027 2027   74 /etc/passwd
    [root@jfht ~]# wc -cmlLw /etc/passwd 
     46   66 2027 2027   74 /etc/passwd
    [root@jfht ~]# wc -wcmlL /etc/passwd 
     46   66 2027 2027   74 /etc/passwd
    [root@jfht ~]#

    问题来了:从上面的命令行运行结果来看,wc的输出数据的顺序与的几个參数的顺序好像没有关系?!

    演示样例二 用wc命令怎么做到仅仅打印统计数字不打印文件名称

    使用管道线,这在编写shell脚本时特别实用。

    [root@jfht ~]# wc -l /etc/passwd 
    46 /etc/passwd
    [root@jfht ~]# cat /etc/passwd | wc -l 
    46
    [root@jfht ~]#

    演示样例三 中文编码的问题

    运行环境是中文编码的。

    [root@jfht ~]# echo $LANG

    zh_CN.UTF-8

    中文编码文件ehr_object.gv,UTF8编码的文件ehr_object_utf8.gv。

    [root@jfht ~]# file ehr_object.gv ehr_object_utf8.gv 
    ehr_object.gv:      ISO-8859 text
    ehr_object_utf8.gv: UTF-8 Unicode text
    [root@jfht ~]#

    [root@jfht ~]# wc ehr_object.gv ehr_object_utf8.gv 
      11  105  830 ehr_object.gv
    wc: ehr_object_utf8.gv:4: 无效或不完整的多字节字符或宽字符
      11  105  866 ehr_object_utf8.gv
      22  210 1696 总计
    [root@jfht ~]#

     

    演示样例四 中文单词数的计算

    [root@jfht ~]# cat test1

    你好中国
    Linux


    [root@jfht ~]# wc test1

    2 2 19 test1

    行数 单词数 字节数 文件名称 

  • 相关阅读:
    Session的使用与Session的生命周期
    Long-Polling, Websockets, SSE(Server-Sent Event), WebRTC 之间的区别与使用
    十九、详述 IntelliJ IDEA 之 添加 jar 包
    十八、IntelliJ IDEA 常用快捷键 之 Windows 版
    十七、IntelliJ IDEA 中的 Maven 项目初体验及搭建 Spring MVC 框架
    十六、详述 IntelliJ IDEA 创建 Maven 项目及设置 java 源目录的方法
    十五、详述 IntelliJ IDEA 插件的安装及使用方法
    十四、详述 IntelliJ IDEA 提交代码前的 Code Analysis 机制
    十三、IntelliJ IDEA 中的版本控制介绍(下)
    十二、IntelliJ IDEA 中的版本控制介绍(中)
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/5072832.html
Copyright © 2011-2022 走看看