linux 文本处理四件套的简单用法
作者:SkyFire
- 2021 年 12 月 29 日
本文字数:5462 字
阅读完需:约 18 分钟
文章只是大致介绍一些常用的用法,还有很多没有提到的,这几个工具在 linux 上的功能是很强大的,最好根据命令的官方文档完整的学习一遍。
find
查找文件或者文件夹
find /usr/include -name "stdio.h"
复制代码
输出:
/usr/include/bits/stdio.h
/usr/include/wine/msvcrt/stdio.h
/usr/include/bsd/stdio.h
/usr/include/c++/9.2.0/tr1/stdio.h
/usr/include/stdio.h
复制代码
使用通配符
find /usr/include -name "*c++"
复制代码
输出:
/usr/include/sigc++-2.0
/usr/include/sigc++-2.0/sigc++
/usr/include/sigc++-2.0/sigc++/sigc++.h
/usr/include/c++
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/stdtr1c++.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/stdc++.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/c++locale.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/c++allocator.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/c++io.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/c++config.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/extc++.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/stdtr1c++.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/stdc++.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/c++locale.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/c++allocator.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/c++io.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/c++config.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/extc++.h
/usr/include/c++/9.2.0/bits/c++0x_warning.h
复制代码
只查找目录
find /usr/include -name "*c++*" -type d
复制代码
输出
/usr/include/sigc++-2.0
/usr/include/sigc++-2.0/sigc++
/usr/include/c++
复制代码
只查找文件
find /usr/include -name "*c++*" -type f
复制代码
输出:
/usr/include/sigc++-2.0/sigc++/sigc++.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/stdtr1c++.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/stdc++.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/c++locale.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/c++allocator.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/c++io.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/c++config.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/extc++.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/stdtr1c++.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/stdc++.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/c++locale.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/c++allocator.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/c++io.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/c++config.h
/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/extc++.h
/usr/include/c++/9.2.0/bits/c++0x_warning.h
复制代码
查找 30 天前修改的文件
find /usr/include -name "*c++*" -mtime +30
复制代码
输出:
/usr/include/sigc++-2.0
/usr/include/sigc++-2.0/sigc++
/usr/include/sigc++-2.0/sigc++/sigc++.h
复制代码
查找今天修改的文件
find . -mtime -1
复制代码
输出:
.
./find_grep_sed_awk.md
复制代码
对找到的每个文件做一些事情
find /usr/include -name "*c++*" -type f -exec cp -r {} ./ \;
复制代码
输出:
'/usr/include/sigc++-2.0/sigc++/sigc++.h' -> './sigc++.h'
'/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/stdtr1c++.h' -> './stdtr1c++.h'
'/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/stdc++.h' -> './stdc++.h'
'/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/c++locale.h' -> './c++locale.h'
'/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/c++allocator.h' -> './c++allocator.h'
'/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/c++io.h' -> './c++io.h'
'/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/c++config.h' -> './c++config.h'
'/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/extc++.h' -> './extc++.h'
'/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/stdtr1c++.h' -> './stdtr1c++.h'
'/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/stdc++.h' -> './stdc++.h'
'/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/c++locale.h' -> './c++locale.h'
'/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/c++allocator.h' -> './c++allocator.h'
'/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/c++io.h' -> './c++io.h'
'/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/c++config.h' -> './c++config.h'
'/usr/include/c++/9.2.0/x86_64-pc-linux-gnu/32/bits/extc++.h' -> './extc++.h'
'/usr/include/c++/9.2.0/bits/c++0x_warning.h' -> './c++0x_warning.h'
复制代码
find . -name "*c++*" -exec rm -v {} \;
复制代码
输出:
已删除 './c++config.h'
已删除 './c++io.h'
已删除 './extc++.h'
已删除 './c++0x_warning.h'
已删除 './stdtr1c++.h'
已删除 './c++allocator.h'
已删除 './c++locale.h'
已删除 './sigc++.h'
已删除 './stdc++.h'
复制代码
查找大小超过 500k 的文件并显示一下行数
find /usr/include -type f -size +500k -exec wc -l {} \;
复制代码
输出:
11857 /usr/include/sqlite3.h
18827 /usr/include/ruby-2.6.0/x86_64-linux/rb_mjit_min_header-2.6.5.h
28987 /usr/include/wine/windows/mshtml.idl
23864 /usr/include/wine/windows/msxml2.h
11856 /usr/include/wine/windows/dwrite_3.h
79293 /usr/include/wine/windows/mshtml.h
16182 /usr/include/wine/windows/shobjidl.h
23255 /usr/include/wine/windows/msxml6.h
19634 /usr/include/epoxy/gl_generated.h
19478 /usr/include/qt/QtOpenGLExtensions/qopenglextensions.h
12264 /usr/include/qt/QtGui/qopenglext.h
8789 /usr/include/qt/QtCore/5.13.1/QtCore/private/qlocale_data_p.h
3533 /usr/include/clang/Basic/DiagnosticSemaKinds.inc
11347 /usr/include/clang/Sema/Sema.h
11177 /usr/include/SDL2/SDL_opengl_glext.h
23686 /usr/include/GL/glew.h
12763 /usr/include/GL/glext.h
11784 /usr/include/dlang/ldc/std/datetime/systime.d
11081 /usr/include/dlang/ldc/std/internal/unicode_tables.d
11784 /usr/include/dlang/dmd/std/datetime/systime.d
11081 /usr/include/dlang/dmd/std/internal/unicode_tables.d
11080 /usr/include/dlang/gdc/std/internal/unicode_tables.d
复制代码
查找指定权限的文件
find /usr/include -type f -perm 755 -exec ls -l {} \;
复制代码
输出:
-rwxr-xr-x 1 root root 18981 4月 21 02:15 /usr/include/http_parser.h
-rwxr-xr-x 1 root root 181547 7月 5 16:28 /usr/include/va/va.h
-rwxr-xr-x 1 root root 56026 7月 5 16:28 /usr/include/va/va_vpp.h
-rwxr-xr-x 1 root root 1966 11月 10 2018 /usr/include/libnethogs.h
复制代码
grep
查找文件中的指定内容
grep "root" /etc/passwd
复制代码
输出:
root:x:0:0::/root:/usr/bin/fish
复制代码
高亮
grep "root" /etc/passwd --color
复制代码
这里看不到,所以就不输出了。
显示行号
grep -n "root" /etc/passwd
复制代码
输出:
1:root:x:0:0::/root:/usr/bin/fish
复制代码
查找正则表达式
grep "sh\$" /etc/passwd
复制代码
输出:
root:x:0:0::/root:/usr/bin/fish
skyfire:x:1000:1001:skyfire:/home/skyfire:/usr/bin/fish
postgres:x:964:964:PostgreSQL user:/var/lib/postgres:/bin/bash
复制代码
反选,除去不满足条件的
grep "sh\$" /etc/passwd | grep -v "bash\$"
复制代码
查找所有以 sh 结尾,但是不以 bash 结尾的行:
输出:
root:x:0:0::/root:/usr/bin/fish
skyfire:x:1000:1001:skyfire:/home/skyfire:/usr/bin/fish
复制代码
统计查找到的次数
grep "sh\$" /etc/passwd -c
复制代码
输出:
3
复制代码
忽略大小写
grep "SH\$" /etc/passwd -i
复制代码
输出:
root:x:0:0::/root:/usr/bin/fish
skyfire:x:1000:1001:skyfire:/home/skyfire:/usr/bin/fish
postgres:x:964:964:PostgreSQL user:/var/lib/postgres:/bin/bash
复制代码
使用正则表达式
输出 ifconfig 命令输出的所有 ip 地址:
ifconfig | egrep "([0-9]{1,3}\.){3}[0-9]{1,3}" --color
复制代码
输出:
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
inet 192.168.42.240 netmask 255.255.255.0 broadcast 192.168.42.255
inet 127.0.0.1 netmask 255.0.0.0
复制代码
awk
awk 脚本的基本结构:
awk 'BEGIN { print "start"} pattern {commands } END {print "end"}' file
复制代码
列出/etc/passwd 文件的第一列,文件使用:分割:
awk -F: '{print $1}' /etc/passwd
复制代码
输出:
root
nobody
dbus
bin
daemon
mail
ftp
http
systemd-journal-remote
systemd-coredump
uuidd
dnsmasq
rpc
avahi
colord
cups
deepin_anything_server
deepin-sound-player
deepin-daemon
deluge
git
lightdm
nm-openconnect
nm-openvpn
ntp
polkitd
rtkit
usbmux
skyfire
systemd-network
systemd-resolve
systemd-timesync
deepin-anything-server
geoclue
lantern
postgres
复制代码
给
hello world
加上双引号
echo hello world | awk 'BEGIN{print "Start"}{print "\""$1"\"" "\""$2"\""}END{print "End"}'
复制代码
输出:
Start
"hello""world"
End
复制代码
给文本加上行号
echo -e "hello\nworld\n666" | awk '{print NR " - " $1}'
复制代码
输出:
1 - hello
2 - world
3 - 666
复制代码
统计文本字段数量
echo hello world 666 | awk '{print "文本字段:" NF}'
复制代码
输出
文本字段:3
复制代码
传递外部变量
echo hello | awk -v SUFFIX="world" '{print $1 "," SUFFIX}'
复制代码
或者:
echo hello | awk '{print $1 "," SUFFIX}' SUFFIX="world"
复制代码
输出
hello,world
复制代码
输出行号小于 5 的行
awk 'NR < 5' /etc/passwd
复制代码
输出:
root:x:0:0::/root:/usr/bin/fish
nobody:x:65534:65534:Nobody:/:/sbin/nologin
dbus:x:81:81:System Message Bus:/:/sbin/nologin
bin:x:1:1::/:/sbin/nologin
复制代码
行号在 3 到 6 之间的行
awk 'NR==3,NR==6' /etc/passwd
复制代码
输出
dbus:x:81:81:System Message Bus:/:/sbin/nologin
bin:x:1:1::/:/sbin/nologin
daemon:x:2:2::/:/sbin/nologin
mail:x:8:12::/var/spool/mail:/sbin/nologin
复制代码
包含 root 的行
awk '/root/' /etc/passwd
复制代码
输出:
root:x:0:0::/root:/usr/bin/fish
复制代码
不包含 root 的行
awk '!/root/' /etc/passwd
复制代码
输出
nobody:x:65534:65534:Nobody:/:/sbin/nologin
dbus:x:81:81:System Message Bus:/:/sbin/nologin
bin:x:1:1::/:/sbin/nologin
daemon:x:2:2::/:/sbin/nologin
mail:x:8:12::/var/spool/mail:/sbin/nologin
ftp:x:14:11::/srv/ftp:/sbin/nologin
http:x:33:33::/srv/http:/sbin/nologin
systemd-journal-remote:x:982:982:systemd Journal Remote:/:/sbin/nologin
systemd-coredump:x:981:981:systemd Core Dumper:/:/sbin/nologin
uuidd:x:68:68::/:/sbin/nologin
dnsmasq:x:980:980:dnsmasq daemon:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
avahi:x:979:979:Avahi mDNS/DNS-SD daemon:/:/sbin/nologin
colord:x:978:978:Color management daemon:/var/lib/colord:/sbin/nologin
cups:x:209:209:cups helper user:/:/sbin/nologin
deepin_anything_server:x:977:977:Deepin Anything Server:/:/sbin/nologin
deepin-sound-player:x:976:976:Deepin Sound Player:/:/sbin/nologin
deepin-daemon:x:975:975:Deepin Daemon:/:/sbin/nologin
deluge:x:974:974:Deluge BitTorrent daemon:/srv/deluge:/sbin/nologin
git:x:973:973:git daemon user:/:/usr/bin/git-shell
lightdm:x:620:620:Light Display Manager:/var/lib/lightdm:/sbin/nologin
nm-openconnect:x:972:972:NetworkManager OpenConnect:/:/sbin/nologin
nm-openvpn:x:971:971:NetworkManager OpenVPN:/:/sbin/nologin
ntp:x:87:87:Network Time Protocol:/var/lib/ntp:/bin/false
polkitd:x:102:102:PolicyKit daemon:/:/sbin/nologin
rtkit:x:133:133:RealtimeKit:/proc:/sbin/nologin
usbmux:x:140:140:usbmux user:/:/sbin/nologin
skyfire:x:1000:1001:skyfire:/home/skyfire:/usr/bin/fish
systemd-network:x:970:970:systemd Network Management:/:/sbin/nologin
systemd-resolve:x:969:969:systemd Resolver:/:/sbin/nologin
systemd-timesync:x:968:968:systemd Time Synchronization:/:/sbin/nologin
deepin-anything-server:x:967:967:Deepin Anthing Server:/:/sbin/nologin
geoclue:x:966:966:Geoinformation service:/var/lib/geoclue:/sbin/nologin
lantern:x:619:619::/var/lib/lantern:/bin/false
postgres:x:964:964:PostgreSQL user:/var/lib/postgres:/bin/bash
复制代码
循环
echo | awk '{for(i=0;i<10;i++){print i}}'
复制代码
输出:
0
1
2
3
4
5
6
7
8
9
复制代码
sed
替换字符串
echo hello world | sed 's/world/skyfire/'
复制代码
输出:
hello skyfire
复制代码
替换全部
echo hello world | sed 's/o/O/g'
复制代码
输出
hellO wOrld
复制代码
删除匹配行
echo -e "hello\nworld\n666" | sed '/h/d'
复制代码
输出
world
666
复制代码
替换正则表达式
echo hello world | sed 's/l\{2\}/LL/g'
复制代码
输出
heLLo world
复制代码
已匹配字符串标记
echo hello world | sed 's/he\(l\+\)o/& \1/g'
复制代码
输出
hello ll world
复制代码
组合多个表达式
echo hello world | sed 's/l/o/g;s/o\+/t/g'
复制代码
输出:
het wtrtd
复制代码
划线
评论
复制
发布于: 12 小时前
版权声明: 本文为 InfoQ 作者【SkyFire】的原创文章。
原文链接:【http://xie.infoq.cn/article/cef4bd5718beac0d05a6335cc】。文章转载请联系作者。
SkyFire
关注
还未添加个人签名 2018.10.13 加入
还未添加个人简介
评论