简体   繁体   English

使用awk添加标题和尾部

[英]Add header and trailer using awk

Below is my splitting files using awk, but I want to add header and trailer. 以下是我使用awk分割文件,但我想添加标题和尾部。

How can i do this? 我怎样才能做到这一点?

awk -v DATE="$(date +"%d%m%Y")" -F\, 'BEGIN{OFS=","}NR==1 {h=$0; next} { gsub(/"/, "", $1); file="Assignment_"$1"_"DATE".csv"; print (a[file]++?"":h ORS) $0 > file}' Test_01012020.CSV

You need to print them in the BEGIN and END Section accordingly: 您需要相应地在BEGIN和END部分中打印它们:

$ h=$'Fruit\tColor'
$ t=$'Total\tTotal'
$ cat file 
Mango Yellow
Lemon Green
$ awk -v h="$h" -v t="$t" 'BEGIN{FS=" ";OFS="\t";print h}{print $1,$2}END{print t}' file
Fruit   Color
Mango   Yellow
Lemon   Green
Total   Total

Alternativelly, Alternativelly,
You can avoid the external variables and print header directly inside awk: 您可以避免外部变量,而直接在awk内部打印标题:

$ awk 'BEGIN{FS=" ";OFS="\t";print "Fruit","Color"}{print $1,$2}END{print "Total","Total"}' file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM