我有一个像这样的 CSV 文件,以 @
作为分隔符:
Chapter 1@This is some text.
@This is some more text.
@This is yet some more text.
Chapter 2@This is some text.
@This is some more text.
@This is yet some more text.
第一列包含章节编号。第二列包含章节的文本。
我需要填写 A 列中的所有章节编号,以便下面的任何空单元格都用章节编号填充。例如,输出将是这样的:
Chapter 1@This is some text.
Chapter 1@This is some more text.
Chapter 1@This is yet some more text.
Chapter 2@This is some text.
Chapter 2@This is some more text.
Chapter 2@This is yet some more text.
如何填写表格 A 列中的所有空单元格?
最佳答案
您可以像这样使用 awk
:
awk 'BEGIN{FS=OFS="@"} {if ($1 ~ /^[ \t]*$/) $1=ch; else ch=$1} 1' file
Chapter 1@This is some text.
Chapter 1@This is some more text.
Chapter 1@This is yet some more text.
Chapter 2@This is some text.
Chapter 2@This is some more text.
Chapter 2@This is yet some more text.
我们使用简单的正则表达式检查来验证 $1
是否为空,然后我们将变量 ch 设置为第一章的名称。然后在接下来的后续行中,我们将章节名称设置为我们存储在变量
ch`中的值。
关于regex - 如何在 AWK 中填写 CSV 中的空单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42911393/