linux - 如何解析 svn diff 结果?

标签 linux shell unix sed grep

使用带有 --summarize 标志的 svn diff 返回类似下面的内容。我们如何将其传递给 sed 或 grep 以执行以下操作:

  1. 删除所有以“D”开头的行(删除的文件)
  2. 删除“M”、“A”或“MM”(或任何其他情况)的前缀以及之后的标签。
  3. 删除 URL 路径,仅保留文件名/文件夹。
  4. 保存在文件中

例子:

D   https://localhost/example/test1.php
D   https://localhost/example/test2.php
M   https://localhost/example/test3.php
M   https://localhost/example/test4.php
A   https://localhost/example/test5.php
M   https://localhost/example/test6.php
A   https://localhost/example/test7.php
M   https://localhost/example/test8.php
M   https://localhost/example/test9.php
M   https://localhost/example/test10.php
A   https://localhost/example/test11.php
M   https://localhost/example/test12.php
M   https://localhost/example/test13.php
MM  https://localhost/example/test.php
M   https://localhost/test0.php

然后会变成:

/example/test3.php
/example/test4.php
/example/test5.php
/example/test6.php
/example/test7.php
/example/test8.php
/example/test9.php
/example/test10.php
/example/test11.php
/example/test12.php
/example/test13.php
/example/test.php
/test0.php

最佳答案

像这样使用 sed:

$ svn diff --summarize | sed -e '/^D/d' -e 's/.*host//'
/example/test3.php
/example/test4.php
/example/test5.php
/example/test6.php
/example/test7.php
/example/test8.php
/example/test9.php
/example/test10.php
/example/test11.php
/example/test12.php
/example/test13.php
/example/test.php
/test0.php

# Redirect output to file
$ svn diff --summarize | sed -e '/^D/d' -e 's/.*host//' > file

您需要pipe | svnsed 的输出。第一部分 '/^D/d' 删除所有以 D 开头的行,第二部分 s/.*host// 替换所有内容直到 host 什么都没有,要存储到文件中使用 redirect >文件

grep类似的逻辑:

$ svn diff --summarize | grep '^[^D]' file | grep -Po '(?<=host).*' > file

第一个 grep 过滤掉以 D 开头的行,第二个使用 positive lookahead使用 -Po 只显示 host 之后的部分行。

关于linux - 如何解析 svn diff 结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14176503/

相关文章:

python - 多个文件到单个文件的符号链接(symbolic link)

regex - grep -e "Pattern"--regexp=Pattern 有什么用?

java - 调试随机 SIGSEGV 崩溃

python - 如何使用ssh执行远程脚本

linux - 从另一个脚本停止正在运行的 bash 脚本

即使添加新的防火墙规则后连接仍被拒绝

php - 从编码人员的角度来看,我应该选择哪种项目 python 而不是 php 因为两者都可以完成这项工作?

c++ - 众所周知的虚拟文件夹 GUID 是什么?

linux - 每 30 秒进行一次日志轮换并将日志文件存储在以日期命名的目录中

unix - 在 Unix 上提取电子邮件附件