如何强制 emacs(或任何编辑器)像 ASCII 格式一样读取文件?

发布于 2024-12-29 04:55:01 字数 412 浏览 4 评论 0原文

我无法在 maninfo 页面中找到这个答案,也无法在此处或 Google 上进行搜索。我有一个文件,本质上是一个文本文件,但在保存时不知何故搞砸了。 (我认为文件前面意外地出现了一些奇怪的字节。)

我可以使用 headcat 打开该文件,并且这是有意义的,但不使用任何类型的编辑器。

最后,我想做的就是在emacs中打开文件,删除“乱”字符,清理干净后保存。然而,该文件巨大,因此我需要像 emacs 这样强大的东西才能打开它。

否则,我想我可以尝试创建一个脚本来逐行读取它,强制脚本以文本格式读取它,然后写入它。但我想要快一点,因为我不会重复做这件事。超过。

谢谢! 麦克风

I could not find this answer in the man or info pages, nor with a search here or on Google. I have a file which is, in essence, a text file, but it somehow got screwed up upon saving. (I think there are a few strange bytes at the front of the file accidentally.)

I am able to open the file, and it makes sense, using head or cat, but not using any sort of editor.

In the end, all I wish to do is open the file in emacs, delete the "messy" characters, and save it once cleaned up. The file, however, is huge, so I need something powerful like emacs to be able to open it.

Otherwise, I suppose I can try to create a script to read this in line by line, forcing the script to read it in text format, then write it. But I wanted something quick, since I won't be doing this over & over.

Thanks!
Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

删除会话 2025-01-05 04:55:01
perl -i.bk -pe 's/[^[:ascii:]]//g;' file

在这里找到了这个 Perl One 衬垫: http://www.perlmonks.org/?node_id=619792

perl -i.bk -pe 's/[^[:ascii:]]//g;' file

Found this perl one liner here: http://www.perlmonks.org/?node_id=619792

仅一夜美梦 2025-01-05 04:55:01

在 Emacs 中尝试 Mxfind-file-literally

Try M-xfind-file-literally in Emacs.

眼眸里的那抹悲凉 2025-01-05 04:55:01

您可以使用 hexl-mode,它允许您以十六进制编辑文件。这可以让你准确地看到那些有问题的字符是什么,并将它们删除。

听起来您要么在文件中以不同的行结尾(例如:*nix 系统上的回车符),要么以意外的编码保存。

You could edit the file using hexl-mode, which lets you edit the file in hexadecimal. That would let you see precisely what those offending characters are, and remove them.

It sounds like you either got a different line ending in the file (eg: carriage returns on a *nix system) or it got saved in an unexpected encoding.

不乱于心 2025-01-05 04:55:01

您可以使用 strings 来获取“文件中的可打印字符”。您可能需要使用 --encoding 虽然我只用它来从可执行文件中获取 ascii 字符串。

You could use strings to grab "printable characters in file". You might have to play with the --encoding though I have only ever used it to grab ascii strings from executable files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文