数字引用实例1
- 数据
data.txt
$my_psql -c"drop table $line" >> $my_logfile
$my_psql -c"drop table $line >> $my_logfile
$my_psql -c"drop table $line >> $my_logfile
$my_psql -c"drop table $line >> $my_logfile
$my_psql -c "drop table $line >> $my_logfile
$my_psql -c "drop table $line >> $my_logfile
$my_psql -c "drop table $line >> $my_logfile
- 需求:
将drop table $line
后面的restrict
去除,并添加上cascade
- 解决方法:
sed -i 's/\(.*\)drop.*table.*line.*\(>>.*\)/\1drop table $line cascade \2/g' data.txt
删除匹配行及其下面 N 行
- 数据
[root@localhost dameng]# head seq.sql
CREATE SEQUENCE tbl_alarm_manual_backup_record_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
- 操作, 删除 CREATE SEQUENCE 匹配到的行以及下面 1 行
sed "/CREATE SEQUENCE/,+1d"
删除匹配行的下一行到 最后一行
- 数据
[root@localhost test]# cat test
- 操作
[root@localhost test]# sed '/c/{p;:a;N;$!ba;d}' test
删除匹配行的上面一行
- 删除匹配 your 的行的上面一行(全词匹配, 如果不是全词匹配则去掉 2 边 的 \b )
[root@localhost test]# cat -n test
1 how are you doing
2 now
3 what's your name?
4 hello
5 world
[root@localhost test]#
[root@localhost test]# sed '$!N;/\n.*\byour\b/!P;D' test
how are you doing
what's your name?
hello
world
去除空行
[root@localhost test]# cat test -A
how are you doing$
now $
what's your name?$
$
hello$
$
world$
[root@localhost test]#
[root@localhost test]# sed "/^$/d" test
how are you doing
now
what's your name?
hello
world
[root@localhost test]# sed "/^$/d" test | cat -A
how are you doing$
now $
what's your name?$
$
hello$
world$