我需要使用输出标记来区分两个未排序的文本文件每一个行为旧的、新的或未更改的。这些标记应该出现在行的开头或结尾,并且除了这些标记的行之外不应有其他输出。
例如,假设我们有以下两个文件:
旧的.txt
unchanged line one
unchanged line two
unchanged line three
deleted line one
deleted line two
deleted line three
unchanged line four
unchanged line five
unchanged line six
新的.txt
unchanged line one
unchanged line two
unchanged line three
unchanged line four
unchanged line five
inserted line one
inserted line two
inserted line three
unchanged line six
GNU diff 允许您为旧的、新的和未更改的“输入组”指定打印格式,但这些格式仅应用于旧/新/未更改行的运行中的第一行。例如,运行diff --old-group-format='- %<' --new-group-format='+ %>' --unchanged-group-format='| %=' old.txt new.txt
结果为
| unchanged line one
unchanged line two
unchanged line three
- deleted line one
deleted line two
deleted line three
| unchanged line four
unchanged line five
+ inserted line one
inserted line two
inserted line three
| unchanged line six
但是,我需要差异看起来像这样,其中每一行都被标记:
| unchanged line one
| unchanged line two
| unchanged line three
- deleted line one
- deleted line two
- deleted line three
| unchanged line four
| unchanged line five
+ inserted line one
+ inserted line two
+ inserted line three
| unchanged line six
我可以使用 GNU diff 来完成此操作,无论是单独使用还是与其他常见的命令行文本处理工具(如 sed)结合使用?便携式(POSIX)解决方案可能会更好,只要它不是太麻烦。
答案1
GNUdiff
支持行格式和组格式:
$ diff --old-line-format='- %L' --new-line-format='+ %L' --unchanged-line-format='| %L' old.txt new.txt
| unchanged line one
| unchanged line two
| unchanged line three
- deleted line one
- deleted line two
- deleted line three
| unchanged line four
| unchanged line five
+ inserted line one
+ inserted line two
+ inserted line three
| unchanged line six