
文件和字符处理命令
1.sort排序
很多情况下需要对无序的数据进行排序, 这时就需要用到
sort排序了。sort [OPTION]... [FILE]... -r:倒序 -n:按数字排序 -t:指定分隔符(默认空格) -k:指定第几列, 指定几列几字符(指定1,1 3.1,3.3) //演示soft的用法, 首先创建一个文件: [root@x86cloud ~]# cat >> sort.txt <<EOF b:3 c:2 a:4 e:5 d:1 f:11 EOF //下面对输出的内容进行排序: [root@x86cloud ~]# sort sort.txt a:4 b:3 c:2 d:1 e:5 f:11 //可观察到, sort文件具有一个特点, 第一个字符是字母, 第三个字符是数字, 中间是用冒号隔开。 //这样我们可以使用-t指定分隔符, 使用-k指定用于排序的列了。 [root@x86cloud ~]# sort -t ":" -k2 sort.txt d:1 f:11 //第二行为什么是11?不应该按照顺序排列? c:2 b:3 a:4 e:5 //按照排序的方式, 只会看到第一个字符,11的第一个字符是1, 按照字符来排序确实比2小。 //如果想要按照数字的方式进行排序, 需要使用 -n参数。 [root@x86cloud ~]# sort -t ":" -n -k2 p.txt d:1 c:2 b:3 a:4 e:5 f:11
2.uniq删除重复内容
如果文件中有多行完全相同的内容, 当前是希望能删除重复的行,同时还可以统计出完全相同的行出现的总次数, 那么就可以使用
uniq命令解决这个问题(但是必须配合sort使用)。uniq [OPTION]... [INPUT [OUTPUT]] -c 计算重复的行 //演示uniq的用法, 首先创建一个文件: [root@x86cloud ~]# cat uniq.txt abc 123 abc 123 //uniq需要和sort一起使用, 先使用sort排序, 让重复内容连续在一起 [root@x86cloud ~]# cat uniq.txt |sort 123 123 abc abc //使用uniq去除相邻重复的行 [root@x86cloud ~]# cat uniq.txt |sort|uniq 123 abc //使用-c参数, 能统计出文件中每行内容重复的次数 [root@x86cloud ~]# cat uniq.txt |sort|uniq -c 2 123 2 abc
3.cut命令截取某一个字段
cut OPTION... [FILE]... -d:指定分隔符 -f:数字,取第几列 –f3,6三列和6列 -c:按字符取(空格也算) //测试数据, 过滤出文件里 x86cloud以及552408925 #echo “Im x86cloud, is QQ 552408925” >x86cloud.com //实践思路 # sed 's#,# #g' x86cloud.txt | cut -d " " -f2,6 # sed 's#,# #g' x86cloud.txt | awk -F " " '{print $2 " " $5}' # awk -F '[, ]+' '{print $2,$5}' x86cloud.txt # awk '{print $2 $5}' x86cloud.txt | awk -F "," '{print $1" " $2}' # awk -F "[, ]" '{print $2 " " $6}' x86cloud.txt 此类问题是运维工作中最常见的问题。可以演变成分析日志,查看TCP各个状态连接数,查看单IP连接数排名等 [root@student tmp]# cat web.log http://www.x86cloud.com/index.html http://www.x86cloud.com/1.html http://post.x86cloud.com/index.html http://mp3.x86cloud.com/index.html http://www.x86cloud.com/3.html http://post.x86cloud.com/2.html [root@student tmp]# awk -F '/' '{print $3}' web.log|sort -rn|uniq –c 3 www.x86cloud.comx86cloud.com 2 post.x86cloud.comx86cloud.com 1 mp3.x86cloud.comx86cloud.com [root@student tmp]# cut -d / -f3 web.log|sort -rn|uniq –c 3 www.x86cloud.comx86cloud.com 2 post.x86cloud.comx86cloud.com 1 mp3.x86cloud.comx86cloud.com
4.使用wc统计
wc - print newline, word, and byte counts for each file显示文件的行、单词与字节统计信息 wc [OPTION]... [FILE]... -l:显示文件行数 -c:显示文件字节 -w:显示文件单词 //统计fstab有多少行 [root@student ~]# wc -l /etc/fstab 15 /etc/fstab 显示/etc/services 文件行号 [root@student ~]# wc -l /etc/services 10774 /etc/services //扩展方法 [root@student ~]# grep -n "." /etc/services | tail -1 [root@student ~]# awk '{print NR $0}' /etc/services | tail -1 [root@student ~]# cat -n /etc/services | tail -1 [root@student ~]# sed '=' /etc/services | tail -2
5.删除7天之前的日志
mkdir -p /app/logs cd /app/logs for time in {01..20};do date -s "201805$time"; touch access_www_$(date +%F).log ;done date -s "20180520"找出7天之前的文件并删除三种方法
方法1find -type f -name "*.log" -mtime +7 |xargs ls -l方法2
ls -l .find -type f -name "*.log" -mtime +7.方法3
find -type f -name "*.log" -mtime +7 -exec ls -l {} \;
6.把/cloud目录及其子目录下所有以扩展名 .sh结尾的文件中文件包含cloud的字符串全部替换为opsxdev
1.替换一个文件的内容 (t.sh)
[root@cloudedu-48 cloud]# cat t.sh cloud [root@cloudedu-48 cloud]# #sed 's#找谁#替换成什么#g' t.sh [root@cloudedu-48 cloud]# sed 's#cloud#opsxdev#g' t.sh opsxdev [root@cloudedu-48 cloud]# cat t.sh cloud [root@cloudedu-48 cloud]# sed -i 's#cloud#opsxdev#g' t.sh [root@cloudedu-48 cloud]# cat t.sh opsxdev2.find + sed 写法
[root@cloudedu-48 cloud]# find /cloud/ -type f -name "*.sh" /cloud/test.sh /cloud/t.sh /cloud/test/del.sh [root@cloudedu-48 cloud]# find /cloud/ -type f -name "*.sh" |xargs sed 's#cloud#opsxdev#g' opsxdev opsxdev opsxdev [root@cloudedu-48 cloud]# find /cloud/ -type f -name "*.sh" |xargs sed 's#cloud#opsxdev#g' -i [root@cloudedu-48 cloud]# find /cloud/ -type f -name "*.sh" |xargs cat opsxdev opsxdev opsxdev方法1
find /cloud/ -type f -name "*.sh" |xargs sed 's#cloud#opsxdev#g'方法2
$( ) == `xxxx`(反引号) 优先执行里面的命令 which mkdir -- /bin/mkdir ls -l $(which mkdir)存放which mkdir 命令结果[root@cloudedu-48 cloud]# ls -l $(which mkdir) -rwxr-xr-x. 1 root root 50056 Mar 23 2017 /bin/mkdir使用语句如下
sed 's#cloud#opsxdev#g' find命令找出来的文件 sed 's#cloud#opsxdev#g' $(find /cloud/ -type f -name "*.sh")
7.请给出输出test.txt文件内容时,不包含cloud字符串的命令。
命令的不包含参数
创建测试文件:mkdir -p /data cat >/data/test.txt<<EOF test liyao cloud EOF方法1
[root@cloudedu-48 data]# grep "cloud" test.txt cloud [root@cloudedu-48 data]# #过滤 [root@cloudedu-48 data]# grep -v "cloud" test.txt test liyao方法2
[root@cloudedu-48 data]# head test.txt test liyao cloud [root@cloudedu-48 data]# head -n2 test.txt test liyao [root@cloudedu-48 data]# head -2 test.txt test liyao方法3
[root@cloudedu-48 data]# sed '/cloud/d' test.txt test liyao --d (delete) 删除行,包含cloud的行删除掉方法4
[root@cloudedu-48 data]# awk '/cloud/' test.txt cloud [root@cloudedu-48 data]# awk '!/cloud/' test.txt test liyao
8.只查看ett.txt文件(共100行)内第20到第30行的内容
seq ==sequence --生成数列
[root@cloudedu-48 data]# seq 40 >ett.txt [root@cloudedu-48 data]# cat ett.txt方法1
[root@cloudedu-48 data]# sed -n '20p' ett.txt 20 [root@cloudedu-48 data]# sed -n '20,30p' ett.txt 20 21 22 23 24 25 26 27 28 29 30方法2
[root@cloudedu-48 data]# awk 'NR==20,NR==30' ett.txt 20 21 22 23 24 25 26 27 28 29 30方法3
[root@cloudedu-48 data]# head -30 ett.txt |tail -11 20 21 22 23 24 25 26 27 28 29 30
9.统计文件ip地址出现的次数
1.awk ‘{print $1}’ access.log | sort |uniq -c |sort -h |tail
2.grep