- 论坛徽章:
- 145
|
本帖最后由 jason680 于 2017-01-20 04:51 编辑
回复 42# mcwolf2000
不多说...不为打赏...直上代码...
$ cat field
port nofield Enable MediaType Duplex speed_Params Speed_Status Link RecentUpTime
# case-sensitive
$ awk 'function p(){s=c="";if(!P)return;for(n=1;n<=t;++n){s=s c (a[k[n]]?a[k[n]]:"null");c="\t"}print s}FNR==NR{t=split($0,k," +");for(n=1;n<=NF;++n){k[$n]=1;s=s c $n;c="\t"};print s;FS=" *: *";OFS=":";next}{gsub("^ +| +$","");$1=$1;if(/^Id/){p();P=a["port"]=$2;next}if(/Params|Status/){g=$1;next};$1=$1;if(NF==2){a[$1]=a[$1"_"g]=$2;next};match($2," *([^ ]+)$",m);gsub(" *[^ ]+$","",$2);d=$1;a[d]=a[d"_"g]=$2;d=m[1];a[d]=a[d"_"g]=$3}END{p()}' field FILE
port nofield Enable MediaType Duplex speed_Params Speed_Status Link RecentUpTime
1 null enabled 1000BaseT half null 10Mbps down 0 years 0 days 0 hours 0 minutes 0 seconds
2 null enabled 1000BaseT half null 10Mbps down 0 years 0 days 0 hours 0 minutes 0 seconds
# not case-sensitive
$ awk 'function p(){s=c="";if(!P)return;for(n=1;n<=t;++n){d=tolower(k[n]);s=s c (a[d]?a[d]:"null");c="\t"}print s}FNR==NR{t=split($0,k," +");for(n=1;n<=NF;++n){k[tolower($n)]=k[$n]=1;s=s c $n;c="\t"};print s;FS=" *: *";OFS=":";next}{gsub("^ +| +$","");$1=$1;if(/^Id/){p();P=a["port"]=$2;next}if(/Params|Status/){g=tolower($1);next};$1=$1;if(NF==2){d=tolower($1);a[d]=a[d"_"g]=$2;next};match($2," *([^ ]+)$",m);gsub(" *[^ ]+$","",$2);d=tolower($1);a[d]=a[d"_"g]=$2;d=tolower(m[1]);a[d]=a[d"_"g]=$3}END{p()}' field FILE
port nofield Enable MediaType Duplex speed_Params Speed_Status Link RecentUpTime
1 null enabled 1000BaseT half auto 10Mbps down 0 years 0 days 0 hours 0 minutes 0 seconds
2 null enabled 1000BaseT half auto 10Mbps down 0 years 0 days 0 hours 0 minutes 0 seconds
$ awk '{s="load data infile \"/home/result_101.csv\" into table switch_101(\""$1"\"";for(n=2;n<=NF;++n)s=s",\""$n"\"";print s");"}' field
load data infile "/home/result_101.csv" into table switch_101("port","nofield","Enable","MediaType","Duplex","speed_Params","Speed_Status","Link","RecentUpTime");
|
|