如何重写公共前缀作为标题?

时间:2011-02-21 18:18:43

标签: shell

假设你有一份这样的变更说明清单:

2011-02-21: Removed first feature to get rid of previous customer
2011-02-21: Added second feature to open up new markets
2011-02-09: Implemented hot fix to calm down angry customer
2011-02-08: Developed project into a minimal viable product
2011-02-08: Started project

可以使用标准shell工具将其重写为以下格式吗?

2011-02-21
  * Removed first feature to get rid of previous customer
  * Added second feature to open up new markets

2011-02-09
  * Implemented hot fix to calm down angry customer

2011-02-08
  * Developed project into a minimal viable product
  * Started project

若然,怎么样?

2 个答案:

答案 0 :(得分:3)

通过此管道。

#!/bin/bash

lastprefix=""
while read prefix line
do
  if [ "$prefix" != "$lastprefix" ]
  then
    lastprefix="$prefix";
    echo ""
    echo "$prefix"
  fi
  echo "  *  $line"
done

答案 1 :(得分:1)

这应该这样做:

awk -F: '$1 != prev {print $1; prev = $1} {$1 = ""; print "  *" $0}' inputfile