从文本文件中获取数据

3
我正在尝试从一个具有以下结构的文本文件中提取数据:
Employee: John C.
  2013-01-01  10  $123
  2013-01-02  12  $120
  2013-01-03  8  $150
Employee: Michael G.
  2013-01-01  5  $13
  2013-01-05  11  $20
  2013-01-10  2  $155

如您所见,该模式是一个包含员工姓名的表头和包含其所有交易的表内容,然后重复此模式。
要提取交易,我会这样做:
awk '/^  [A-Z]/{print $1"\t"$2"\t"$3}'

This gives this result:

  2013-01-01  10  $123
  2013-01-02  12  $120
  2013-01-03  8   $150
  2013-01-01  5   $13
  2013-01-05  11  $20
  2013-01-10  2   $155

我可以为您创建一个两步提取过程,返回以下内容:
  2013-01-01  10  $123  John C.
  2013-01-02  12  $120  John C.
  2013-01-03  8   $150  John C.
  2013-01-01  5   $13   Michael G.
  2013-01-05  11  $20   Michael G.
  2013-01-10  2   $155  Michael G.
2个回答

5

使用 awk 的一种方式:

awk -F":" '/^Employee/{a=$NF;next}{print $0,a}' file

测试:

$ cat file
Employee: John C.
  2013-01-01  10  $123
  2013-01-02  12  $120
  2013-01-03  8  $150
Employee: Michael G.
  2013-01-01  5  $13
  2013-01-05  11  $20
  2013-01-10  2  $155
$ awk -F":" '/^Employee/{a=$NF;next}{print $0,a}' file
  2013-01-01  10  $123  John C.
  2013-01-02  12  $120  John C.
  2013-01-03  8  $150  John C.
  2013-01-01  5  $13  Michael G.
  2013-01-05  11  $20  Michael G.
  2013-01-10  2  $155  Michael G.

2

GNU sed 代码:

sed '/:/{s/[^:]\+://;H;x;s/.*\n//;d};G;s/\n//' file

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接