庆典计算人的年龄和阅读到CSV文件? [英] bash Calculate age of person and read to csv file?
问题描述
我有三列的csv文件,上面写着姓名,职务code,年龄DD / MM / YY。我想从计算今天的日期和产出每个条目的时代到CSV文件的第四列?我知道AWK是很方便的,但我不知道读取和各列的写入数据,并创建一个新的!
例如
I have a csv file with three columns that reads name,postcode,age dd/mm/yy. I would like to calculate the age of each entry from todays date and output to a fourth column of the csv file? I know awk is handy but i dont know to read and write the data from the various column and create a new one! e.g
name,postcode,dob,age
Dave,ws245f,09/12/2000,13
我有以下输入
`cat estimateAge.csv|awk -F'/|,' '{b=mktime($5" "$4" "$3" 00 00 00 00");a (systime()-b)/(365*24*60*60);a=a==int(a)?a:int(a)+1;print $0","a}`'
和这是在输出
Joe Bloggs,0121 545465650,01/03/1982,31
输出应
Joe Bloggs,0121 545465650,01/03/1982,30
请注意年龄计算不正确,因为李四还没有31至三月
Note the age calc is incorrect as the Joe Bloggs is not yet 31 until March
推荐答案
我觉得这个问题的核心部分不加入新时代的领域进行到底。但年龄计算。
I think the core part of this problem is not adding the new age field to the end. but the age calculation.
试试这个:
awk -F'/|,' '{b=mktime($5" "$4" "$3" 00 00 00 00");a=(systime()-b)/(365*24*60*60);a=a==int(a)?a:int(a)+1;print $0","a}' file
很好,也许我不应该把它们放进一行:
well maybe I should not put them into one line:
awk -F'/|,' '{b=mktime($5" "$4" "$3" 00 00 00 00");
a=(systime()-b)/(365*24*60*60);
a=a==int(a)?a:int(a)+1;print $0","a}' file
测试与您的示例数据:
test with your example data:
kent$ echo "Dave,ws245f,09/12/2000"|awk -F'/|,' '{b=mktime($5" "$4" "$3" 00 00 00 00");a=(systime()-b)/(365*24*60*60);a=a==int(a)?a:int(a)+1;print $0","a}'
Dave,ws245f,09/12/2000,13
你可以看到,我没有检查标题行,一个 NR大于1
检查可以很容易地跳过称号。你可以DIY。
as you can see, I didn't check the title line, a NR>1
check could easily skip the title. You could DIY.
希望它能帮助
这篇关于庆典计算人的年龄和阅读到CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!