2017-03-20 102 views
1

我有一個CSV文件這樣,用@作爲分隔符:如何在AWK中填充CSV格式的空單元格?

Chapter [email protected] is some text. 
     @This is some more text. 
     @This is yet some more text. 
Chapter [email protected] is some text. 
     @This is some more text. 
     @This is yet some more text. 

第一列包含章節編號。第二欄包含本章的內容。

我需要填寫A列中的所有章節編號,以便下面的任何空單元格都填入章節編號。例如,輸出爲:

Chapter [email protected] is some text. 
Chapter [email protected] is some more text. 
Chapter [email protected] is yet some more text. 
Chapter [email protected] is some text. 
Chapter [email protected] is some more text. 
Chapter [email protected] is yet some more text. 

如何填滿表格A列中的所有空單元格?

回答

2

您可以使用awk這樣的:

awk 'BEGIN{FS=OFS="@"} {if ($1 ~ /^[ \t]*$/) $1=ch; else ch=$1} 1' file 

Chapter [email protected] is some text. 
Chapter [email protected] is some more text. 
Chapter [email protected] is yet some more text. 
Chapter [email protected] is some text. 
Chapter [email protected] is some more text. 
Chapter [email protected] is yet some more text. 

使用簡單的regex檢查,我們確認是否$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 ch`。

1

使用awk

輸入

$ cat file 
Chapter [email protected] is some text. 
     @This is some more text. 
     @This is yet some more text. 
Chapter [email protected] is some text. 
     @This is some more text. 
     @This is yet some more text. 

輸出

$ awk 'BEGIN{FS=OFS="@"}/^[ \t]+/{$1=c}{c=$1}1' file 
Chapter [email protected] is some text. 
Chapter [email protected] is some more text. 
Chapter [email protected] is yet some more text. 
Chapter [email protected] is some text. 
Chapter [email protected] is some more text. 
Chapter [email protected] is yet some more text. 

$ awk 'BEGIN{FS=OFS="@"}/^[^\t ]+/{c=$1}{$1=c}1' file 
Chapter [email protected] is some text. 
Chapter [email protected] is some more text. 
Chapter [email protected] is yet some more text. 
Chapter [email protected] is some text. 
Chapter [email protected] is some more text. 
Chapter [email protected] is yet some more text. 
相關問題