The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
reformat date, awk and sed mondrar Shell Programming and Scripting 5 06-05-2008 04:08 PM
Reformat Crontab file alnita Shell Programming and Scripting 1 04-11-2007 03:28 AM
reformat drive with terminal. ajstre24 OS X (Apple) 3 10-24-2006 04:17 AM
Date Reformat F-1 UNIX for Dummies Questions & Answers 2 04-25-2006 04:32 PM
reformat the file CamTu Shell Programming and Scripting 3 03-09-2005 05:01 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-29-2008
climbak climbak is offline
Registered User
  
 

Join Date: May 2008
Posts: 3
help reformat data with awk

I am trying to write an awk program to reformat a data table and convert the date to julian time. I have all the individual steps working, but I am having some issues joing them into one program. Can anyone help me out? Here is my code so far:
Code:
# This is an awk program to convert the dates from time series data to Julian dates.
#
#  1. Remove leading "1" from every line
#
{sub(1,"",$1); #print}
#
# 2. Change single digit months to MM
#
 sub(/^[ \t]+/,0); #print}  #### works up to here if i comment everything following out ####
#
# 3. Change single digit days to DD
#
 if ( NF == 37 ) sub(/^[ \t]+/,0,2); #print}
 else if ( NF == 38 ) sub(/^[ \t]+/,0,2) && sub(/^[ \t]+/,0,3); 
 print}   ### This is what I can't get working so far ###
#
Here is an example of the date part of the data:
Code:
110/24/74FDUNC
110/24/74FDUNC
110/25/74FDUNC
1 3/ 3/75FDUNC
1 3/ 3/75FDUNC
1 3/ 3/75FDUNC
step one as it says in the code removes the leading "1", step 2 adds a "0" to the single digit months and step 3 is suppose to add a "0" before the single digit days and years.
Right now I can get step 1 and 2 to run together, but I can't get step 3 to do anything. There are more steps after this, but not much use trying to get them to work until I get the first stuff to work. Thanks in advance.

Last edited by climbak; 05-29-2008 at 08:18 PM.. Reason: forgot an example of the date part of the data I am trying to change.
  #2 (permalink)  
Old 05-30-2008
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438
You could use the printf format %02d to pad your single digits with 0.

If your (g)awk version supports the FIELDWIDTHS variable:
Code:
BEGIN {FIELDWIDTHS = "1 2 1 2 1 2 1 "}
{printf "%02d %02d %02d\n", $2, $4, $6}
If not:
Code:
{
        mm=substr($0, 2, 2); dd=substr($0, 5, 2); yy=substr($0, 8, 2)
        printf "%02d %02d %02d\n", mm, dd,  yy
}
  #3 (permalink)  
Old 05-30-2008
climbak climbak is offline
Registered User
  
 

Join Date: May 2008
Posts: 3
Thanks for the reply. I actually figured out another way to make it work. Now if I could figure out why another step is adding a 0 before every row.
  #4 (permalink)  
Old 05-30-2008
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438
If you are using awk, just concatenate a 0 in front of your print or printf awk instruction.

Code:
(...)
    print "0" $1, $2, etc...
(...)
    printf "0%s %s", $1, $2
  #5 (permalink)  
Old 05-30-2008
climbak climbak is offline
Registered User
  
 

Join Date: May 2008
Posts: 3
Thanks again ripat. Everything works smoothly now pending writing up the formula to convert to julian time and add that as the first column which should be easy.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0