Linux下文本处理命令的使用
一、查看文件的部分截取
1、head:显示文件的开头几行,默认显示前10行;
head [–n 行数] 文件名
---------------------------------------------------------------------------------
[root@localhost ~]# head -n 3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
---------------------------------------------------------------------------------
2、tail:显示文件的最后几行,默认显示后10行;
tail [选项] 文件名
-n:确定显示的行数; tail [–n 行数] 文件名
-f:可以一直不断的查看某个文件的更新; tail -f 文件名 通常用来查看系统日志;调试服务、make程序时使用;直到按Ctrl-c为止。
---------------------------------------------------------------------------------
[root@localhost ~]# tail -f /var/log/messages
Aug 23 11:27:55 localhost syslogd 1.4.1: restart.
Aug 23 12:46:49 localhost NET[7159]: /sbin/dhclient-script : updated /etc/resolv.conf
…………后面会根据系统的情况持续更新显示,直到按Ctrl - c
---------------------------------------------------------------------------------
思考:查看/etc/passwd第5行-第10行的如何打命令呢?
三、抽取文本命令:
1、正则表达式:
• [0-9] [a-z] [A-Z]等 表示一个集合;
• [abc]:匹配列表里的任何一个字符
• [^abc]:匹配列表以外的字符
• ^abc:匹配以abc开头
• abc$:匹配以abc结尾的
2、grep:显示文件或标准输入中匹配的文本内容
Ø 下面我们看一下grep和正规表达式一起使用的案例:
1)[abc]:
---------------------------------------------------------------------------------
[root@localhost ~]# ls
anaconda-ks.cfg Desktop install.log install.log.syslog
[root@localhost ~]# ls |grep '[ai]n'
anaconda-ks.cfg
install.log
install.log.syslog
---------------------------------------------------------------------------------
2) [^abc]
---------------------------------------------------------------------------------
[root@localhost ~]# ls
anaconda-ks.cfg Desktop install.log install.log.syslog test
[root@localhost ~]# ls |grep '[^i]n'
anaconda-ks.cfg
---------------------------------------------------------------------------------
3)^abc
---------------------------------------------------------------------------------
[root@localhost ~]# ls
anaconda-ks.cfg Desktop install.log install.log.syslog test
[root@localhost ~]# ls |grep '^in'
install.log
install.log.syslog
---------------------------------------------------------------------------------
4)abc$
---------------------------------------------------------------------------------
[root@localhost ~]# ls
anaconda-ks.cfg Desktop install.log install.log.syslog test
[root@localhost ~]# ls|grep 'log$'
install.log
install.log.syslog
---------------------------------------------------------------------------------
Ø grep命令选项:
-i :搜索匹配的关键词时忽略大小写;
-n :显示匹配的行的行号;
-v :过滤掉匹配关键字的行,显示不匹配的;
---------------------------------------------------------------------------------
[root@localhost ~]# ls
anaconda-ks.cfg Desktop install.log install.log.syslog
[root@localhost ~]# ls |grep -v ^i
anaconda-ks.cfg
Desktop
---------------------------------------------------------------------------------
3、cut:显示文件或者标准输入数据的指定的列
cut –d区分分割的定界符 –f 要显示的列的编码 文件名
-d:指定区分的定界符,默认为TAB
-f:指定要显示的列的编码
---------------------------------------------------------------------------------
[root@server ~]# cut -d: -f1 /etc/passwd
root
Bin
daemon
……下面省略
---------------------------------------------------------------------------------
三、文本分析处理工具:
1、wc文本统计:
wc [选项] 目标文件
---------------------------------------------------------------------------------
[root@server ~]# wc /etc/passwd
35 54 1589 /etc/passwd
行数 单次总数 字节总数
---------------------------------------------------------------------------------
-l:只统计行数
-w:只统计单次总数
-c:只统计字节数
-m:只统计字符总数,包含不显示的;
2、diff:比较文件:
diff 文件1 文件2
---------------------------------------------------------------------------------
[root@server ~]# diff install.log install.log1
9c9
< 安装 nash-5.1.19.6-54.i386
---
> 装 nash-5.1.19.6-54.i386
---------------------------------------------------------------------------------
diff –u 文件1 文件2 >补丁文件名 比较文件,然后把不同写到补丁文件中
---------------------------------------------------------------------------------
[root@localhost ~]# cat test test1
this is a test
where are yourhoume?
this is a exam
where are yourtown?
[root@localhost ~]# diff -u test test1 >test.patch
[root@localhost ~]# cat test.patch
--- test 2010-08-26 15:17:31.000000000 +0800
+++ test1 2010-08-26 15:17:56.000000000 +0800
@@ -1,2 +1,2 @@
-this is a test
-where are yourhoume?
+this is a exam
+where are yourtown?
[root@localhost ~]# ls
anaconda-ks.cfg Desktop install.log test test1 test.patch
---------------------------------------------------------------------------------
3、patch:应用文件在其他文件中的改变
patch [-b] 目标文件名 .patch的比较文件
.patch的文件:由diff命令比较创建
-b:备份目标文件;
---------------------------------------------------------------------------------
[root@localhost ~]# cat test test1
this is a test
where are yourhoume?
this is a exam
where are yourtown?
[root@localhost ~]# patch -b ./test test.patch
patching file ./test
[root@localhost ~]# cat test
this is a exam
where are yourtown?
---------------------------------------------------------------------------------
4、sort:整理文本命令:
sort [选项] 文件
-r :执行反方向整理(有上之下)
---------------------------------------------------------------------------------
[root@server ~]# grep bash /etc/passwd|sort
op:x:501:501::/home/op:/bin/bash
redhat:x:500:500::/home/redhat:/bin/bash
root:x:0:0:root:/root:/bin/bash
[root@server ~]# grep bash /etc/passwd|sort -r
root:x:0:0:root:/root:/bin/bash
redhat:x:500:500::/home/redhat:/bin/bash
op:x:501:501::/home/op:/bin/bash
---------------------------------------------------------------------------------
-n:按照数字大小整理
-u:删除输出中的重复行;
-t 符号:使用符号作为字段的定界符;
-k 列数:按照使用的定界符分割的字段的第 列数 来整理;
---------------------------------------------------------------------------------
[root@server ~]# sort -t : -k 3 -r /etc/passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
news:x:9:13:news:/etc/news:
sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
……后面省略
---------------------------------------------------------------------------------
5、tr:把某个集合内的字符换成另外一个集合中的相应的字符
tr ‘[a-z]’ ‘[A-Z]’ <目标文件 >新文件名
目标文件里的小写字母替换成大写然后不存成新文件;
---------------------------------------------------------------------------------
[root@localhost ~]# tr '[a-z]' '[A-Z]' <anaconda-ks.cfg >an.bak
[root@localhost ~]# cat an.bak
# THE FOLLOWING IS THE PARTITION INFORMATION YOU REQUESTED
# NOTE THAT ANY PARTITIONS YOU DELETED ARE NOT EXPRESSED
# HERE SO UNLESS YOU CLEAR ALL PARTITIONS FIRST, THIS IS
# NOT GUARANTEED TO WORK
---------------------------------------------------------------------------------
转载于:https://blog.51cto.com/iminmin/384014
Linux下文本处理命令的使用相关推荐
- linux下Vi编辑器命令大全
linux下Vi编辑器命令大全 /根目录下的文件夹 输入"/",回车 里面有很多文件夹,如:etc,home,lib,mnt等等 etc:存放用户名和密码 home:每个用户有个文 ...
- Linux下的grep命令
Linux下的grep命令 作用:grep 指令用于查找内容包含指定的范本样式的文件,如果发现某文件的内容符合所指定的范本样式,预设 grep 指令会把含有范本样式的那一列显示出来.若不指定任何文件名 ...
- linux 文档编辑器 word,linux下vi编辑器命令大全
常用命令linux下vi编辑器命令大全来源:考试大 [学而优则进,考试大以成!] 2007年8月14日 文本编辑器是所有计算机系统中最常用的一种工具.UNIX下的编辑器有ex,sed和vi等,其中,使 ...
- linux 下的vi命令汇总
linux 下的vi命令汇总 linux 下的vi 主要作用是什么? Vi是Unix及Linux系统bai下标准的编辑器,由美国加州大学伯克利分校的Bill Joy所创立. 基本上vi可以分为三种状态 ...
- linux 下 FFmpeg 录制命令
linux 下 FFmpeg 录制命令 转载自:http://blog.topspeedsnail.com/archives/1325 录制屏幕(mkv格式): ffmpeg -f x11grab - ...
- linux下的ld命令(1)
使用ld 本文档介绍GNU连接器ld的2.14版本. 本文档在GNU自由文档许可证下发行.在"GNU自由文档许可证"一章中有关于本许可证的一份拷贝. 概述 'ld'把一定量的目标文 ...
- linux下grep同时匹配多个单词,Linux下使用grep命令搜索多个单词的方法
摘要 腾兴网为您分享:Linux下使用grep命令搜索多个单词的方法,篆刻字典,中国知网,虚拟机,微博等软件知识,以及掌上公交app,尚书ocr,汇博企业,强密码,支付宝,mh,海融易,我的世界火影忍 ...
- Linux下视频截取命令
比如你有一个视频,然后你对其中某一段感兴趣,你想把他截取下来,并且不希望画面变差,(当然你也不想花钱买视频编辑软件),可以在Linux下使用如下命令: ffmpeg -ss 00:00:05 -t 0 ...
- Linux之文本搜索命令 grep
Linux之文本搜索命令 1. grep命令的使用 命令 说明 grep 文本搜索 grep命令效果图: [使用单引号和双引号效果是一样的] 2. grep命令选项的使用 命令选项 说明 -i 忽略大 ...
最新文章
- yum 安装指定版本php,怎样通过yum安装指定版本的php
- 如何使用 autofs 挂载 NFS 共享
- java发送内嵌图片邮件
- CSS隐藏元素的十四种方法
- android 实例源码解释,Android Handler 原理分析及实例代码
- springboot nacos_springboot集成nacos
- C语言第三节关键字、标识符、注释
- Bailian3670 计算鞍点(POJ NOI0108-05)【矩阵】
- 系统学习深度学习(八)--损失函数
- MyBatis 别名标签 sql的复用
- 算法笔记-二分查找和二分答案
- 服务器虚拟化思维导图,虚拟语气思维导图解析.ppt
- 不重启刷新win7环境变量(注册表)
- 浏览器控制台Network面板简述
- 谷歌中国进入后李开复时代:向总部架构靠拢
- 使用paddlepaddle进行手写数字识别
- 2016年中山市信息学竞赛暨全国信息学联赛成绩表(普及组)
- css3简单实现页面中出现雪花飘落效果
- 干细胞培养的进展和挑战
- 【转载】人体经络图(详细图片、清晰穴位)
热门文章
- 【Windows 逆向】OD 调试器工具 ( OD 调试数据时硬件断点对应的关键代码 | 删除硬件端点恢复运行 )
- 【错误记录】360 加固后的运行错误 ( 加固 SO 动态库时不能对第三方动态库进行加固 )
- 【Android 应用开发】Android屏幕适配解析 - 详解像素,设备独立像素,归一化密度,精确密度及各种资源对应的尺寸密度分辨率适配问题
- 钉钉、阿里云和PaaS平台的整合开发
- OpenCV+Qt+CMake安装+十种踩坑
- mysqlcppconn之ConnectOptionsMap的使用
- Informatica ETL workflow 定时运行功能介绍 scheduler
- javascript实现正整数分数约分
- macOS下加载动态库dylib报code signature invalid错误的解决办法
- 网页瀑布流效果实现的几种方式