grep進階與sed行編輯器正則表達式
上一篇(http://nearlv.blog.51cto.com/2432295/1729198)咱們介紹了grep的一些基本的使用方法,其它grep仍是一些比較高級的用法,讓咱們一塊兒來看看。bash
先來看一下下面例子的運行結果:app
相同的命令輸出的結果有點不同,對,這裏就是利用「alias」去設置命令的別名,語法格式爲「alias=」command」」便可,可能經過直接在命令行輸入「alias」查看當前系統全部的命令別名ssh
[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」設置一個簡短的別名,接下來咱們進入正題。tcp
[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
特殊符號的意義與實例編輯器
「.」:匹配任意字節的字符,ide
[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,行編輯器
post
語法格式:sed [option]... '編輯命令' inputfile..ui
經常使用選項與參數this
-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
參考文獻: