需要写一个shell,从a文件中提取身份证号码,并计算出年龄、出生日期、性别,输出到b文件中。
shell文件为id.sh,旧身份证文件和新身份证文件在开头配置,例子中分别为id1.txt和id2.txt。
#!/bin/bash
# Usage: ./id.sh
in_file="id1.txt" # old id list file
out_file="id2.txt" # new id list file
pattern_id_15="^((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65)[0-9]{4})([0-9]{2})([
0|1][0-9])([0-3][0-9])([0-9]{3})$"
pattern_id_18="^((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65)[0-9]{4})([1|2][0-9]{
3})([0|1][0-9])([0-3][0-9])([0-9]{3})([0-9xX])$"
while read line
do
id=$line
if [[ $id =~ $pattern_id_15 ]]
then
year=${BASH_REMATCH[3]}
year="19$year"
month=${BASH_REMATCH[4]}
day=${BASH_REMATCH[5]}
sex_id=${BASH_REMATCH[6]}
year_now=`date +%Y`
birthday="$year-$month-$day"
((age=$year_now-$year))
if ((10#$sex_id%2==0))
then
sex="Female"
else
sex="Male"
fi
new_line="$id\t\t$age\t$birthday\t$sex"
echo -e $new_line >> $out_file
elif [[ $id =~ $pattern_id_18 ]]
then
year=${BASH_REMATCH[3]}
month=${BASH_REMATCH[4]}
day=${BASH_REMATCH[5]}
sex_id=${BASH_REMATCH[6]}
year_now=`date +%Y`
birthday="$year-$month-$day"
((age=$year_now-$year))
if ((10#$sex_id%2==0))
then
sex="Female"
else
sex="Male"
fi
new_line="$id\t$age\t$birthday\t$sex"
echo -e $new_line >> $out_file
fi
done < $in_file
测试的id1.txt内容为:
110312560208011
110312581229120
1213212442
11031219610613135x
11031219590829080X
aaaaaaaa
110312198203121803
110312200105020310
生产的id2.txt内容为:
110312560208011 55 1956-02-08 Male
110312581229120 53 1958-12-29 Female
11031219610613135x 50 1961-06-13 Male
11031219590829080X 52 1959-08-29 Female
110312198203121803 29 1982-03-12 Female
110312200105020310 10 2001-05-02 Male
#!/bin/bash
# Usage: ./id.sh
in_file="id1.txt" # old id list file
out_file="id2.txt" # new id list file
pattern_id_15="^((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65)[0-9]{4})([0-9]{2})([
0|1][0-9])([0-3][0-9])([0-9]{3})$"
pattern_id_18="^((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65)[0-9]{4})([1|2][0-9]{
3})([0|1][0-9])([0-3][0-9])([0-9]{3})([0-9xX])$"
while read line
do
id=$line
if [[ $id =~ $pattern_id_15 ]]
then
year=${BASH_REMATCH[3]}
year="19$year"
month=${BASH_REMATCH[4]}
day=${BASH_REMATCH[5]}
sex_id=${BASH_REMATCH[6]}
year_now=`date +%Y`
birthday="$year-$month-$day"
((age=$year_now-$year))
if ((10#$sex_id%2==0))
then
sex="Female"
else
sex="Male"
fi
new_line="$id\t\t$age\t$birthday\t$sex"
echo -e $new_line >> $out_file
elif [[ $id =~ $pattern_id_18 ]]
then
year=${BASH_REMATCH[3]}
month=${BASH_REMATCH[4]}
day=${BASH_REMATCH[5]}
sex_id=${BASH_REMATCH[6]}
year_now=`date +%Y`
birthday="$year-$month-$day"
((age=$year_now-$year))
if ((10#$sex_id%2==0))
then
sex="Female"
else
sex="Male"
fi
new_line="$id\t$age\t$birthday\t$sex"
echo -e $new_line >> $out_file
fi
done < $in_file
测试的id1.txt内容为:
110312560208011
110312581229120
1213212442
11031219610613135x
11031219590829080X
aaaaaaaa
110312198203121803
110312200105020310
生产的id2.txt内容为:
110312560208011 55 1956-02-08 Male
110312581229120 53 1958-12-29 Female
11031219610613135x 50 1961-06-13 Male
11031219590829080X 52 1959-08-29 Female
110312198203121803 29 1982-03-12 Female
110312200105020310 10 2001-05-02 Male
温馨提示:答案为网友推荐,仅供参考
第1个回答 2011-08-08
设号码存放在A列,在B列中年龄(兼判断),C列出生年月,D列性别
以A2单元格的内容为例
B2输入=IF(OR(LEN(A2)=18,LEN(A2)=15),YEAR(TODAY())-YEAR(C2),"身份证号码位数不对")
C2输入=DATE(IF(LEN(A2)=18,MID(A2,7,4),IF(LEN(A2)=15,"19"&MID(A2,7,2),1900)),IF(LEN(A2)=18,MID(A2,11,2),IF(LEN(A2)=15,MID(A2,9,2),1)),IF(LEN(A2)=18,MID(A2,13,2),IF(LEN(A2)=15,MID(A2,11,2),1)))
D2输入=IF(MOD(IF(LEN(A2)=18,MID(A2,17,1),IF(LEN(A2)=15,RIGHT(A2),0)),2)=0,"女","男")追问
以A2单元格的内容为例
B2输入=IF(OR(LEN(A2)=18,LEN(A2)=15),YEAR(TODAY())-YEAR(C2),"身份证号码位数不对")
C2输入=DATE(IF(LEN(A2)=18,MID(A2,7,4),IF(LEN(A2)=15,"19"&MID(A2,7,2),1900)),IF(LEN(A2)=18,MID(A2,11,2),IF(LEN(A2)=15,MID(A2,9,2),1)),IF(LEN(A2)=18,MID(A2,13,2),IF(LEN(A2)=15,MID(A2,11,2),1)))
D2输入=IF(MOD(IF(LEN(A2)=18,MID(A2,17,1),IF(LEN(A2)=15,RIGHT(A2),0)),2)=0,"女","男")追问
大哥 看好了是用shell 不是用Excel... 你说的这些网上有一堆
第2个回答 2011-08-08
你啥意思?追问
给一个身份证号 用shell验证再计算年龄、出生日期、性别。看了网上不少,身份证号有15位及18位的。
追答说清楚点。
追问需要写一个shell,从a文件中提取身份证号码,并计算出年龄、出生日期、性别,输出到b文件中。