我收到了以下格式的Feed文件。
employee_id||034100151730105|L|
employee_cd||03410015|L|
dept_id||1730105|L|
dept_name||abc|L|
employee_firstname||pqr|L|
employee_lastname||ppp|L|
|R||L|
employee_id||034100151730108|L|
employee_cd||03410032|L|
dept_id||4230105|L|
dept_name||fdfd|L|
employee_firstname||sasas|L|
employee_lastname||dfdf|L|
|R||L|
.....
是否有任何简单的unix脚本可以提取子字段并创建 CSV ,如下所示..
employee_cd,employee_firstname,dept_name
03410015,pqr,abc
03410032,sasas,fdfd
.....
答案 0 :(得分:0)
我建议使用 awk 解决方案(考虑到dept_name
项始终位于employee_firstname
项之前):
awk -F'|' 'BEGIN{OFS=","; print "employee_cd,employee_firstname,dept_name";}
$1~/employee_cd|employee_firstname|dept_name/{ a[++c]=$3 }
END { for(i=1;i<length(a);i+=3) print a[i],a[i+2],a[i+1] }' file
输出:
employee_cd,employee_firstname,dept_name
03410015,pqr,abc
03410032,sasas,fdfd
解决方案详情:
OFS=","
- 设置输出字段分隔符
$1~/employee_cd|employee_firstname|dept_name/
- 如果第一列与所需项目之一匹配
a[++c]=$3
- 捕获由后续位置索引的项目值
for(i=1;i<length(a);i+=3) print a[i],a[i+2],a[i+1]
- 按三分之一输出项目值
将输出保存为.csv
文件:
the above command > output.csv