shell工具-grep
在 Linux 的世界里,grep(Global Regular Expression Print)就像是一把瑞士军刀,是处理日志、分析代码和排查故障的必备神器。它的核心逻辑很简单:在文件中搜索特定的模式(Pattern),并将匹配的行打印出来。
多目录递归搜索 (-r)
1 2 3 4
| grep -r "get_user_balance" ./src/
grep -r "config" . --exclude-dir=node_modules
|
基础搜索与定位,这是最常见的用法,用于在海量文本中快速找到目标。
1 2 3 4 5 6 7 8 9
| grep "error" sys.log
grep -i "user_id" app.log
grep -w "lib" config.txt
grep -n "NullPointerException" debug.log vim +100 debug.log
|
生产排障:查看上下文 (-A, -B, -C),这一招在分析 Java 堆栈轨迹(Stack Trace)时非常管用
1 2 3 4 5 6
| grep -A 5 "Exception" app.log
grep -B 5 "Exception" app.log
grep -C 5 "Exception" app.log
|
统计与过滤 (-v, -c, -l)
1 2 3 4 5 6 7
| cat /etc/redis/redis-origin.conf |grep -v "#" |grep -v "^$" > /etc/redis/redis.conf ps -ef | grep "java" | grep -v "grep"
grep -c "ERROR" access.log
grep -l "target_config" /etc/*.conf
|
正则表达式增强 (-E)
1 2 3 4
| grep -E "error|critical|fatal" sys.log
grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" access.log
|
注意事项:
- 性能问题: 在超大文件(数 GB 级别)上频繁 grep 可能会导致磁盘 I/O 飙升,建议配合 sed 或 awk 分段处理,或者先用 tail 或 grep 缩小范围。
- 特殊字符: 如果搜索的内容包含
*、[、$ 等,记得用单引号 ' ' 包裹。
shell工具-cut
cut的工作就是“剪”,具体的说就是在文件中负责剪切数据用的。cut命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段输出。注意cut操作文件内容并没有改变原文件内容,除非你使用重定向存储输出。
基本用法
cut [选项参数] filename
选项参数说明:
- -f 列号,提取第几列,
-f n-表示第n列及以后的所有列,-f -n表示第n列及以前的所有列;
- -d 分隔符,按照指定分隔符分割列(默认分割符是制表符)
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| $ touch cut.txt $ vim cut.txt bei shen jing zhen wo wo lai lai le le
[root@host-test test]# cut -d " " -f 1 cut.txt bei jing wo lai le
[root@host-test test]# cut -d " " -f 2,3 cut.txt shen zhen wo lai le
[root@host-test test]# cat cut.txt | grep jing | cut -d " " -f 1 jing
[root@host-test test]# echo $PATH | cut -d ":" -f 2- /usr/local/bin:/usr/sbin:/usr/bin:/opt/java/bin:/opt/nodejs/bin:/root/bin
[root@host-test test]# ifconfig enp0s8 | grep inet.*netmask | cut -d " " -f 10 192.168.56.101
|
shell工具-sed
sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”;接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕;接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。
基本用法
sed [选项参数] ‘command’ filename
选项参数说明:
命令功能描述:
- a:新增,a的后面可以接字串,在下一行出现
- d:删除
- s:查找并替换
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| $ touch sed.txt $ vim sed.txt bei shen jing zhen wo wo lai lai le le
[root@host-test test]# sed '2a mei nv' sed.txt bei shen jing zhen mei nv wo wo lai lai le le
[root@host-test test]# sed '/wo/d' sed.txt bei shen jing zhen lai lai le le
[root@host-test test]# sed 's/wo/ni/g' sed.txt bei shen jing zhen ni ni lai lai le le
[root@host-test test]# sed -e '2d' -e 's/wo/ni/g' sed.txt bei shen ni ni lai lai le le
|
shell工具-awk
- awk是行处理器: 相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓慢的问题,通常用来格式化文本信息
- awk处理过程: 依次对每一行进行处理,然后输出
基本用法
1
| awk [选项参数] 'BEGIN{} pattern1{action1} pattern2{action2} ... END{}' filename
|
注意:
- pattern:表示AWK在数据中查找的内容,就是匹配模式
- action:在找到匹配内容时所执行的一系列命令
选项参数说明:
- -F:指定输入文件折分隔符
- -f:调用脚本
- -v:赋值一个用户定义变量
awk的内置变量:
FILENAME:文件名
NR:已读的记录数
NF:浏览记录的域的个数(切割后,列的个数)
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| $ sudo cp /etc/passwd ./
$ awk -F : '/^root/{print $7}' passwd /bin/bash
$ awk -F : '/^root/{print $1","$7}' passwd root,/bin/bash
$ awk -F : 'BEGIN{print "user,shell"} {print $1","$7} END{print "xiaojuan,/bin/zhenhaokan"}' passwd user,shell root,/bin/bash bin,/sbin/nologin daemon,/sbin/nologin ... tomcat,/bin/bash hexo,/bin/bash xiaojuan,/bin/zhenhaokan
$ awk -F : -v i=1 '{print $3+i}' passwd 1 2 3 ... 1001 1002
$ awk -F : '{print "filename:" FILENAME ",linenum:" NR ",columns:" NF}' passwd filename:passwd,linenum:1,columns:7 filename:passwd,linenum:2,columns:7 filename:passwd,linenum:3,columns:7 ... filename:passwd,linenum:20,columns:7 filename:passwd,linenum:21,columns:7
$ ifconfig enp0s8 enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.56.101 netmask 255.255.255.0 broadcast 192.168.56.255 inet6 fe80::6163:e383:f5e:1c85 prefixlen 64 scopeid 0x20<link> ether ff:ff:27:8f:b1:a1 txqueuelen 1000 (Ethernet) RX packets 47657 bytes 26156068 (24.9 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 26602 bytes 9689205 (9.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 $ ifconfig enp0s8 | grep inet.*netmask | awk -F " " '{print $2}' 192.168.56.101
awk '/^$/{print NR}' sed.txt 5
|
shell工具-sort
sort命令是在Linux里非常有用,它将文件进行排序,并将排序结果标准输出。
基本语法
选项:
- -n:依照数值的大小排序
- -r:以相反的顺序来排序
- -t:设置排序时所用的分隔字符
- -k:指定需要排序的列
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| $ touch sort.txt $ vim sort.txt bb:40:5.4 bd:20:4.2 xz:50:2.3 cls:10:3.5 ss:30:1.6
$ sort -t : -nrk 3 sort.txt bb:40:5.4 bd:20:4.2 cls:10:3.5 xz:50:2.3 ss:30:1.6
|
测试示例
示例1:查询file1中空行所在的行号
1
| $ awk '/^$/{print NR}' sed.txt
|
示例2:有文件chengji.txt内容如下
张三 40
李四 50
王五 60
使用Linux命令计算第二列的和并输出:
1
| $ cat chengji.txt | awk -F " " '{sum+=$2} END{print sum}'
|
示例3:对文本中无序的一列数字排序并求和
1 2 3 4 5 6
| sort -n test.txt | awk '{sum+=$0; print $0} END{print "sum="sum}' 1 2 5 8 sum=16
|
文件编码(iconv)
1 2
| iconv -f UTF-8 -c -t GBK src-file.csv > target-file.csv
|
文件解压和压缩
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
tar -cvf xxx.tar file1 file2 tar -xvf xxx.tar -C /xxx/path
tar -zcvf xxx.tar.gz file1 file2 tar -zxvf xxx.tar.gz -C /xxx/path
tar -jcvf xxx.tar.bz2 file1 file2 tar -jxvf xxx.tar.gz -C /xxx/path
gzip -d examples.gz examples gunzip examples.gz
zip -r examples.zip examples (examples可为目录) unzip examples.zip
rar -a examples.rar examples rar -x examples.rar
|
标准输出和错误输出
1
| $ nohup xxx/xxx.sh > mystd.out 2> myerr.ouct &
|