3

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  /
    /
    
0
3

The following awk program should do the trick:

awk '$1!=lastdate {printf "DATES\n%s %s %s /\n/\n",$2,$3,$4; lastdate=$1}
     {print "WCONPROD"; for (i=5;i<=NF;i++) printf "%s%s",$i,OFS; print "/\n/"}' input.txt

This will compare the value of the first column with the content of the variable lastdate, and if they differ, print the specified "header" section. It will also update lastdate to the newly-found date on the line.

For all lines, it will print the WCONPROD string, then fields 5 to (whatever many you have), separated by the "output field separator" (default is a single space, change with -v OFS='whatever' command-line argument to suit your needs), and followed by the / <newline> / pattern.

Output for your example:

DATES
16 Oct 2007 /
/
WCONPROD
A X1 X2 X3 X4 X5 /
/
DATES
17 Oct 2007 /
/
WCONPROD
A X1 X2 X3 X4 X5 /
/
WCONPROD
B X1 X2 X3 X4 X5 /
/
DATES
18 Oct 2007 /
/
WCONPROD
C X1 X2 X3 X4 X5 /
/
5
  • Thank you very much! for some reason all i am getting is this if through the console. DATES / / WCONPROD / / I will try to troubleshoot thnaks – alex-sh Dec 7 '20 at 11:43
  • 1
    Did the source file originate on a windows machine? If so, you may have to run it through dos2unix – AdminBee Dec 7 '20 at 11:44
  • thank you very much! it worked now! you see who you are dealing with ... – alex-sh Dec 7 '20 at 11:52
  • 1
    No problem, and don't belittle yourself. We all had to learn it at some point, and these types of pitfall are often subtle. If the answer worked for you, you may want to consider accepting it so that others facing a similar issue can find it more easily. – AdminBee Dec 7 '20 at 11:55
  • Thank you very much! this answer is more than i hoped for! – alex-sh Dec 7 '20 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.