![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 ###
#
Code:
110/24/74FDUNC 110/24/74FDUNC 110/25/74FDUNC 1 3/ 3/75FDUNC 1 3/ 3/75FDUNC 1 3/ 3/75FDUNC 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. |
|
||||
|
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}
Code:
{
mm=substr($0, 2, 2); dd=substr($0, 5, 2); yy=substr($0, 8, 2)
printf "%02d %02d %02d\n", mm, dd, yy
}
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|