简体   繁体   中英

AWK keep header of input file in output

i have an input file that looks like this:

datetime,num1,num2
10-01-2015 00:00,1,2
10-01-2015 00:00,4,5

my awk code looks like this:

awk -F"," '{thisid=substr($1,1,10);if(lastid!=thisid)
{print lastid"|"cnt1"|"cnt2;cnt1=0;cnt2=0;lastid=thisid;}
if($2>=40){cnt1+=1;}if($3>=40){cnt2+=1;}
lastid=thisid;}' input > output

i want my output to have the header still:

datetime,num1,num2
10-01-2015,0,0
10-01-2015,0,0

currently output of header just look like this:

datetime|1|1

i dont want to manually print the header.

在左括号({)之前,添加:

NR == 1 {print; next}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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