学习笔记八(cat,more,less,head,tail,cut,paste,wc,sort,uniq,diff,patch)
2020-04-27 07:31
846 查看
学习笔记八
文本工具
抽取文本的工具
文件内容: cat ,more,less
文件截取:head,tail
按列抽取:cut
排序和统计:sort,wc
按关键字抽取:grep
文件查看
文件查看命令:
cat,nl,tac,rev cat [OPTION]... [FILE]... -E:显示行结束符$ -n:对显示出的每一行进行编号 -A:显示所有控制符 -b:非空行编号 -s:压缩连续的空行成一行 nl tac rev [root@centos7 data]#cat -E f1 a$ b $ c d$ 用$字符显示换行符号 [root@centos7 data]#cat -A f1 a$ b $ c ^Id$ 用^I显示tab键 [root@centos7 data]#cat -n f1 1 a 2 3 b 4 5 6 c d 把所有行前加个编号 [root@centos7 data]#cat -b f1 1 a 2 b 3 c d 所有非空行前加编号
分页查看文件内容
more:分页查看文件 more [OPTIONS...] FILE... -d: 显示翻页及退出提示 less:一页一页地查看文件或STDIN输出 查看时有用的命令包括: /文本 搜索 文本 n/N 跳到下一个 或 上一个匹配 less 命令是man命令使用的分页器
显示文本前或后行内容
head [OPTION]... [FILE]... -c # 指定获取前#字节 -n # 指定获取前#行 -# 指定行数 tail [OPTION]... [FILE]... -c # 指定获取后#字节 -n # 指定获取后#行 -# 同上 -f 跟踪显示文件fd新追加的内容,常用日志监控 相当于 --follow=descriptor -F 跟踪文件名,相当于--follow=name --retry tailf 类似tail –f,当文件不增长时并不访问文件
按列抽取文本cut和合并文件paste
cut [OPTION]... [FILE]... -d DELIMITER: 指明分隔符,默认tab -f FILEDS: #: 第#个字段 #,#[,#]:离散的多个字段,例如1,3,6 #-#:连续的多个字段, 例如1-6 混合使用:1-3,7 -c 按字符切割 --output-delimiter=STRING指定输出分隔符 [root@centos7 data]#ifconfig ens33 |head -2 |tail -1 |tr -s " " |cut -d" " -f3 192.168.160.129 通过head -2取出ifconfig ens33命令中的前两行,然后通过tail -1取出最后一行,然后tr -s " "把所有连续的空白符以单独空白符表示,最后通过cut命令,以空白符为分隔符分隔出我们需要的ip地址
cut和paste
显示文件或STDIN数据的指定列 cut -d: -f1 /etc/passwd cat /etc/passwd | cut -d: -f7 cut -c2-5 /usr/share/dict/words paste 合并两个文件同行号的列到一行 paste [OPTION]... [FILE]... -d 分隔符:指定分隔符,默认用TAB -s : 所有行合成一行显示 示例: paste f1 f2 paste -s f1 f2 [root@centos7 data]#paste f1 f2 1 11 2 12 3 13 4 14 5 15 6 16 7 17 8 18 9 19 10 20 把f1和f2合并,默认分隔符为tab [root@centos7 data]#paste -d: f1 f2 1:11 2:12 3:13 4:14 5:15 6:16 7:17 8:18 9:19 10:20 修改分隔符为: [root@centos7 data]#paste -s f1 f2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 先把f1和f2的文本从列转化为行,然后合并
收集文本统计数据wc
计数单词总数、行总数、字节总数和字符总数
可以对文件或STDIN中的数据运行
wc story.txt
39 237 1901 story.txt
行数 字数 字节数
常用选项
-l 只计数行数
-w 只计数单词总数
-c 只计数字节总数
-m只计数字符总数
-L 显示文件中最长行的长度
[root@centos7 ~]#wc /data/f1 10 10 21 /data/f1
文本排序sort
把整理过的文本显示在STDOUT,不改变原始文件
sort [options] file(s)
常用选项
-r 执行反方向(由上至下)整理
-R 随机排序
-n 执行按数字大小整理
-f 选项忽略(fold)字符串中的字符大小写
-u 选项(独特,unique)删除输出中的重复行
-t c 选项使用c做为字段界定符
-k X 选项按照使用c字符分隔的X列来整理能够使用多次
[root@centos7 ~]#cut -d: -f1,3 /etc/passwd |sort -t: -k2 -n root:0 bin:1 daemon:2 adm:3 lp:4 sync:5 shutdown:6 halt:7 mail:8 operator:11 games:12 ftp:14 rpcuser:29 rpc:32 ntp:38 gdm:42 tss:59 avahi:70 tcpdump:72 sshd:74 radvd:75 dbus:81 postfix:89 nobody:99 qemu:107 usbmuxd:113 pulse:171 rtkit:172 abrt:173 systemd-network:192 gnome-initial-setup:988 saned:989 geoclue:990 unbound:991 chrony:992 setroubleshoot:993 saslauth:994 gluster:995 colord:996 libstoragemgmt:997 sssd:998 polkitd:999 li:1000 gentoo:1001 nginx:1002 varnish:1003 mysql:1004 nfsnobody:65534 先通过cut命令吧passwd的用户和UID剪切出来,然后执行sort命令,-t:把:作为字段界定符,区分列数,-k2则是把第二列单独拿出来,-n则是按照从小到大进行排序
uniq
uniq命令:从输入中删除前后相接的重复的行 uniq [OPTION]... [FILE]... -c: 显示每行重复出现的次数 -d: 仅显示重复过的行 -u: 仅显示不曾重复的行 注:连续且完全相同方为重复 常和sort 命令一起配合使用: sort userlist.txt | uniq -c
比较文件
比较两个文件之间的区别
diff foo.conf foo2.conf 5c5 < use_widgets = no --- > use_widgets = yes 注明第5行有区别(改变)
复制对文件改变patch
diff 命令的输出被保存在一种叫做“补丁”的文件中
使用 -u 选项来输出“统一的(unified)”diff格式文件,最适用于补丁文件
patch 复制在其它文件中进行的改变(要谨慎使用)
适用 -b 选项来自动备份改变了的文件
diff -u foo.conf foo2.conf > foo.patch patch -b foo.conf foo.patch
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- linux cat,tac,more,less,head,tail,cut,sort,uniq,wc,tr命令的使用
- linux基础篇-07,linux文本处理cat more less head tail sort uniq grep cut jion sed awk
- Linux - 1.5文本文件编辑命令(cat,more,head,tail,tr,wc,stat,cut,diff)
- 文本处理命令 cat more less cut wc sort uniq
- Linux入门笔记——cat、sort、uniq、wc、head、tail、tee
- Linux学习笔记——信息显示命令cat、more、less、head和tail
- linux-12基本命令之 cat,more,head, tail ,tr,od,wc,cut,diff
- Linux学习笔记(PATH,cp,mv,文档查看cat/more/less/head/tail)
- linux学习第七篇:环境变量PATH,命令cp,mv以及文档查看cat,more,less,head,tail
- linux笔记:文件处理命令touch,cat,more,less,head,tail
- 学习cat sort grep cut sed uniq find less
- grep&fgrep&egrep&wc&diff&patch&wc&cut&sort&uniq
- 查看文件命令cat,more,less,tail,tac,nl,od---linux学习笔记
- cat more less wc -l head tail
- centos shell基础 alias 变量单引号 双引号 history 错误重定向 2>&1 jobs 环境变量 .bash_history source配置文件 nohup & 后台运行 cut,sort,wc ,uniq ,tee ,tr ,split, paste cat> 2.txt <<EOF 通配符 glob模式 发邮件命令mail 2015-4-8 第十二节课
- 【linux学习笔记】sort|uniq | cut | wc (我的第一篇markdown笔记)
- 命令详解查看文件cat、more、less、head、tail、wc、grep、echo
- grep,cut,wc,sort,diff,uniq,patch命令
- 每日命令:过滤命令cut、sort、uniq、wc、tee、head、tail、grep、
- 文本查看及处理工具:wc, cut, sort, uniq, diff, patch