日期:2014-05-17  浏览次数:20965 次

orace 脚本是否规范
有三条命令:awk -F "," '{print $1","$2","$3","$4}' report.csv > event.csv
  awk -F "," '{print $7","$8","$9","$10}' report.csv > remov.csv
  awk -F "," '{print $5","$6","$11}' report.csv > camor.csv


我写了个shell脚本,查看该脚本为一下内容
[localhost@admin]$cat awk.sh
awk -F "," '{print $1","$2","$3","$4}' report.csv > event.csv
awk -F "," '{print $7","$8","$9","$10}' report.csv > remov.csv
awk -F "," '{print $5","$6","$11}' report.csv > camor.csv



想请教下这样写是否规范。。。。。。。。。



 

------解决方案--------------------
当然不规范,作为一个成熟的码农,必须要写出健壮的脚本,异常处理都没有,嘿嘿。。。。

------解决方案--------------------
大哥,你这个shell的问题提到oracle版来了。。。

1)加个判断,当前行的字段数是否满足要求,加上NF>...判断。
2)输出默认也是用空格作为分隔符,print $1, $2 这中间的逗号并不是输出分隔符而只是一个语法。看你想要的应该是逗号分隔吧,用OFS指定输出分隔符。

Perl code
awk -F "," 'NF>=4 {OFS=","; print $1, $2, $3, $4}' report.csv > event.csv
awk -F "," 'NF>=10 {OFS=","; print $7, $8, $9, $10}' report.csv > remov.csv
awk -F "," 'NF>=11 {OFS=","; print $5, $6, $11}' report.csv > camor.csv

------解决方案--------------------
差不多了,满足需求即可