以下命令完全符合我的要求 它检查列中是否存在字符串,如果不存在则添加逗号。
awk -F, -v OFS=, '!/Reading Comprehension/ { $5 = $5 "," } 1' somefile.csv \
| awk -F, -v OFS=, '!/Sentence Skills/ { $6 = $6 "," } 1' \
| awk -F, -v OFS=, '!/Arithmetic/ { $7 = $7 "," } 1' \
| awk -F, -v OFS=, '!/College Level Math/ { $8 = $8 "," } 1' \
| awk -F, -v OFS=, '!/Elementary Algebra/ { $9 = $9 "," } 1' > endfile.csv
如何使用此连接/管道命令并将其转换为awk循环?
我在想这样的事情:
awk -F, OFS=,
i = Reading Comprehension, Sentence Skills, Arithmetic, College Level Math, Elementary Algebra
j = 5,6,7,8,9
{for ((i<=NF; j<=NF) '!/i/ { $j = $j "," } 1')}
示例输入:
last,first,A00XXXXXX,1888-01-01,2015-05-13,Elementary Algebra 34
last,first,A00XXXXXX,1888-01-01,2015-05-13,Reading Comprehension 97,Sentence Skills 104,College Level Math 76,Elementary Algebra 115
last,first,A00XXXXXX,1888-01-01,2015-05-13,Sentence Skills 104,Arithmetic 08,College Level Math 76,Elementary Algebra 115
last,first,A00XXXXXX,1888-01-01,2015-05-13,College Level Math 76,Elementary Algebra 115
last,first,A00XXXXXX,1888-01-01,2015-05-13,Sentence Skills 104,Elementary Algebra 115
示例输出:
last,first,A00XXXXXX,1888-01-01,2015-05-13,,,,,Elementary Algebra 34
last,first,A00XXXXXX,1888-01-01,2015-05-13,Reading Comprehension 97,Sentence Skills 104,Arithmetic 08,College Level Math 76,Elementary Algebra 115
last,first,A00XXXXXX,1888-01-01,2015-05-13,,Sentence Skills 104,,College Level Math 76,Elementary Algebra 115
last,first,A00XXXXXX,1888-01-01,2015-05-13,,,,College Level Math 76,Elementary Algebra 115
last,first,A00XXXXXX,1888-01-01,2015-05-13,,Sentence Skills 104,,,Elementary Algebra 115
我仍然在学习AWK,对于如何进行awk循环只有部分了解。
任何帮助都会受到赞赏,对答案的解释真的很棒。
答案 0 :(得分:3)
这个答案假定每个类别中的第一个单词是唯一的:
awk -F, -v OFS=, '
{
delete val # clear the previous values if any
for (i=6; i<=NF; i++) {
split($i, a, " ")
val[a[1]] = $i # a[1] is the first space-separated word
}
print $1,$2,$3,$4,$5, val["Reading"], # null values are OK
val["Sentence"],
val["Arithmetic"],
val["College"],
val["Elementary"]
}
' input
答案 1 :(得分:1)
您可以执行以下操作,但由于我们在每个缺失的列中更改了NF,因此需要进行一些awk数组跳舞。
BEGIN
{
FS=OFS=","
n=split("Reading Comprehension,Sentence Skills,Arithmetic,College Level Math,Elementary Algebra",c,",")
}
{
delete a;
for (i=6;i<=NF;i++) {
for (j=1;j<=n;j++) {
if ($i ~ c[j]) a[j]=$i
}
}
print $1,$2,$3,$4,$5,a[1],a[2],a[3],a[4],a[5]
}