Greetings,
After a few hours of trial and error, I decide to ask for some help.
I am new to AWK and shell script, so please don't laugh
I made the below script, to gather data from some logs and have the output into a CSV file :
#!/bin/sh
#Script to collect Errors
file="/Path/Errors_Report_$(date +%Y%m%d).csv"
DAY=`date +%d -d "Yesterday"`
#Header of the CSV file
echo -e "User_ID,Host_name,Date,Time,Code_302,Code_304,Code_305,Code_1,Code_2,Code_3,Code_7,Code_10,Code_18,Code_101,Code_201,Code_300,Code_301,Code_303,Code_306,Code_14,Code_9999,Code_19,Code_9,Code_11" > /path/Error_Report_$(date +%Y%m%d).csv
grep 'Pattern' Path/File_$(date +%Y%m%d)* |awk -F "|" '{if ($28~/ Pattern /) print substr($1,74,10)","substr($1,85,2)","$28; else if($29~/ Pattern /) print substr($1,74,10)","substr($1,85,2)","$29; else if($30~/ Pattern /) print substr($1,74,10)","substr($1,85,2)","$30}' | cut -d, -f1,2,10 | awk -F "," '{if ($3~/ Error1/) print $1","$2","302; else if ($3~/ Error2/) print $1","$2","304; else if ($3~/ Error3/) print $1","$2","305; else if ($3~/ Error4/) print $1","$2","1; else if ($3~/ Error5/) print $1","$2","2; else if ($3~/ Error6./) print $1","$2","3; else if ($3~/ Error7./) print $1","$2","7; else if ($3~/ Error 8/) print $1","$2","10; else if ($3~/ Error./) print $1","$2","18; else if ($3~/ Error 9/) print $1","$2","101; else if ($3~/ Error 10/) print $1","$2","201; else if ($3~/ Error11/) print $1","$2","300; else if ($3~/ Error12/) print $1","$2","301; else if ($3~/ Error13/) print $1","$2","303; else if ($3~/ Error14/) print $1","$2","306; else if ($3~/ Error15/) print $1","$2","14; else if ($3~/ Error16/) print $1","$2","9999; else if ($3~/ Error17./) print $1","$2","19; else if ($3~/ Error18/) print $1","$2","9; else if ($3~/ Error19/) print $1","$2","11}' | sort | uniq -c | awk -F" " '{print $1","$2}' | awk -F "," '{if($4==302) print $2","$3","$1",,,,,,,,,,,,,,,,,,,"; else if($4==304) print $2","$3",,"$1",,,,,,,,,,,,,,,,,,"; else if($4==305) print $2","$3",,,"$1",,,,,,,,,,,,,,,,,"; else if($4==1) print $2","$3",,,,"$1",,,,,,,,,,,,,,,,"; else if($4==2) print $2","$3",,,,,"$1",,,,,,,,,,,,,,,"; else if($4==3) print $2","$3",,,,,,"$1",,,,,,,,,,,,,,"; else if($4==7) print $2","$3",,,,,,,"$1",,,,,,,,,,,,,"; else if($4==10) print $2","$3",,,,,,,,"$1",,,,,,,,,,,,"; else if($4==18) print $2","$3",,,,,,,,,"$1",,,,,,,,,,,"; else if($4==101) print $2","$3",,,,,,,,,,"$1",,,,,,,,,,"; else if($4==201) print $2","$3",,,,,,,,,,,"$1",,,,,,,,,"; else if($4==300) print $2","$3",,,,,,,,,,,,"$1",,,,,,,,"; else if($4==301) print $2","$3",,,,,,,,,,,,,"$1",,,,,,,"; else if($4==303) print $2","$3",,,,,,,,,,,,,,"$1",,,,,,"; else if($4==306) print $2","$3",,,,,,,,,,,,,,,"$1",,,,,"; else if($4==14) print $2","$3",,,,,,,,,,,,,,,,"$1",,,,"; else if($4==9999) print $2","$3",,,,,,,,,,,,,,,,,"$1",,,"; else if($4==19) print $2","$3",,,,,,,,,,,,,,,,,,"$1","; else if($4==9) print $2","$3",,,,,,,,,,,,,,,,,,,"$1","; else if($4==11) print $2","$3",,,,,,,,,,,,,,,,,,,,"$1}' | awk -F"," '{print "2001,Server1,"$1","$2":00:00,"$3","$4","$5","$6","$7","$8","$9","$10","$11","$12","$13","$14","$15","$16","$17","$18","$19","$20","$21","$22}' >> /Path/Error_Report_$(date +%Y%m%d).csv
The output is like below :
User_ID,Host_name,Date,Time,Code_302,Code_304,Code_305,Code_1,Code_2,Code_3,Code_7,Code_10,Code_18,Code_101,Code_201,Code_300,Code_301,Code_303,Code_306,Code_14,Code_9999,Code_19,Code_9,Code_11
2001,Server1,2016-11-18,00:00:00,,,,,,,,,,,,,,,,,,,,5
2001,Server1,2016-11-18,00:00:00,1,,,,,,,,,,,,,,,,,,,
2001,Server1,2016-11-18,00:00:00,,,,,,,,,,,,,,1,,,,,,
2001,Server1,2016-11-18,00:00:00,,,,,,,,,,,,,,,,,,,42,
2001,Server1,2016-11-18,01:00:00,,,,,,,,,,,,,,,,,,,,3
2001,Server1,2016-11-18,01:00:00,2,,,,,,,,,,,,,,,,,,,
2001,Server1,2016-11-18,01:00:00,,,,,,,,,,,,,,3,,,,,,
2001,Server1,2016-11-18,01:00:00,,,,,,,,,,,,,,,,,,,21,
The plan was to gather all data and have the amount of errors found from a given time in just one line like below, grouping by hours all errors in just one line :
User_ID,Host_name,Date,Time,Code_302,Code_304,Code_305,Code_1,Code_2,Code_3,Code_7,Code_10,Code_18,Code_101,Code_201,Code_300,Code_301,Code_303,Code_306,Code_14,Code_9999,Code_19,Code_9,Code_11
2001,smom01brb.rio.oi.ombr,2016-11-18,00:00:00,1,,,,,,,,,,,,1,,,,,,42,5
And that is what I am not able to achieve with my knowledge, which is very limited by the way.
How could I make the script output the information in the desired format ? All errors from a time set, just in one line, instead of one line from each errors counted ?
Any sugestion to fix the script I did are welcome and any other script which accomplish that is more than welcome.
Thanks for you time in reading my doubt / request and thanks in advanced for all sugestions.