【Linux】一步一步学Linux——cut命令(44)
发布日期:2021-06-29 20:59:39
浏览次数:2
分类:技术文章
本文共 3092 字,大约阅读时间需要 10 分钟。
00. 目录
文章目录
01. 命令概述
cut - 在文件的每一行中提取片断
说明:该命令有两项功能,其一是用来显示文件的内容,它依次读取由参数所指 明的文件,将它们的内容输出到标准输出上;其二是连接两个或多个文件,如cut fl f2 > f3
将把文件fl和几的内容合并起来,然后通过输出重定向符“>”的作用,将它们放入文件f3中。
02. 命令格式
cut [选项]… [文件]…
03. 常用选项
cut - 在文件的每一行中提取片断在 每个文件 FILE 的 各行 中, 把 提取的 片断 显示在 标准输出.-b, --bytes=LIST 输出 这些 字节 -c, --characters=LIST 输出 这些 字符 -d, --delimiter=DELIM 使用 DELIM 取代 TAB 做 字段(field) 分隔符 -f, --fields=LIST 输出 这些 字段 -n (忽略) -s, --only-delimited 不显示 没有 分隔符 的 行 --output-delimiter=STRING 使用 STRING 作为 输出分隔符, 缺省 (的 输出分隔符) 为 输入分隔符 --help 显示 帮助信息, 然后 结束 --version 显示 版本信息, 然后 结束 使用 且 只使用 -b, -c 或 -f 中的 一个 选项. LIST 由 一个 范围 (range) 或 逗号 隔开的 多个 范围 组成. 范围 是 下列 形式 之一:N 第 N 个 字节, 字符 或 字段, 从 1 计数 起 N- 从 第 N 个 字节, 字符 或 字段 直至 行尾 N-M 从 第 N 到 第 M (并包括 第M) 个 字节, 字符 或 字段 -M 从 第 1 到 第 M (并包括 第M) 个 字节, 字符 或 字段 如果 没有 指定 文件 FILE, 或 FILE 是 -, 就从 标准输入 读取 数据.
04. 参考示例
示例文件
[deng@localhost test]$ cat testrootx:0:0:bin:x:1:1:daemon:x:2adm:x:3:4:lp:x:4:7:l[deng@localhost test]$ cat test2任务一任务二任务三任务四任务五任务六任务七[deng@localhost test]$
4.1 提取每一行的第一个字符
[deng@localhost test]$ cut -b 1 testrbdal[deng@localhost test]$
4.2 提取每一行第 1 3 5个字符
[deng@localhost test]$ cut -b 1,3,5 testroxbnxdeoamxl::[deng@localhost test]$
4.3 提取每一行第 1-5 个字符
[deng@localhost test]$ cut -b 1-5 testrootxbin:xdaemoadm:xlp:x:[deng@localhost test]$
4.4 提取每一行第 1-5 个字符
[deng@localhost test]$ cut -b -5 testrootxbin:xdaemoadm:xlp:x:[deng@localhost test]$
4.5 提取每一行第 3个字符以后的
[deng@localhost test]$ cut -b 3- testotx:0:0:n:x:1:1:emon:x:2m:x:3:4::x:4:7:l[deng@localhost test]$
4.6 提取每一行的第一个汉字
[deng@localhost test]$ cut -b 1 test2 ]0;deng@localhost:~/test[deng@localhost test]$ [deng@localhost test]$
出现了乱码的现象,因为-b 只是针对字节进行提取,对一个汉字进行字节提取,得到的结果必然是乱码,若想使用 -b 命令对字节进行提取,那么则需要使用 -n 选项,此选项的作用是取消分割多字节字符。
[deng@localhost test]$ cut -nb 1 test2任任任任任任任[deng@localhost test]$
或者
[deng@localhost test]$ cut -c 1 test2任任任任任任任[deng@localhost test]$
以上是提取第一个汉字。
4.7 提取前面两个汉字
[deng@localhost test]$ cut -nb 1,2 test2任务任务任务任务任务任务任务[deng@localhost test]$
或者
[deng@localhost test]$ cut -c 1,2 test2任务任务任务任务任务任务任务[deng@localhost test]$
4.8 提取前面三个汉字
[deng@localhost test]$ cut -nb 1,2,3 test2任务一任务二任务三任务四任务五任务六任务七[deng@localhost test]$
或者
[deng@localhost test]$ cut -c 1,2,3 test2任务一任务二任务三任务四任务五任务六任务七[deng@localhost test]$
4.9 提取前面四个汉字
文档中没有四个汉字的情形
[deng@localhost test]$ cut -nb 1,2,3,4 test2任务一任务二任务三任务四任务五任务六任务七[deng@localhost test]$
或者
[deng@localhost test]$ cut -c 1,2,3,4 test2任务一任务二任务三任务四任务五任务六任务七[deng@localhost test]$
4.10 以:分隔,将文件每一行分成若干列,显示第一列
[deng@localhost test]$ cut -d: -f1 /etc/passwdrootbindaemonadmlpsyncshutdown
4.11打印除了第二个字符之外的列
–complement 选项提取指定字符之外的列(打印除了第二个字符之外的列)
[deng@localhost test]$ cut -c2 --complement testrotx:0:0:bn:x:1:1:demon:x:2am:x:3:4:l:x:4:7:l[deng@localhost test]$ [deng@localhost test]$ cut -c1,2 --complement testotx:0:0:n:x:1:1:emon:x:2m:x:3:4::x:4:7:l[deng@localhost test]$
4.12 定义以空格分隔,然后输出第二个字段
[deng@localhost test]$ cat testr ootx:0:0:b in:x:1:1:d aemon:x:2a dm:x:3:4:l p:x:4:7:l[deng@localhost test]$ cut -d ' ' -f2 testootx:0:0:in:x:1:1:aemon:x:2dm:x:3:4:p:x:4:7:l[deng@localhost test]$
05. 附录
参考:
转载地址:https://dengjin.blog.csdn.net/article/details/96114261 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2024年04月05日 09时45分44秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
WCF传递Stream时,同时传递其它参数的问题
2019-04-30
abp vNex
2019-04-30
关闭页面前js提示
2019-04-30
window.open关闭后刷新父页面
2019-04-30
ajax异步提交修改按钮文字
2019-04-30
draw.io
2019-04-30
Convert VB.NET to C#
2019-04-30
VB内部函数(三)——财务函数
2019-04-30
Financial.IPmt/Financial.PPmt
2019-04-30
ASP.NET Boilerplate
2019-04-30
服务器后端发展三个阶段
2019-04-30
easyui-快速布局
2019-04-30
EPPlus--ExcelPackage 导出Excel
2019-04-30
setInterval
2019-04-30
window.open关闭窗体后刷新
2019-04-30
easyui.dialog
2019-04-30
js保存指定html
2021-07-03
会改bug不值钱,预防bug才值钱
2021-07-03
confluence--团队协同与知识管理工具
2021-07-03