如何修改git-log-peit格式以从其中一个字段中去掉某些字符(如逗号)



我有一个bash脚本,它创建了一个CSV文件,其中包含一些repo中一组子目录的git日志,使用git-log的--pretty格式将每个提交与脚本中的一些变量以及一些内置选项放在一行。

我正在使用的漂亮的基本格式如下:

git log --pretty="$PWD,${current_dir},%h,%cs,%cN,%s" 

在bash脚本中,整行看起来像:

(git log --no-merges --after="${number_of_days} days ago" --pretty="$PWD,${current_dir},%h,%cs,%cN,%s" -- "$current_dir") >> "$starting_dir"/export.csv

下面是CSV的一个示例:

/path/to/parent,./NameOfSubdirectory,bdacd7e,2021-03-24,kclemson,commit message,with,extra,commas,in it
/path/to/parent,./NameOfSubdirectory,45cb4a0,2021-03-24,kclemson,commit message foo
/path/to/parent,./NameOfSubdirectory,9f8294b,2021-03-24,kclemson,commit message bar
/path/to/parent,./NameOfSubdirectory,3e91e92,2021-03-24,kclemson,commit message baz

这在大多数情况下都很好,但当在Excel中打开CSV时,%s中的逗号当然会打乱格式。我想让它做的是用空格替换%s中的任何逗号,所以它看起来像这样:

/path/to/parent,./NameOfSubdirectory,bdacd7e,2021-03-24,kclemson,commit message with extra commas in it
/path/to/parent,./NameOfSubdirectory,45cb4a0,2021-03-24,kclemson,commit message foo
/path/to/parent,./NameOfSubdirectory,9f8294b,2021-03-24,kclemson,commit message bar
/path/to/parent,./NameOfSubdirectory,3e91e92,2021-03-24,kclemson,commit message baz

做这件事的正确方法是什么?我想我需要把它传递给sed,但我正在寻找关于sed语法的帮助,以便在"对于"在该字段中,以及如何在bash脚本中做到这一点。

谢谢。

不要使用,,而是使用主题中不太可能出现的其他分隔符,如^^^。先用空白替换,,然后用,替换^^^

git log --pretty="$PWD^^^${current_dir}^^^%h^^^%cs^^^%cN^^^%s" | 
sed -e 's/,/ /g' | sed -e 's/^^^/,/g'

最新更新