使用sed、awk等格式化git日志输出

使用sed、awk等格式化git日志输出,git,formatting,sed,awk,Git,Formatting,Sed,Awk,如果我有这样的输出(来自定制的git日志) 您如何通过sed、awk或类似方式来实现此输出 d1fd022 2011-09-21 17:02 Replace double quotes with single quotes. 7227fe4 2011-09-21 13:57 Add automatic CommandTFlush to mkdir/rm/touch/cd 这里唯一的区别是秒和时区从日期中删除。不幸的是,git似乎不支持自定义日期格式,而且我不擅长使用se

如果我有这样的输出(来自定制的git日志)

您如何通过
sed
awk
或类似方式来实现此输出

d1fd022 2011-09-21 17:02       Replace double quotes with single quotes.
7227fe4 2011-09-21 13:57       Add automatic CommandTFlush to mkdir/rm/touch/cd
这里唯一的区别是秒和时区从日期中删除。不幸的是,git似乎不支持自定义日期格式,而且我不擅长使用
sed
,因此需要一些帮助

sed 's/:[^:]* -[^     ]*//' infile
已修复(它不是空格,而是选项卡)。

awk版本:

awk -F':[0-9][0-9] | ' '{$4=""}1' inputFile
测试:

kent$  echo "d1fd022 2011-09-21 17:02:26 -0400       Replace double quotes with single quotes.
7227fe4 2011-09-21 13:57:36 -0400       Add automatic CommandTFlush to mkdir/rm/touch/cd"|awk -F':[0-9][0-9] | ' '{$4=""}1'

d1fd022 2011-09-21 17:02        Replace double quotes with single quotes.
7227fe4 2011-09-21 13:57        Add automatic CommandTFlush to mkdir/rm/touch/cd

我建议使用
cut
(POSIX):


这个正则表达式还删除了提交消息的第一个单词。明白了,如果您的sed实现支持它(例如GNU sed),您需要输入一个文本选项卡:
sed的/:[^:]*-[^\t]*-[^Ctrl+V]*/'infle
,或者使用以下语法:
sed的/:[^:]*-[^\t]*/'inflee
。在使用sed时,这似乎仍然会中断输出:
d1fd022 2011-09-21 17:02用单引号替换双引号。
7227fe4 2011-09-21 13:57将自动命令flush添加到mkdir/rm/touch/cd命令。
(我尝试了两个建议的版本,现在使用OS X)好的,您似乎没有正确插入文本选项卡,你需要在这里键入Control-V,然后键入键盘的制表符:
sed的/:[^:]*-[^*这里你需要键入Control-V,然后键入TAB*/'infle
。啊,我明白了,是的,这有点复杂,可能是因为它的日志输出中使用了字符/嗯,运行这个命令(
git-mine | awk-F':[0-9][0-9]|“{$4=”“}1'
)返回与
git-mine
相同的输出。(
git-mine
是我的git别名,它以给定格式返回git日志)。也许有什么东西需要在zsh中逃脱?这里也是zsh尝试我的“测试”块下的代码:echo“…”awk-F。。。你得到了什么输出?我决定选择
cut
解决方案。
kent$  echo "d1fd022 2011-09-21 17:02:26 -0400       Replace double quotes with single quotes.
7227fe4 2011-09-21 13:57:36 -0400       Add automatic CommandTFlush to mkdir/rm/touch/cd"|awk -F':[0-9][0-9] | ' '{$4=""}1'

d1fd022 2011-09-21 17:02        Replace double quotes with single quotes.
7227fe4 2011-09-21 13:57        Add automatic CommandTFlush to mkdir/rm/touch/cd
[guest@pawel] ~ $ cut -c 1-24,34- foo.txt  
d1fd022 2011-09-21 17:02       Replace double quotes with single quotes.
7227fe4 2011-09-21 13:57       Add automatic CommandTFlush to mkdir/rm/touch/cd