I am a complete novice with AWK and trying to prepare input for another program with the AWK. I have data as per table below and I need to extract data from columns 5 to 10. Also, for each new date encountered (as stated in colums 1 and 2-4) I need to write a preceding command (DATES
), please see below 'desired output'.
Input example (
input.txt
) - Note that the first commented line is for illustrative purposes only, and not present in the actual data.# 1 2 3 4 5 6 7 8 9 10 20071016 16 Oct 2007 A X1 X2 X3 X4 X5 20071017 17 Oct 2007 A X1 X2 X3 X4 X5 20071017 17 Oct 2007 B X1 X2 X3 X4 X5 20071018 18 Oct 2007 C X1 X2 X3 X4 X5
Desired output (
out.txt
)DATES 16 Oct 2007 / / WCONPROD A X1 X2 X3 X3 X4 X5 / / DATES 17 Oct 2007 / / WCONPROD A X1 X2 X3 X3 X4 X5 / / WCONPROD B X1 X2 X3 X3 X4 X5 / / DATES 18 Oct 2007 / / WCONPROD C X1 X2 X3 X3 X4 X5 / /
What I have problem with is to print DATES
only once for each date if there are several lines with the same date. I found and modified the following AWK lines to get this far but i am struggling to have dates only once for each (new) date. I have made another column in an attempt to make AWK to look/compare column 1 with the previous but not sure how to implement this.
awk
command tried{ printf "%-s\n%-s %s %s %s\n%s\n", "DATES", $2,$3,$4,"/","/" print "\nWCONPROD\n" printf " %-s \t%s %s %s %s %s %s %s\n%s\n\n", $5, $6, $7, $8, $8, $9, $10, "/","/" }
- If I run these AWK commands then I have
DATES
printed for each row while as shown below:DATES 16 Oct 2007 / / WCONPROD A X1 X2 X3 X3 X4 X5 / / DATES 17 Oct 2007 / / WCONPROD A X1 X2 X3 X3 X4 X5 / / ## i want to remove (not to print) these/next 3 lines as this date was already declared earlier DATES 17 Oct 2007 / / WCONPROD B X1 X2 X3 X3 X4 X5 / / DATES 18 Oct 2007 / / WCONPROD C X1 X2 X3 X3 X4 X5 / /