简体   繁体   English

如何在 AWK 中填充 CSV 中的空单元格?

[英]How to fill down empty cells in a CSV in AWK?

I have a CSV file like this, with @ as the delimiter:我有一个这样的 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.

The first column contains the chapter number.第一列包含章节编号。 The second column contains the text of the chapter.第二列包含该章的文本。

I need to fill down all of the chapter number in column A, such that any empty cells below are filled with the chapter number.我需要填写 A 列中的所有章节编号,以便下面的任何空单元格都填充章节编号。 Eg, the output would be this:例如,输出将是这样的:

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.

How can I fill down all of the empty cells in column A of the table?如何填写表格 A 列中的所有空单元格?

You can use awk like this:您可以像这样使用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.

Using a simple regex check we verify if $1 is not-empty then we set variable ch as name of the first chapter. Then in next subsequent lines we set chapter name to the value we've stored in variable使用简单的正则表达式检查,我们验证$1是否不为空,然后我们将变量 ch 设置as name of the first chapter. Then in next subsequent lines we set chapter name to the value we've stored in variable as name of the first chapter. Then in next subsequent lines we set chapter name to the value we've stored in variable ch`. as name of the first chapter. Then in next subsequent lines we set chapter name to the value we've stored in variable ch` as name of the first chapter. Then in next subsequent lines we set chapter name to the value we've stored in variable

Other way using awk使用awk其他方式

Input输入

$ cat file
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.

Output输出

$ awk 'BEGIN{FS=OFS="@"}/^[ \t]+/{$1=c}{c=$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.

$ awk 'BEGIN{FS=OFS="@"}/^[^\t ]+/{c=$1}{$1=c}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.

Try this solution -试试这个解决方案 -

awk -F@  '$1!~/^[[:space:]]+/ {h=$1FS} {print ($1!~/^[[:space:]]+/? $0 : h$2)}' f
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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM