如果当前为空,则 Sed 复制上一个字段记录

Sed Copy Previous Field record if current is null

我有一个 csv 或 html 带有 table 这样的

Number Name ReportTo Time
11111 John Medical 0500
22222 Jane Dental 0700
                       Medical 1100
44444 Steve HR 0900
55555 Julie Training 0800
                       Records 1400
                       Business 1700
66666 David Medical 0800

我想找到一种方法来填充此 table 并消除所有空白字段。 table 应如下所示:

Number Name ReportTo Time
11111 John Medical 0500
22222 Jane Dental 0700
22222 Jane Medical 1100
44444 Steve HR 0900
55555 Julie Training 0800
55555 Julie Records 1400
55555 Julie Business 1700
66666 David Medical 0800

类似于 this 但使用 sed 并且从左开始 谢谢

在不知道更多关于格式的情况下,这个 awk 应该做的:

awk 'NF == 4 { p1 = ; p2 = ; print } NF == 2 { print p1, p2, ,  }' filename

即:

NF == 4 {   # in a line with four fields
  p1 =    # remember the first two
  p2 = 
  print     # print the line unchanged
}
NF == 2 {   # in a line with two fields
            # put the remembered fields before them.
  print p1, p2, , 
}

请注意,这假设整个文件由具有两个或四个字段的行组成;不符合此模式的行将被静默删除。如果您的文件包含此类行,我不清楚您希望如何处理它们。

如果你真的想用sed来做,那么

sed '/^[[:space:]]/ { G; s/^[[:space:]]*\(.*\)\n\(.*\)/ /; p; d; }; h; s/[[:space:]]\+/\n/2; s/\n.*//; x' filename

有效,但有点复杂:

/^[[:space:]]/ {                        # if a line begins with spaces, we
                                        # assume that front tokens are missing
  G                                     # get the remembered tokens from the
                                        # hold buffer
  s/^[[:space:]]*\(.*\)\n\(.*\)/ /  # put them before the tokens in this
                                        # line
  p                                     # print
  d                                     # and we're done.
}
h                                       # otherwise: Hold the line
s/[[:space:]]\+/\n/2                    # replace the second whitespace
                                        # sequence with \n
s/\n.*//                                # then remove it and everything after
                                        # it. This isolates the two first
                                        # fields in the line.
x                                       # swap that with the saved full line,
                                        # so the first two fields are in the
                                        # hold buffer for later use.

                                        # Dropping off the end leads to the
                                        # default action (printing), so the
                                        # full line is printed unchanged.