如何删除文件中位置 51 处的所有 CRLF,以相反的顺序处理文件

问题描述 投票:0回答:1

我有一个很大的文本文件,输出该文件的应用程序将其自动换行为 50 个字符。

实际原始展开行长度差异很大,从 1 个字符到 1500 多个字符不等。

我需要能够以相反的顺序处理文件(从底部开始)并删除位于位置 51 的每个 CRLF,但将其他位置的所有 CRLF 单独保留。

(因此顺序相反。超过 1500 个字符的行在每个位置 51 处都有大约 56 个 CRLF。最后的必须首先删除,以保持字符串完整性)。

由于逆序的必要性,据我所知,这意味着 sed 已经过时了。 notepad++ 中的正则表达式查找和替换也没有“向后方向”作为可选选项。

我在窗户上。文件本身是通过 powershell 生成的,但我通过 Cmder 安装了 python、node、cygwin,老实说,我愿意为此安装任何东西,但由于公司政策,wsl 目前是不可能的。 VBScript 也是如此。

我尝试了 n++ 中的各种查找和替换扩展选项,但除了 pos51 处的 [CR][LF] 之外,没有一致的轮廓符。

示例——尝试保留格式:

COMMENT ON COLUMN "vendor"."things_andstuf_associa
tions"."id" IS 'The unique identifier for a things
andstuf association record.';

COMMENT ON COLUMN "vendor"."things_andstuf_associa
tions"."course_id" IS 'Identifies the course.';
COMMENT ON COLUMN "vendor"."things_andstuf_associa
tions"."created_at" IS 'Timestamp of when the reco
rd was created.';

COMMENT ON COLUMN 只是数千行日志记录中的一小部分。有些从调试开始,有些从信息开始,有些从 SELECT 开始,有些从格式化日期开始,有些从 UPSERT 开始,有些从 ON CONFLICT 开始......它变化很大。 --所有行都不以分号结尾。保留空白行可能会更好。 –

没有特定于每行开头的唯一格式化文本字符串。我可能必须(并且愿意)接受所有完全包含 50 个字符的行将与所有正确合并的行一起错误地合并。

输出来自已编译的 python 应用程序,并通过 powershell 中的 start-transcript 捕获。我无法影响 powershell 生成时的输出。然而,我可以影响 powershell 事后输出的转录文件。

我能找到的唯一常量是换行在位置 51 处有一个 CRLF。

regex powershell logging awk text
1个回答
0
投票

也许您可以使用模式

(?m)(?<=^.{50})\r?\n
,PowerShell 示例:

$text = @'
COMMENT ON COLUMN "vendor"."things_andstuf_associa
tions"."id" IS 'The unique identifier for a things
andstuf association record.';

COMMENT ON COLUMN "vendor"."things_andstuf_associa
tions"."course_id" IS 'Identifies the course.';
COMMENT ON COLUMN "vendor"."things_andstuf_associa
tions"."created_at" IS 'Timestamp of when the reco
rd was created.';
'@

$text -replace '(?m)(?<=^.{50})\r?\n'

结果:

COMMENT ON COLUMN "vendor"."things_andstuf_associations"."id" IS 'The unique identifier for a thingsandstuf association record.';

COMMENT ON COLUMN "vendor"."things_andstuf_associations"."course_id" IS 'Identifies the course.';
COMMENT ON COLUMN "vendor"."things_andstuf_associations"."created_at" IS 'Timestamp of when the record was created.';

如果这是您期望的输出,那么您可以使用

Get-Content -Raw
将文件内容获取为单个多行字符串,然后应用此替换:

$content = Get-Content path\to\thefile.txt -Raw
$content -replace '(?m)(?<=^.{50})\r?\n' | Set-Content path\to\otherfile.txt

详情请参阅https://regex101.com/r/sO0miY/1

© www.soinside.com 2019 - 2024. All rights reserved.