Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Arrange date in a field

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-28-2006
sabercats sabercats is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 10 July 2015, 12:44 PM EDT
Posts: 130
Thanks: 1
Thanked 0 Times in 0 Posts
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 .
Sponsored Links
    #2  
Old Unix and Linux 02-28-2006
sabercats sabercats is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 10 July 2015, 12:44 PM EDT
Posts: 130
Thanks: 1
Thanked 0 Times in 0 Posts
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,
Sponsored Links
    #3  
Old Unix and Linux 02-28-2006
mahendramahendr mahendramahendr is offline Forum Advisor  
Registered User
 
Join Date: Dec 2005
Last Activity: 22 August 2008, 8:28 AM EDT
Location: London
Posts: 222
Thanks: 0
Thanked 0 Times in 0 Posts
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
    #4  
Old Unix and Linux 02-28-2006
mgirinath mgirinath is offline
Registered User
 
Join Date: May 2005
Last Activity: 3 November 2008, 2:18 PM EST
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
why not try this...

Code:
datetime=`echo "$3" | awk -F"/" '{ print $2"/"substr($3,1,2)"/20"$1 }'`

Sponsored Links
    #5  
Old Unix and Linux 02-28-2006
sabercats sabercats is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 10 July 2015, 12:44 PM EDT
Posts: 130
Thanks: 1
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #6  
Old Unix and Linux 02-28-2006
mahendramahendr mahendramahendr is offline Forum Advisor  
Registered User
 
Join Date: Dec 2005
Last Activity: 22 August 2008, 8:28 AM EDT
Location: London
Posts: 222
Thanks: 0
Thanked 0 Times in 0 Posts
$ 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....
Sponsored Links
    #7  
Old Unix and Linux 02-28-2006
mgirinath mgirinath is offline
Registered User
 
Join Date: May 2005
Last Activity: 3 November 2008, 2:18 PM EST
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
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

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Check if a date field has date or timestamp or date&timestamp machomaddy Shell Programming and Scripting 1 02-25-2012 10:29 PM
Sorting Date Field with Sort -k :/ Glitch100 Shell Programming and Scripting 17 01-05-2012 12:09 PM
date field manipulation dsravan Shell Programming and Scripting 5 06-23-2010 02:58 AM
Sort two columns in a field, one of them being a date sweta_doshi Shell Programming and Scripting 5 07-28-2009 06:15 AM
handling date field mgirinath Shell Programming and Scripting 3 04-03-2007 06:00 PM



All times are GMT -4. The time now is 08:54 PM.