如果列中包含特定列号,则删除该列的一部分。 (该列有一个变量)

时间:2015-06-17 15:37:49

标签: bash awk sed

我有一个像这样的行的csv:

Last,First,A00XXXXXX,1492-01-10,2015-06-17,,Sentence Skills 104,,Elementary Algebra 38,
Last,First,A00XXXXXX,1492-01-10,2015-06-17,,,,Elementary Algebra 101,College Level Math 56
Last,First,A00XXXXXX,1492-01-10,2015-06-17,Reading Comprehension 102,,,,
Last,First,A00XXXXXX,1492-01-10,2015-06-17,,,,Elementary Algebra 118,College Level Math 97

我想删除"阅读理解"但是留下数字,但只有在第6栏中,如果它在任何其他栏中,则不管它。

变量再一次让我感到困惑,我知道如果我知道号码,如何将其从特定列中删除,但是当我不知道号码时,不要删除该号码并留下号码

awk -v old="Reading Comprehension 102" -v new="" -v col=6 '$col==old{$col=new} 1' FS="," OFS="," mergedfile.csv > testmerg.csv

感谢您的帮助,

3 个答案:

答案 0 :(得分:3)

你可以使用这个awk:

awk 'BEGIN{FS=OFS=","} {sub(/Reading Comprehension */, "", $6)} 1' file.csv
Last,First,A00XXXXXX,1492-01-10,2015-06-17,,Sentence Skills 104,,Elementary Algebra 38,
Last,First,A00XXXXXX,1492-01-10,2015-06-17,,,,Elementary Algebra 101,College Level Math 56
Last,First,A00XXXXXX,1492-01-10,2015-06-17,102,,,,
Last,First,A00XXXXXX,1492-01-10,2015-06-17,,,,Elementary Algebra 118,College Level Math 97

答案 1 :(得分:3)

重新使用您的awk变量定义:

$ awk -v old="Reading Comprehension " -v new="" -v col=6 'BEGIN{FS=OFS=","} {sub(old,new,$col)} 1' file
Last,First,A00XXXXXX,1492-01-10,2015-06-17,,Sentence Skills 104,,Elementary Algebra 38,
Last,First,A00XXXXXX,1492-01-10,2015-06-17,,,,Elementary Algebra 101,College Level Math 56
Last,First,A00XXXXXX,1492-01-10,2015-06-17,102,,,,
Last,First,A00XXXXXX,1492-01-10,2015-06-17,,,,Elementary Algebra 118,College Level Math 97

获取由Arnold Robbins撰写的Effective Awk Programming,4th Edition。

答案 2 :(得分:2)

让sed有机会(虽然不是它的域名)

echo "Last,First,A00XXXXXX,1492-01-10,2015-06-17,Reading Comprehension 102,,,," |  
sed -r 's/(([^,]*,){5})Reading Comprehension /\1/'  

最后,首先,A00XXXXXX,1492-01-10,2015-06-17,102 ,,,,

Ed Morton的建议使用变量

old="Reading Comprehension"  
new=""  
col=6  
sed -r 's/(([^,]*,){'"$((col-1))"'})'"$old"' /\1'"$new"'/'