grep进阶与sed行编辑器
上一篇()我们介绍了grep的一些基本的使用方法,其它grep还是一些比较高级的用法,让我们一起来看看。
先来看一下下面例子的运行结果:
相同的命令输出的结果有点不一样,对,这里就是利用“alias”去设置命令的别名,语法格式为“alias=”command””即可,可能通过直接在命令行输入“alias”查看当前系统所有的命令别名
[root@localhost ~]# alias alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep -n --color=auto' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
以上我们看到命令“cp”也设置了“-i 代表交互式”参数,另外,我们在平时的工作当中,也可以对经常使用到命令“history”设置一个简短的别名,接下来我们进入正题。
[root@localhost ~]# grep -A -B '搜索字串' 文件名“-A#”:After之意,代表除了列出该行外,后续的#(代表数字)行也列出来“-B#”:Before之意,代表除了列出该行外,前面的#(代表数字)行也列出来例如:查找包含“daemon”字串,并输出匹配行的前4行和后3行[root@localhost tmp]# cat /etc/passwd | grep -A3 -B4 'daemon'34-gdm:x:42:42::/var/lib/gdm:/sbin/nologin 35-gnome-initial-setup:x:992:990::/run/gnome-initial-setup/:/sbin/nologin 36-postfix:x:89:89::/var/spool/postfix:/sbin/nologin 37-sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 38:avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin 39-tcpdump:x:72:72::/:/sbin/nologin 40-jimjimlv:x:1000:1000:Linux_basic:/home/jimjimlv:/bin/bash 41-Martin:x:1007:1008::/home/Martin:/sbin/nologin
特殊符号的意义与实例
“.”:匹配任意字节的字符,
[root@localhost tmp]# grep '^#.' /etc/fstab 3:# /etc/fstab 4:# Created by anaconda on Fri Dec 18 15:12:53 2015 6:# Accessible filesystems, by reference, are maintained under '/dev/disk' 7:# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info”\“:跳脱字符,将特殊符号的特殊意义去除[root@localhost tmp]# grep -n \' /etc/fstab 6:# Accessible filesystems, by reference, are maintained under '/dev/disk'
Sed,Stream EDitor,行编辑器
语法格式:sed [option]... '编辑命令' inputfile..
常用选项与参数
-n:不输出模式中的内容至屏幕;
-e: 多点编辑; -f /PATH/TO/SCRIPT_FILE: 从指定文件中读取编辑脚本; -r: 支持使用扩展正则表达式; -i: 原处编辑;常用编辑命令
a: 新增内容,可以接字串,并出现在新一一行 d: 删除,通常后面不接任何东西 p: 显示模式空间中的内容,通常与参数sed –n 一起运行 a \text:在行后面追加文本;支持使用\n实现多行追加; i \text:在行前面插入文本;支持使用\n实现多行插入; c \text:替换行为单行或多行文本; w /path/to/somefile: 保存模式空间匹配到的行至指定文件中; r /path/from/somefile:读取指定文件的文本流至模式空间中匹配到的行的行后; =: 为模式空间中的行打印行号; !: 取反条件; s///:支持使用其它分隔符,s@@@,s###; 替换标记: g: 行内全局替换; p: 显示替换成功的行; w /PATH/TO/SOMEFILE:将替换成功的结果保存至指定文件中;
例1:将设备挂载信息”/data /dev/sdb1 ext4 defaults 0 0 “写入/etc/fstab
[root@Jimjimlv ~]# sed '15a /data /dev/sdb1 ext4 defaults 0 0 ' /etc/fstab# # /etc/fstab # Created by anaconda on Wed Jan 20 00:57:58 2016 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/vg_jimjimlv-lv_root / ext4 defaults 1 1 UUID=16071ed0-f4ea-4651-96ac-e40d2a158e01 /boot ext4 defaults 1 2 /dev/mapper/vg_jimjimlv-lv_swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 /data /dev/sdb1 ext4 defaults 0 0备注:以上只是将修改的结果输出屏幕,如果想修改源文件的内容,可以直接使用参数”-i“,不过在使用它之前,要确认过你确实是要修改源文件的内容,以免造成不必要的麻烦。
例2:删除文件/etc/fstab里面第3~6行的内容
[root@Jimjimlv ~]# cat -n /etc/passwd | sed '3,6d' 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8 halt:x:7:0:halt:/sbin:/sbin/halt
例3:将/etc/fstab文件的第3~6行替换为”Line 3~6 replaced“
[root@Jimjimlv ~]# cat -n /etc/passwd | sed '3,6c Line from 2 to 6 being replaced' 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin Line from 2 to 6 being replaced 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8 halt:x:7:0:halt:/sbin:/sbin/halt
例4:前面我们结合head 和 tail命令去获取想要的结果的某个范围内的数据,在这里,可以直接利用sed命令的”-p”参数去实现,如下:
[root@Jimjimlv ~]# cat -n /etc/passwd | head -n 7 | tail -n 5 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin 4 adm:x:3:4:adm:/var/adm:/sbin/nologin 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown [root@Jimjimlv ~]# cat -n /etc/passwd | sed -n '3,7p' 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin 4 adm:x:3:4:adm:/var/adm:/sbin/nologin 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
例5:将文件sed.txt开头的单词”THIS“替换为”this“
[root@Jimjimlv sed]# sed 's/^THIS/this/g' sed.txt this IS THE TESTING FOR SED this IS THE TESTING FOR SED this IS THE TESTING FOR SED
例6:删除/boot/grub/grub.conf文件中所有以空白开头的行行首的空白字符
[root@Jimjimlv sed]# cat /etc/grub.conf # grub.conf generated by anaconda # # Note that you do not have to rerun grub after making changes to this file # NOTICE: You have a /boot partition. This means that # all kernel and initrd paths are relative to /boot/, eg. # root (hd0,0) # kernel /vmlinuz-version ro root=/dev/mapper/vg_jimjimlv-lv_root # initrd /initrd-[generic-]version.img #boot=/dev/sda default=0 timeout=5 splashp_w_picpath=(hd0,0)/grub/splash.xpm.gz hiddenmenu title CentOS 6 (2.6.32-573.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-573.el6.x86_64 ro root=/dev/mapper/vg_jimjimlv-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 rd_LVM_LV=vg_jimjimlv/lv_swap crashkernel=auto rd_LVM_LV=vg_jimjimlv/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet initrd /initramfs-2.6.32-573.el6.x86_64.img [root@Jimjimlv sed]# sed 's@^[[:space:]]\+@@' /etc/grub.conf # grub.conf generated by anaconda # # Note that you do not have to rerun grub after making changes to this file # NOTICE: You have a /boot partition. This means that # all kernel and initrd paths are relative to /boot/, eg. # root (hd0,0) # kernel /vmlinuz-version ro root=/dev/mapper/vg_jimjimlv-lv_root # initrd /initrd-[generic-]version.img #boot=/dev/sda default=0 timeout=5 splashp_w_picpath=(hd0,0)/grub/splash.xpm.gz hiddenmenu title CentOS 6 (2.6.32-573.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-573.el6.x86_64 ro root=/dev/mapper/vg_jimjimlv-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 rd_LVM_LV=vg_jimjimlv/lv_swap crashkernel=auto rd_LVM_LV=vg_jimjimlv/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet initrd /initramfs-2.6.32-573.el6.x86_64.img
例7: 获取网关地址
[root@Jimjimlv sed]# /sbin/ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:0C:29:0A:E0:0D inet addr:10.100.100.133 Bcast:10.100.100.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe0a:e00d/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:163885 errors:1 dropped:51 overruns:0 frame:0 TX packets:3868 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:10655166 (10.1 MiB) TX bytes:349013 (340.8 KiB) Interrupt:19 Base address:0x2000[root@Jimjimlv sed]# /sbin/ifconfig eth0 | grep 'inet addr' inet addr:10.100.100.133 Bcast:10.100.100.255 Mask:255.255.255.0 [root@Jimjimlv sed]# /sbin/ifconfig eth0 | grep 'inet addr' | \ > sed 's/^.*Mask://g' 255.255.255.0
例8:获取IP地址
[root@Jimjimlv sed]# /sbin/ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:0C:29:0A:E0:0D inet addr:10.100.100.133 Bcast:10.100.100.255 Mask:255.255.255.0 inet addr:10.100.100.133 Bcast:10.100.100.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe0a:e00d/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:167933 errors:1 dropped:51 overruns:0 frame:0 TX packets:4239 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:10931502 (10.4 MiB) TX bytes:388761 (379.6 KiB) Interrupt:19 Base address:0x2000[root@Jimjimlv sed]# /sbin/ifconfig eth0 | sed -n '2p' inet addr:10.100.100.133 Bcast:10.100.100.255 Mask:255.255.255.0 [root@Jimjimlv sed]# /sbin/ifconfig eth0 | sed -n '2p' | \ > sed 's/^.*addr://g' 10.100.100.133 Bcast:10.100.100.255 Mask:255.255.255.0 [root@Jimjimlv sed]# /sbin/ifconfig eth0 | sed -n '2p' | sed 's/^.*addr://g' | \ > sed 's/Bcast.*$//g' 10.100.100.133
例9:获取子网码
[root@Jimjimlv sed]# /sbin/ifconfig eth0 | sed -n '2p' inet addr:10.100.100.133 Bcast:10.100.100.255 Mask:255.255.255.0 [root@Jimjimlv sed]# /sbin/ifconfig eth0 | sed -n '2p' | sed 's/^.*Bcast://g' | \ > sed 's/Mask.*$//g' 10.100.100.255
一些高级编辑命令: h: 把模式空间中的内容覆盖至保持空间中; H:把模式空间中的内容追加至保持空间中; g: 从保持空间取出数据覆盖至模式空间; G:从保持空间取出内容追加至模式空间; x: 把模式空间中的内容与保持空间中的内容进行互换; n: 读取匹配到的行的下一行至模式空间; N:追加匹配到的行的下一行至模式空间; d: 删除模式空间中的行; D:删除多行模式空间中的所有行;
例1:显示偶数行
[root@Jimjimlv sed]# cat 1.txt 1 2 3 4 5 6 7 8 [root@Jimjimlv sed]# cat 1.txt | sed -n 'n;p' 2 4 6 8
例2:显示奇数行
[root@Jimjimlv sed]# cat 1.txt | sed 'n;d' 1 3 5 7
例3:显示文件最后一行
[root@Jimjimlv sed]# cat 1.txt | sed '$!d' 8
例4:逆向显示文件内容
[root@Jimjimlv sed]# cat 1.txt | sed '1!G;h;$!d' 8 7 6 5 4 3 2 1
参考文献: