awk从之前的值填充然后删除

时间:2017-09-16 18:50:21

标签: awk

如果第二列( $2=="")为空,则考虑该行,然后将值打印为$1,其余行直到下一个第二列为空($2=="")。 然后需要delete第二列为空的所有行( $2=="")

FILE.CSV

Name_Subject,Marks,Desc,Details
Adam,,,
English,10,xxx,xxx
Maths,20,yyy,yyy
Benn,,,
English,10,xxx,xxx
Maths,20,yyy,yyy
Science,30,zzz,zzz
Zak,,,
English,10,xxx,xxx
Maths,20,yyy,yyy

output.csv

Name,Subject,Marks,Desc,Details
Adam,English,10,xxx,xxx
Adam,Maths,20,yyy,yyy
Benn,English,10,xxx,xxx
Benn,Maths,20,yyy,yyy
Benn,Science,30,zzz,zzz
Zak,English,10,xxx,xxx
Zak,Maths,20,yyy,yyy

我尝试过以下命令但没有成功......

awk -F, 'NR==1{print; next} {if($2 == ""){$2=A}} {A=$2} 1' file.csv

修改#1

实际文件包含,某些行的整行在数据之间是空的。对于此实例,它填充空值但喜欢捕获先前的非空值。

FILE.CSV

Name_Subject,Marks,Desc,Details
Adam,,,
English,10,xxx,xxx
Maths,20,yyy,yyy
Benn,,,
English,10,xxx,xxx
Maths,20,yyy,yyy
Science,30,zzz,zzz
,,,
,,,
History,40,zzz,zzz
Zak,,,
English,10,xxx,xxx
Maths,20,yyy,yyy

输出#1

Name,Subject,Marks,Desc,Details
Adam,English,10,xxx,xxx
Adam,Maths,20,yyy,yyy
Benn,English,10,xxx,xxx
Benn,Maths,20,yyy,yyy
Benn,Science,30,zzz,zzz
,History,40,zzz,zzz
Zak,English,10,xxx,xxx
Zak,Maths,20,yyy,yyy

2 个答案:

答案 0 :(得分:1)

$ awk '
    BEGIN  { FS=OFS="," }
    NR==1  { split($1,f,/_/); name=f[1]; $1=f[2] }
    $2=="" { name=$1; next }
    { print name, $0 }
' file
Name,Subject,Marks,Desc,Details
Adam,English,10,xxx,xxx
Adam,Maths,20,yyy,yyy
Benn,English,10,xxx,xxx
Benn,Maths,20,yyy,yyy
Benn,Science,30,zzz,zzz
Zak,English,10,xxx,xxx
Zak,Maths,20,yyy,yyy

答案 1 :(得分:1)

awk 方法:

awk -F, 'NR==1;$1==""{next}NR>1 && $2==""{ f=$1; next }f{ print f,$0 }' OFS=',' file 

输出:

Name_Subject,Marks,Desc,Details
Adam,English,10,xxx,xxx
Adam,Maths,20,yyy,yyy
Benn,English,10,xxx,xxx
Benn,Maths,20,yyy,yyy
Benn,Science,30,zzz,zzz
Benn,History,40,zzz,zzz
Zak,English,10,xxx,xxx
Zak,Maths,20,yyy,yyy