我有很多* .csv文件。我要删除特定行之后的内容。我将删除20031231之后的所有行 如何使用Shell脚本的某些行解决此问题?
Test,20031231,000107,0.74843,0.74813
Test,20031231,000107,0.74838,0.74808
Test,20031231,000108,0.74841,0.74815
Test,20031231,000108,0.74835,0.74809
Test,20031231,000110,0.74842,0.74818
Test,20040101,000100,0.73342,0.744318
答案 0 :(得分:2)
快速又脏,但没有其他有关约束的信息
sed '1,/20031231/p;d' YourFile
答案 1 :(得分:0)
如果要使用Shell脚本,最好使用awk
。这将达到目的:
awk 'BEGIN {FS=","} {if ($2 == "20031231") print $0}' input.csv > output.csv
此代码将仅将具有20031231的行写入不同的文件。
答案 2 :(得分:0)
忽略空行和不匹配的数据
awk文件:
$ cat awk.awk
{
if($2<="20031231" && $0!=""){
print $0
}else{
next
}
}
执行:
$ awk -F',' -f awk.awk input
Test,20031231,000107,0.74843,0.74813
Test,20031231,000107,0.74838,0.74808
Test,20031231,000108,0.74841,0.74815
Test,20031231,000108,0.74835,0.74809
Test,20031231,000110,0.74842,0.74818
一个班轮:
$ awk -F',' '{if($2<="20031231" && $0!=""){print $0}else{next}}' input
Test,20031231,000107,0.74843,0.74813
Test,20031231,000107,0.74838,0.74808
Test,20031231,000108,0.74841,0.74815
Test,20031231,000108,0.74835,0.74809
Test,20031231,000110,0.74842,0.74818
答案 3 :(得分:0)
与米勒(http://johnkerl.org/miller/doc/)
mlr --nidx --fs "," filter '$2>20031231' input
给你
Test,20040101,000100,0.73342,0.744318
答案 4 :(得分:0)
使用awk
,请尝试:
awk -F, '$2<=20031231' input.csv