写点什么

linux 文本处理四件套的简单用法

作者:SkyFire
  • 2021 年 12 月 29 日
  • 本文字数:5462 字

    阅读完需:约 18 分钟

linux文本处理四件套的简单用法

文章只是大致介绍一些常用的用法,还有很多没有提到的,这几个工具在 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.h18827 /usr/include/ruby-2.6.0/x86_64-linux/rb_mjit_min_header-2.6.5.h28987 /usr/include/wine/windows/mshtml.idl23864 /usr/include/wine/windows/msxml2.h11856 /usr/include/wine/windows/dwrite_3.h79293 /usr/include/wine/windows/mshtml.h16182 /usr/include/wine/windows/shobjidl.h23255 /usr/include/wine/windows/msxml6.h19634 /usr/include/epoxy/gl_generated.h19478 /usr/include/qt/QtOpenGLExtensions/qopenglextensions.h12264 /usr/include/qt/QtGui/qopenglext.h8789 /usr/include/qt/QtCore/5.13.1/QtCore/private/qlocale_data_p.h3533 /usr/include/clang/Basic/DiagnosticSemaKinds.inc11347 /usr/include/clang/Sema/Sema.h11177 /usr/include/SDL2/SDL_opengl_glext.h23686 /usr/include/GL/glew.h12763 /usr/include/GL/glext.h11784 /usr/include/dlang/ldc/std/datetime/systime.d11081 /usr/include/dlang/ldc/std/internal/unicode_tables.d11784 /usr/include/dlang/dmd/std/datetime/systime.d11081 /usr/include/dlang/dmd/std/internal/unicode_tables.d11080 /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/fishskyfire:x:1000:1001:skyfire:/home/skyfire:/usr/bin/fishpostgres: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/fishskyfire: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/fishskyfire:x:1000:1001:skyfire:/home/skyfire:/usr/bin/fishpostgres: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
复制代码


输出:


rootnobodydbusbindaemonmailftphttpsystemd-journal-remotesystemd-coredumpuuidddnsmasqrpcavahicolordcupsdeepin_anything_serverdeepin-sound-playerdeepin-daemondelugegitlightdmnm-openconnectnm-openvpnntppolkitdrtkitusbmuxskyfiresystemd-networksystemd-resolvesystemd-timesyncdeepin-anything-servergeocluelanternpostgres
复制代码


  • 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 - hello2 - world3 - 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/fishnobody:x:65534:65534:Nobody:/:/sbin/nologindbus:x:81:81:System Message Bus:/:/sbin/nologinbin:x:1:1::/:/sbin/nologin
复制代码


  • 行号在 3 到 6 之间的行


awk 'NR==3,NR==6' /etc/passwd
复制代码


输出


dbus:x:81:81:System Message Bus:/:/sbin/nologinbin:x:1:1::/:/sbin/nologindaemon:x:2:2::/:/sbin/nologinmail: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/nologindbus:x:81:81:System Message Bus:/:/sbin/nologinbin:x:1:1::/:/sbin/nologindaemon:x:2:2::/:/sbin/nologinmail:x:8:12::/var/spool/mail:/sbin/nologinftp:x:14:11::/srv/ftp:/sbin/nologinhttp:x:33:33::/srv/http:/sbin/nologinsystemd-journal-remote:x:982:982:systemd Journal Remote:/:/sbin/nologinsystemd-coredump:x:981:981:systemd Core Dumper:/:/sbin/nologinuuidd:x:68:68::/:/sbin/nologindnsmasq:x:980:980:dnsmasq daemon:/:/sbin/nologinrpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologinavahi:x:979:979:Avahi mDNS/DNS-SD daemon:/:/sbin/nologincolord:x:978:978:Color management daemon:/var/lib/colord:/sbin/nologincups:x:209:209:cups helper user:/:/sbin/nologindeepin_anything_server:x:977:977:Deepin Anything Server:/:/sbin/nologindeepin-sound-player:x:976:976:Deepin Sound Player:/:/sbin/nologindeepin-daemon:x:975:975:Deepin Daemon:/:/sbin/nologindeluge:x:974:974:Deluge BitTorrent daemon:/srv/deluge:/sbin/nologingit:x:973:973:git daemon user:/:/usr/bin/git-shelllightdm:x:620:620:Light Display Manager:/var/lib/lightdm:/sbin/nologinnm-openconnect:x:972:972:NetworkManager OpenConnect:/:/sbin/nologinnm-openvpn:x:971:971:NetworkManager OpenVPN:/:/sbin/nologinntp:x:87:87:Network Time Protocol:/var/lib/ntp:/bin/falsepolkitd:x:102:102:PolicyKit daemon:/:/sbin/nologinrtkit:x:133:133:RealtimeKit:/proc:/sbin/nologinusbmux:x:140:140:usbmux user:/:/sbin/nologinskyfire:x:1000:1001:skyfire:/home/skyfire:/usr/bin/fishsystemd-network:x:970:970:systemd Network Management:/:/sbin/nologinsystemd-resolve:x:969:969:systemd Resolver:/:/sbin/nologinsystemd-timesync:x:968:968:systemd Time Synchronization:/:/sbin/nologindeepin-anything-server:x:967:967:Deepin Anthing Server:/:/sbin/nologingeoclue:x:966:966:Geoinformation service:/var/lib/geoclue:/sbin/nologinlantern:x:619:619::/var/lib/lantern:/bin/falsepostgres:x:964:964:PostgreSQL user:/var/lib/postgres:/bin/bash
复制代码


  • 循环


echo | awk '{for(i=0;i<10;i++){print i}}'
复制代码


输出:


0123456789
复制代码

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' 
复制代码


输出


world666
复制代码


  • 替换正则表达式


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 小时前
用户头像

SkyFire

关注

还未添加个人签名 2018.10.13 加入

还未添加个人简介

评论

发布
暂无评论
linux文本处理四件套的简单用法