The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to sort a field in a file having date values risshanth Shell Programming and Scripting 4 06-04-2008 02:03 AM
how to arrange 3 file to one using awk...? penyu Shell Programming and Scripting 9 01-17-2008 01:26 AM
Compare dates in a field and print the latest date row cvkishore Shell Programming and Scripting 1 08-04-2007 04:58 AM
handling date field mgirinath Shell Programming and Scripting 3 04-03-2007 03:00 PM
re arrange the columns ahmedwaseem2000 Shell Programming and Scripting 0 09-22-2005 11:51 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-28-2006
Registered User
 

Join Date: Feb 2006
Posts: 66
Stumble this Post!
Arrange date in a field

Hi all,

If I have a flat file
ID|Location|Date|Hostname|Age|Sex
1|SFO|06/02/24 12:12:34|hawkeye|35|M
2|LAX|06/02/24 13:12:35|sf49ers|30|M
3|OAK|06/02/25 11:12:36|goraiders|27|F
4|PIT|06/02/25 12:12:37|steeler|35|M

How can I create an output
1|SFO|02/24/2006 12:12:34|hawkeye|35|M
2|LAX|02/24/2006 13:12:35|sf49ers|30|M
3|OAK|02/25/2006 11:12:36|goraiders|27|F
4|PIT|02/25/2006 12:12:37|steeler|35|M

Thanks .
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-28-2006
Registered User
 

Join Date: Feb 2006
Posts: 66
Stumble this Post!
What was wrong with my code ??? it look right to me but it give me the error

datetime="`echo $3 | nawk '{n=split($1, da, "/"); print da[2]"/" da[3]"/20" da[1] " " $2}'`";


this is the error `(' is not expected.

if datetime is work then I solve my problem .

I test this and it works ???
echo '06/02/25 12:05:06' | nawk '{n=split($1, da, "/"); print da[2]"/" da[3]"/20" da[1] " " $2}'

Please knock me where i made mistake .
Thanks,
Reply With Quote
  #3 (permalink)  
Old 02-28-2006
Registered User
 

Join Date: Dec 2005
Location: London
Posts: 222
Stumble this Post!
nawk 'BEGIN {FS="[ |/]"}
{
printf("%s|%s|%s/%s/%s %s",$1,$2,$4,$5,$3,$6);
for(i=7;i<=NF;i++)
{
printf("|%s",$i);
}
printf "\n";
}' filename

if you want to sort based on the date field, then append the following to end of above command.

| sort -t "|" -k 3.3
Reply With Quote
  #4 (permalink)  
Old 02-28-2006
Registered User
 

Join Date: May 2005
Posts: 69
Stumble this Post!
why not try this...
Code:
datetime=`echo "$3" | awk -F"/" '{ print $2"/"substr($3,1,2)"/20"$1 }'`
Reply With Quote
  #5 (permalink)  
Old 02-28-2006
Registered User
 

Join Date: Feb 2006
Posts: 66
Stumble this Post!
Oh, I would like to have the date field change as I want.

mgirinath, this one give me only the date
mahendramahendr, this one give me the out put the same as b4.


I want datetime field change from 06/02/25 11:12:36 to 02/25/2006 11:12:36
but my code got the error ? why syntax error is `(' is not expected
Reply With Quote
  #6 (permalink)  
Old 02-28-2006
Registered User
 

Join Date: Dec 2005
Location: London
Posts: 222
Stumble this Post!
$ more tmp1
2|LAX|06/02/24 13:12:35|sf49ers|30|M
3|OAK|06/02/25 11:12:36|goraiders|27|F
1|SFO|06/02/24 12:12:34|hawkeye|35|M
4|PIT|06/02/25 12:12:37|steeler|35|M

$ nawk 'BEGIN {FS="[ |/]"}
{
printf("%s|%s|%s/%s/%s %s",$1,$2,$4,$5,$3,$6);
for(i=7;i<=NF;i++)
{
printf("|%s",$i);
}
printf "\n";
}' tmp1
2|LAX|02/24/06 13:12:35|sf49ers|30|M
3|OAK|02/25/06 11:12:36|goraiders|27|F
1|SFO|02/24/06 12:12:34|hawkeye|35|M
4|PIT|02/25/06 12:12:37|steeler|35|M

You can clearly see the date format changing to MM/DD/YY...

Is it not what you are looking for ?? what do you mean by same as b4 ?

I think the format of your flat file is different than what you have posted if it is not working....
Reply With Quote
  #7 (permalink)  
Old 02-28-2006
Registered User
 

Join Date: May 2005
Posts: 69
Stumble this Post!
Quote:
Originally Posted by sabercats
Oh, I would like to have the date field change as I want.

mgirinath, this one give me only the date
mahendramahendr, this one give me the out put the same as b4.


I want datetime field change from 06/02/25 11:12:36 to 02/25/2006 11:12:36
but my code got the error ? why syntax error is `(' is not expected
OOPS Sorry...here is date and time...
Code:
date=`echo "$3" | awk -F"/" '{ print $2"/"substr($3,1,2)"/20"$1 }'`
time=`echo "$3" | awk '{ print $2 }'`
echo $date" "$time
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:48 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0