Linux文本处理三剑客——sed

2018-05-31  本文已影响0人  Hye_Lau

文本处理三工具:grep,sed,awk

sed命令

1.sed命令概述

2.sed命令

命令格式

sed [OPTION]... 'script'  [input-file]...
script:地址定界编辑命令

常用选项

-n:不输出模式空间中的内容至屏幕;
-e:直接在命令行上进行sed的动作编辑;
-f :直接将sed的动作写在一个文件内,-f filename则可以执行filename内的sed动作;
-r:支持使用扩展正则表达式;
-i:直接编辑源文件;(危险操作)

地址定界

 #:指定行;
/pattren/:被此模式所匹配到的每一行;
#,#:
#,+#:
#, /part1/
/part1/,/part2/
$:最后一行
1~2:所有奇数行
2~2:所有偶数行

编辑命令

d:删除
p:显示模式空间中的内容;
a \text:在行后面追加文本“text”,支持使用\n实现多行插入;
i \text:在行前面插入文本“text”,支持使用\n实现多行插入;(危险操作)
c \text:把匹配到的行替换为此处指定的文本“text”;
w /PATH/TO/SOMEFILE:保持模式空间匹配到的行至指定的文件中;
r  /PATH/TO/SOMEFILE:读取指定文件的内容至当前文件被模式匹配到的行处;实现文件合并;
=:为模式匹配到的行打印行号;
!:条件取反;
s///:查找替换,其分隔符可自行指定,常用的有s@@@,s###等;

替换标记

g:全局替换;
w /PATH/TO/SOMEFILE:将替换成功的结果保存至指定的文件中;
p:显示替换成功的行;

3.sed功能演示

新增/删除

[root@localhost ~]# nl /etc/passwd | sed  -e '2a test add'
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
test add
......
[root@localhost ~]# nl /etc/passwd | sed -e '2,5d'
     1  root:x:0:0:root:/root:/bin/bash
     6  sync:x:5:0:sync:/sbin:/bin/sync
     7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
......

以行为单位的替换与显示功能

[root@localhost ~]# nl /etc/passwd | sed '2,5c No 2-5 number'
     1  root:x:0:0:root:/root:/bin/bash
No 2-5 number
     6  sync:x:5:0:sync:/sbin:/bin/sync
//命令中加-n,否则5~7行会重复输出
[root@localhost ~]# nl /etc/passwd | sed -n '5,7p'
     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
部分数据的查找并替换功能
sed 's/要被替换的字符串/新的字符串/g'
//查询ip
[root@localhost ~]# /sbin/ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:48:5F:02  
          inet addr:192.168.0.10  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe48:5f02/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1761880 errors:4 dropped:4 overruns:0 frame:0
          TX packets:85356 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:140153541 (133.6 MiB)  TX bytes:6807963 (6.4 MiB)
          Interrupt:19 Base address:0x2000 
//利用关键字配合grep选取出关键字的一行数据
[root@localhost ~]# /sbin/ifconfig eth0 | grep 'inet addr'
          inet addr:192.168.0.10  Bcast:192.168.0.255  Mask:255.255.255.0
//将ip前面的部分予以删除
[root@localhost ~]# /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g'
192.168.0.10  Bcast:192.168.0.255  Mask:255.255.255.0
//将ip后面的部分予以删除
[root@localhost ~]# /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g'
192.168.0.10  
//先用grep将关键字MAN所在的行取出来
[root@localhost ~]# cat /etc/man.config | grep 'MAN'
# when MANPATH contains an empty substring), to find out where the cat
# MANBIN        pathname
# MANPATH       manpath_element [corresponding_catdir]
# MANPATH_MAP       path_element    manpath_element
# MANBIN        /usr/local/bin/man
# Every automatically generated MANPATH includes these fields
MANPATH /usr/man
......
//删除掉批注之后的数据
[root@localhost ~]# cat /etc/man.config | grep 'MAN' | sed 's/#.*$//g'






MANPATH /usr/man
//原本批注的数据都变成空白行
......
//删除掉空白行
[root@localhost ~]# cat /etc/man.config | grep 'MAN' | sed 's/#.*$//g' | sed '/^$/d'
MANPATH /usr/man
MANPATH /usr/share/man
MANPATH /usr/local/man

练习

//查看/etc/grub.conf文件
[root@localhost ~]# cat /tmp/grub.conf 
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32-279.el6.i686)
    root (hd0,0)
    kernel /vmlinuz-2.6.32-279.el6.i686 ro root=UUID=e31be215-2c21-45f2-a43f-1a9c6bc383f2 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
    initrd /initramfs-2.6.32-279.el6.i686.img
//删除行首的空白字符
[root@localhost ~]# sed 's/^[[:space:]]\+//' /tmp/grub.conf 
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32-279.el6.i686)
root (hd0,0)
kernel /vmlinuz-2.6.32-279.el6.i686 ro root=UUID=e31be215-2c21-45f2-a43f-1a9c6bc383f2 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-279.el6.i686.img
[root@localhost ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sun Jul  2 04:16:54 2017
#
# 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
#
......
[root@localhost ~]# sed 's/^#[[:space:]]\+//' /etc/fstab

#
/etc/fstab
Created by anaconda on Sun Jul  2 04:16:54 2017
#
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
#
......
上一篇下一篇

猜你喜欢

热点阅读