免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 80995 | 回复: 14
打印 上一主题 下一主题

perl 怎么以16进制操作文件,替换文件内容? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-08-14 09:03 |只看该作者 |正序浏览
本帖最后由 sofs 于 2016-08-14 09:05 编辑



左边是原文件,右边是我替换了内容后的文件。

我用的命令是 perl -p -i -e 's/\x48\x5c\x00\x00\x00\x00\x00\x00\x03\x80.*//g' ./file.gz

需求是要将左边 1 红框后面的内容全部删除掉。用了上面那个perl替换命令后,只是删除掉了一部分内容(右边空白处就是被删除的),而后面还有一大段内容没被删除(2)。

研究了很久,sed,tr这些都试了,没找到方法,求大神指点。

下面是原附件。

file.gz (2.31 KB, 下载次数: 25)


如果不想在CU扫描下载的话,我在这里也传了一份附件 : doora.qiniudn.com/vSREh.gz   (下载后请重命名为file.gz)

论坛徽章:
0
15 [报告]
发表于 2016-08-18 15:49 |只看该作者
回复 14# sunzhiguolu


    嗯,这个也行,但用xxd两次转换,效率比较差一点。

实现了总是好的。

感谢。

论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
14 [报告]
发表于 2016-08-17 22:46 |只看该作者
本帖最后由 sunzhiguolu 于 2016-08-17 23:04 编辑

回复 1# sofs
这样?
  1. xxd -p file.gz|perl -0ne '$p=$+[0] while(/7d81a03ec1a7/g);print substr($_,0,$p)'|xxd -p -r>rst
复制代码


   

论坛徽章:
0
13 [报告]
发表于 2016-08-17 17:45 |只看该作者
回复 12# jason680


    测试通过。

感谢。

论坛徽章:
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 [报告]
发表于 2016-08-16 23:13 |只看该作者
回复 11# sofs

$ awk 'BEGIN{RS="\x48\x5c\x5c\x00\x00\x00\x00\x00\x00\x03\x80"}{if(a!="")printf("%s",a);a=$0RT}' file.gz | hexdump -C
00000000  0f c9 50 a6 a2 e5 00 a2  e3 38 05 05 d3 5e 15 a9  |..P......8...^..|
00000010  32 b7 a0 00 b0 69 30 a5  43 2f 07 ef f8 73 5c 1a  |2....i0.C/...s\.|
...
000005c0  80 21 1a d3 40 7d 81 a0  3e c1 a7 48 5c 00 00 00  |.!..@}..>..H\...|
000005d0  00 00 00 03 80                                    |.....|
000005d5

   

论坛徽章:
0
11 [报告]
发表于 2016-08-16 15:35 |只看该作者
回复 5# jason680


    刚刚放到实际应用中使用,又发现了问题:

\x48\x5c\x00\x00\x00\x00\x00\x00\x03\x80 这个特征码在很多文件里面不是唯一,如何可以做到只删除最后一次出现该特征码后的内容?

论坛徽章:
0
10 [报告]
发表于 2016-08-16 14:25 |只看该作者
回复 5# jason680


    可行,解决了困扰我两天的问题。

感谢大神!

论坛徽章:
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
9 [报告]
发表于 2016-08-16 08:49 |只看该作者
回复 7# hztj2005

1. 无Win10
有Win8

2. in Windows
  Don't use it in Windows

* you need modify the code into a script file by yourself

论坛徽章:
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
8 [报告]
发表于 2016-08-16 08:40 |只看该作者
本帖最后由 jason680 于 2016-08-16 08:42 编辑

回复 6# sunzhiguolu

    binmode FILEHANDLE, LAYER
    binmode FILEHANDLE
            Arranges for FILEHANDLE to be read or written in "binary" or
            "text" mode on systems where the run-time libraries distinguish
            between binary and text files. If FILEHANDLE is an expression, the
            value is taken as the name of the filehandle. Returns true on
            success, otherwise it returns "undef" and sets $! (errno).

            
            On some systems (in general, DOS- and Windows-based systems)
            binmode() is necessary when you're not working with a text file.

            For the sake of portability it is a good idea always to use it
            when appropriate, and never to use it when it isn't appropriate.
            Also, people can set their I/O to be by default UTF8-encoded
            Unicode, not bytes.

            ...

论坛徽章:
0
7 [报告]
发表于 2016-08-16 01:04 |只看该作者
jason680 发表于 2016-08-15 23:31
回复 1# sofs

$ perl -pe 'BEGIN{$/=undef}{s/\x48\x5c\x00\x00\x00\x00\x00\x00\x03\x80.*//gs}' file ...


我在win10下执行这个语句,返回的是:

'$' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

是否只能在unix/Linux下执行?
  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP