gpt4 book ai didi

shell - diff 文件名在每行前

转载 作者:行者123 更新时间:2023-12-05 05:25:57 25 4
gpt4 key购买 nike

如何区分两个(或更多)文件,在每行的开头显示文件名?

也就是说,不是这样的:

--- file1.c
+++ file2.c
@@ -1 +1 @@
-int main() {
+int main(void) {

我更喜欢这样的东西:

file1.c:- int main() {
file2.c:+ int main(void) {

当只有两个文件时这不是很有用,但在使用 --from-file/--to-file 时非常方便。

最佳答案

我找不到更简洁的解决方案,所以我编写了自己的脚本来执行此操作,多次调用 diff 以每次添加不同的前缀。

#!/bin/bash

# the first argument is the original file that others are compared with
orig=$1
len1=${#1}
shift

# we compute the length of the filenames to ensure they are aligned
for arg in "$@"
do
len2=${#arg}
maxlen=$((len1 > len2 ? len1 : len2))
prefix1=$(printf "%-${maxlen}s" "$orig")
prefix2=$(printf "%-${maxlen}s" "$arg")
diff --old-line-format="$prefix1:-%L" \
--new-line-format="$prefix2:+%L" \
--unchanged-line-format="" $orig $arg
echo "---" # not necessary, but helps visual separation
done

关于shell - diff 文件名在每行前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28857594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com