How to extract year from date format and find all

2019-08-29 23:20发布

I need to print all years greater than 1900 in a field. I have used to awk to get the field with the date. But I can not figure out how to use awk to pull only the years that are >=1900.

So far I pulled this field by entering this:

awk -F',' '{print $5}' presidents.csv

this gives me these dates

4/03/1893
4/03/1897
14/9/1901
4/3/1909
4/03/1913
4/03/1921
2/8/1923
4/03/1929
4/03/1933
12/4/1945
20/01/1953
20/01/1961
22/11/1963
20/1/1969
9/8/1974
20/01/1977
20/01/1981
20/01/1989
20/01/1993
20/01/2001
20/01/2009
Incumbent

2条回答
\"骚年 ilove
2楼-- · 2019-08-29 23:44

Try this

  #instead of presidents.csv
 echo "
f1,f2,f3,f4,4/03/1893
f1,f2,f3,f4,4/03/1897
f1,f2,f3,f4,14/9/1901
f1,f2,f3,f4,4/3/1909
f1,f2,f3,f4,4/03/1913
f1,f2,f3,f4,4/03/1921" \
| awk -F',' '{print $5}' \
| awk '{split($0,lineArr,"/");if (lineArr[3] > 1900) print $0}'
14/9/1901
4/3/1909
4/03/1913
4/03/1921
查看更多
我想做一个坏孩纸
3楼-- · 2019-08-29 23:52
   awk -F',' '{print $5}' presidents.csv | awk -F '/'  '$3 > 1900 { print $1"/"$2"/"$3 }'
查看更多
登录 后发表回答