lu@lu-VirtualBox:~$ sed --version
sed (GNU sed) 4.2.2
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Jay Fenlason, Tom Lord, Ken Pizzini,
and Paolo Bonzini.
GNU版sed主页: <http://www.gnu.org/software/sed/>。
使用GNU软件所需帮助文档: <http://www.gnu.org/gethelp/>。
将错误报告通过电子邮件发送到:<bug-sed@gnu.org>.
请务必将单词“sed”放在标题的某处。
#显示前十行
lu@lu-VirtualBox:~$ head -n10 /etc/passwd
passwd passwd-
lu@lu-VirtualBox:~$ head -n10 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
#创建到一个新的临时文件
lu@lu-VirtualBox:~$ cat /tmp/passwd.bak
sed的命令行语法:
sed [-n] program [file-list]
sed [-n] -f program [file-list]
sed工具从命令行所指定的文件或者标准输入中获取输入流。除非明确指定输出目标,否则SED将把结果输出到标准输出。
lu@lu-VirtualBox:~$ sed -e 'd' /tmp/passwd.bak
#d不输出被选择的行
lu@lu-VirtualBox:~$ sed -e '5d' /tmp/passwd.bak
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
#删除一到五行
lu@lu-VirtualBox:~$ sed -e '1,5d' /tmp/passwd.bak
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
lu@lu-VirtualBox:~$ cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
exit 0
#去除带注释的行
lu@lu-VirtualBox:~$ sed -e '/^#/d' /etc/rc.local |more
exit 0
#打印1到5行
lu@lu-VirtualBox:~$ sed -n -e '1,5p' /tmp/passwd.bak
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
#打印注释行
lu@lu-VirtualBox:~$ sed -n -e '/^#/p' /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
#有陷阱(windows通过SSH传给LINUX回车\r的问题)
lu@lu-VirtualBox:~$ sed -n -e '/main[[:space:]]*(/,/^}/p' /test/First.java |more public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(1);
// int a=10;
// int b=20;
// System.out.println(a+b);
Second first=new Second();
System.out.println(first.add(1,2));
System.out.println(new Second());
}
#把每行的OUT修改为IN
#加不加g有陷阱
#sed -e 's/a/xxxxxxx/g' /test/First.java
lu@lu-VirtualBox:~$ sed -e 's/out/in/g' /test/First.java
package hello;
public class First {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.in.println(1);
// int a=10;
// int b=20;
// System.in.println(a+b);
Second first=new Second();
System.in.println(first.add(1,2));
System.in.println(new Second());
}
//as;lkdjfa;sdjf
}
#将main函数中的所有a替换为xxxxx
lu@lu-VirtualBox:~$ sed -e '/main[[:space:]]*(/,/^}/s/a/xxxxx/g' /test/First.java
package hello;
public class First {
public stxxxxxtic void mxxxxxin(String[] xxxxxrgs) {
// TODO Auto-generxxxxxted method stub
System.out.println(1);
// int xxxxx=10;
// int b=20;
// System.out.println(xxxxx+b);
Second first=new Second();
System.out.println(first.xxxxxdd(1,2));
System.out.println(new Second());
}
//as;lkdjfa;sdjf
}
#冒号为分隔符
lu@lu-VirtualBox:~$ sed -e 's://:##:g' /test/First.java
## int a=10;
## int b=20;
## System.out.println(a+b);