Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 差异行格式:是否显示删除、新建和更改的行?_Linux_Shell - Fatal编程技术网

Linux 差异行格式:是否显示删除、新建和更改的行?

Linux 差异行格式:是否显示删除、新建和更改的行?,linux,shell,Linux,Shell,backup.txt user1:password:17002:0:99:7::: user2:password:17003:0:99:7::: user3:password:17004:0:99:7::: “main.txt”与“backup.txt”相同。如果我重命名“user1”,添加一个新用户,并删除“main.txt”中的“user2”。“main.txt”看起来像: username1:password:17002:0:99:7::: user3:password:17004:0:

backup.txt

user1:password:17002:0:99:7:::
user2:password:17003:0:99:7:::
user3:password:17004:0:99:7:::
“main.txt”与“backup.txt”相同。如果我重命名“user1”,添加一个新用户,并删除“main.txt”中的“user2”。“main.txt”看起来像:

username1:password:17002:0:99:7:::
user3:password:17004:0:99:7:::
newUser:password:17005:0:99:7:::
之后,我使用以下命令比较两个文件:

diff --unchanged-line-format="" --old-line-format=":%dn: %L" --new-line-format=":%dn: %L" backup.txt main.txt
…与实际输出:

:1: user1:password:17002:0:99:7:::
:2: user2:password:17003:0:99:7:::
:1: username1:password:17002:0:99:7:::
:3: newUser:password:17005:0:99:7:::
然而,我的预期产出是:

:1c: user1:password:17002:0:99:7:::
:2d: user2:password:17003:0:99:7:::
:1c: username1:password:17002:0:99:7:::
:3a: newUser:password:17005:0:99:7::: 

像这样。这些字符是使用默认“diff”命令启用的。如何启用这些字符进行行格式设置。有可能吗?

BSD和GNU diff提供的
LTYPE
s是“旧的”、“新的”和“未更改的”。因此,您无法区分“新”和“更改”

也就是说,要在格式字符串中获得一些区别,您需要正确地填写它们。在
%dn
中,使用
d
n
(前者指定一个十进制值,
n
指定它指的是行号或修改的行数,具体取决于上下文)。因此,如果您需要任何额外字符(如
c
d
a
),则需要在替换完成后添加这些字符

# declaring functions to allow testing without creating files on-disk
backup () { printf '%s\n' user1:password:17002:0:99:7::: user2:password:17002:0:99:7::: user3:password:17002:0:99:7:::; }
main () { printf '%s\n' username1:password:17002:0:99:7::: user3:password:17004:0:99:7::: newUser:password:17005:0:99:7:::; }

diff \
  --unchanged-line-format=":%dnu: %L" \
  --old-line-format=":%dnd: %L" \
  --new-line-format=":%dnn: %L" \
  <(backup) <(main)
#声明允许在不在磁盘上创建文件的情况下进行测试的函数
备份(){printf“%s\n”user1:password:17002:0:99:7:::user2:password:17002:0:99:7:::user3:password:17002:0:99:7::;}
main(){printf'%s\n'用户名1:密码:17002:0:99:7:::用户3:密码:17004:0:99:7:::新用户:密码:17005:0:99:7:;}
差异\
--未更改的行格式=“:%dnu:%L”\
--旧行格式=“:%dnd:%L”\
--新行格式=“:%dnn:%L”\

您的意思是希望diff使用这些字符来显示删除、更改或添加的行?请详细说明这一点,而不是让读者从上下文中假设您的意图。(此外,
diff
实际上不是shell的一部分,这使它更像是关于操作系统工具的问题,而不是shell本身的问题)。