免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: iceblood
打印 上一主题 下一主题

[文本处理] 求删除特定字符片区的方法 [复制链接]

论坛徽章:
1
金牛座
日期:2014-05-29 15:55:47
11 [报告]
发表于 2014-05-21 18:26 |只看该作者
本帖最后由 iceblood 于 2014-05-21 18:29 编辑
expert1 发表于 2014-05-21 17:49
提前说一下,要原文改,记得-i,省得再问。

也许Linux行,不过我是FreeBSD……

  1. > cat tt.sh
  2. #awk -vRS='lease' 'NR>1&&!/20:dc:e6:3c:75:4d/{printf RS$0}' tt.txt
  3. #awk  '!/20:dc:e6:3c:7f:4b/&&NF{print RS$0}'  RS=lease tt.txt
  4. sed '/{/{:a;N;/}/!ba;/20:dc:e6:3c:7f:4b/d}' tt.txt

  5. > sh tt.sh
  6. sed: 1: "/{/{:a;N;/}/!ba;/20:dc: ...": unexpected EOF (pending }'s)
  7. >
复制代码
看了你那个帖子,你的sed和awk简直到神般的境界,不过……哎,我不是gnu sed。

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
12 [报告]
发表于 2014-05-21 19:00 |只看该作者
回复 11# iceblood

Try this way for Unix like

note: you can replace file directly with -i argument(ex: perl -i -lane '.....')

$ perl -lane '$P.="\n$_";if(m/^lease /){$c=1;$P=$_};print if(!$c);if(m/^}\s*$/){print $P if($P!~m/20:dc:e6:3c:75:4d/);$c=0}' tt.txt
# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-4.2.4

lease 10.108.215.157 {
  starts 2 2014/02/11 04:06:44;
  ends 3 2014/02/12 02:01:38;
  tstp 3 2014/02/12 02:01:38;
  cltt 2 2014/02/11 04:06:44;
  binding state free;
  hardware ethernet 6c:e8:73:ad:56:17;
  uid "\001l\350s\255V\027";
}
lease 10.108.215.155 {
  starts 3 2014/02/12 04:15:06;
  ends 3 2014/02/12 07:21:35;
  tstp 3 2014/02/12 07:21:35;
  cltt 3 2014/02/12 04:15:06;
  binding state free;
  hardware ethernet 10:78:d2:cc:d3:9d;
  uid "\001\020x\322\314\323\235";
}
lease 10.108.215.153 {
  starts 2 2014/02/18 09:00:22;
  ends 3 2014/02/19 09:00:22;
  tstp 3 2014/02/19 09:00:22;
  cltt 2 2014/02/18 09:00:22;
  binding state free;
  hardware ethernet 18:03:73:97:8c:82;
  uid "\001\030\003s\227\214\202";
}
lease 10.108.215.154 {
  starts 4 2014/02/20 07:34:35;
  ends 5 2014/02/21 02:00:32;
  tstp 5 2014/02/21 02:00:32;
  cltt 4 2014/02/20 07:34:35;
  binding state free;
  hardware ethernet 6c:e8:73:ad:32:51;
  uid "\001l\350s\2552Q";
}

论坛徽章:
1
金牛座
日期:2014-05-29 15:55:47
13 [报告]
发表于 2014-05-21 19:07 |只看该作者
本帖最后由 iceblood 于 2014-05-21 19:14 编辑
jason680 发表于 2014-05-21 19:00
回复 11# iceblood

Try this way for Unix like

sed awk都有了,就差perl,然后你就来了。
能把20:dc:e6:3c:75:4d当参数赋予吗?
比如:
xxx.pl 20:dc:e6:3c:75:4d
或者
perl "xxxxxxxxx $MAC"
因为你这个是“'”我无法赋予变量。
另外文件编辑后立刻生效。

论坛徽章:
1
金牛座
日期:2014-05-29 15:55:47
14 [报告]
发表于 2014-05-22 08:50 |只看该作者
顶上来。。。。。。。。。。

论坛徽章:
1
金牛座
日期:2014-05-29 15:55:47
15 [报告]
发表于 2014-05-22 10:45 |只看该作者
expert1 发表于 2014-05-21 17:27
我说是个月经贴没人反对吧?
http://bbs.chinaunix.net/thread-4136159-1-1.html
看明 ...

能弄个awk的试试吗?我的sed不是gnu sed。

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
16 [报告]
发表于 2014-05-22 11:24 |只看该作者
  1. awk -vRS='lease [0-9]+.[0-9]+.[0-9]+.[0-9]+ {[^}]+}' 'RT!~/20:dc:e6:3c:7f:4b/{print RT}'
复制代码
回复 15# iceblood


    正对1楼的数据测试如下
lease 10.108.215.134 {

  starts 2 2014/05/20 13:46:14;

  ends 3 2014/05/21 13:46:14;

  cltt 2 2014/05/20 13:46:14;

  binding state active;

  next binding state free;

  rewind binding state free;

  hardware ethernet 6c:e8:73:ad:32:db;

  uid "\001l\350s\2552\333";

  option agent.remote-id 0:23:ed:22:69:a2;

  client-hostname "WR740N";

}
lease 10.108.215.148 {

  starts 2 2014/05/20 15:12:33;

  ends 3 2014/05/21 15:12:33;

  cltt 2 2014/05/20 15:12:33;

  binding state active;

  next binding state free;

  rewind binding state free;

  hardware ethernet 40:16:9f:7c:74:e7;

  uid "\001@\026\237|t\347";

  option agent.remote-id 0:25:f1:fd:90:7a;

  client-hostname "NatRouter";

}

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
17 [报告]
发表于 2014-05-22 11:31 |只看该作者
# The format of this file is documented in the dhcpd.leases(5) manual page.

# This lease file was written by isc-dhcp-4.2.4

若这种垃圾数据需要的话可以改一下。

估计你的又没有GNU的exensions(RT)
那么还有别的办法,把RS设为{,遍历Filed,不匹配就打印$0

论坛徽章:
0
18 [报告]
发表于 2014-05-22 12:30 |只看该作者
awk '!/20:dc:e6:3c:75:4d/{printf $0RS}' RS="}" XXX.txt

论坛徽章:
1
金牛座
日期:2014-05-29 15:55:47
19 [报告]
发表于 2014-05-22 14:46 |只看该作者
wslm31 发表于 2014-05-22 12:30
awk '!/20:dc:e6:3c:75:4d/{printf $0RS}' RS="}" XXX.txt

成功!!!!谢谢!!!!!

论坛徽章:
1
金牛座
日期:2014-05-29 15:55:47
20 [报告]
发表于 2014-05-22 14:49 |只看该作者
expert1 发表于 2014-05-22 11:31
# The format of this file is documented in the dhcpd.leases(5) manual page.

# This lease file was ...

你楼下的兄弟的好用了。你的可能也是因为gawk的原因,所以暂时失败。但我相信Linux下不会有问题,我想以后我应该能用上。谢谢你提供这么多次帮助。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP