Vil*_*age 3 regex csv bash awk
我有一个像这样的 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.
Run Code Online (Sandbox Code Playgroud)
第一列包含章节编号。第二列包含该章的文本。
我需要填写 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.
Run Code Online (Sandbox Code Playgroud)
如何填写表格 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.
Run Code Online (Sandbox Code Playgroud)
使用简单的正则表达式检查,我们验证是否$1为非空,然后我们设置变量 ch as name of the first chapter. Then in next subsequent lines we set chapter name to the value we've stored in variablech`。