AWK/GAWK/TCSH guru's? Help with DATE command


  1. Posts : 19,383
    Windows 10 Pro x64 ; Xubuntu x64
       #1

    AWK/GAWK/TCSH guru's? Help with DATE command


    Are there any GAWK/TCSH guru's about that can give me a hand with this?

    Imagine an ASCII format file with 5 columns of data:

    Code:
    ID,X,Y,Z,Date
    1,10,10,4,3-Aug-2014
    2,10,5,5,1,10-Aug-2015
    .
    .
    .
    .
    I'm trying to change the date format in the last column such that 10-Aug-2015 is represented as 20150810. On a TCSH command line, I can do this by using the date command as follows:

    Code:
    date -d "10-Aug-2015" +%Y%m%d
    The problem is I have several hundred files and each can have up to 100,000 lines. Thus, I need a mechanism to parse column 5 through the date command, such that each instance of the date is formatted as described. I only have the TCSH and GAWK (at a push SED) tools available to solve this, so my plan was to write a TCSH script referencing GAWK to do this.

    I have tried piping the date command through a gawk print statement, but I can't quite work out how to read $5 into the date command. I'd had a look on several UNIX sites, but have yet to find a solution.

    Can anyone help?

    Thanks.
      My Computer


  2. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #2

    Is it not an option to import all these files to a Windows machine and use PowerShell instead, then place them back?

    Btw, line 3 in your example csv file has 6 columns
    Golden said:
    Code:
    ID,X,Y,Z,Date
    1,10,10,4,3-Aug-2014
    2,10,5,5,1,10-Aug-2015
      My Computer


  3. Posts : 19,383
    Windows 10 Pro x64 ; Xubuntu x64
    Thread Starter
       #3

    Thanks for the reply. I wanted to try and keep everything in TCSH since I have other things that are scripted to run on the data, and it makes things easy to manage..

    Anyway, I landed on a solution. Its clunky and dirty, but it works :)

    Code:
    gawk -F, 'FNR != 1 {print $1","$3","$4","$5}' OFS="," COL.TXT | tr -s ',' | tr -d '\r' > 1.JUNK
    gawk -F, 'FNR != 1 {print $8}' OFS="," COL.TXT | tr -s ',' | tr -d '\r' > TEMP.JUNK
    date -f TEMP.JUNK +%Y%m%d > 2.JUNK
    paste -d, 1.JUNK 2.JUNK > DATA.CSV
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 18:00.
Find Us