Date conversion in row while preserving rest of fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date conversion in row while preserving rest of fields
# 1  
Old 03-17-2011
Date conversion in row while preserving rest of fields

Hi Forum.

I searched the forum for a solution but could not find an exact one to my problem.

I have some records in the file where I would like to convert the last date field to another format while preserving the rest of the other fields.

For example:
Code:
Found:
00000000|2011-Inactive-Segment|2|1|03/15/2011 0:00:00

Expected:
00000000|2011-Inactive-Segment|2|1|15-MAR-2011

Please help.

Thanks.
# 2  
Old 03-17-2011
nawk -F'|' -f pc.awk OFS='|' myFile

pv.awk:
Code:
BEGIN {
   mon="JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC"
   monN=split(mon, monA, "|");
}
{
  n=split($NF,a,"[/ ]")
  $NF=sprintf("%02d-%s-%d",a[2], monA[a[1]+0], a[3])
}
1

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 03-17-2011
This will be much easier on some systems than others. Are you running Linux? do you have GNU awk?
# 4  
Old 03-17-2011
Hi Corona688.

I'm running on an AIX box - how do I check for the awk version?

Thanks Vgersh for your solution - I will give it a try.
# 5  
Old 03-17-2011
I don't think AIX has GNU awk, but it may have a nawk which is newer than the basic awk anyway. But vgersh' clever solution might be minimal enough to work in any. It's sometimes hard to tell what an old awk will throw up on though, so nawk gets suggested first.

On linux this is redundant, awk and nawk and gawk will all be the same full-featured GNU awk.
# 6  
Old 03-17-2011
Vgersh99.

Your code solution was awesome and works great.

Would you mind explaining the awk code portion?

I would like to understand the code better.
# 7  
Old 03-17-2011
Code:
BEGIN {
   mon="JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC"
   # Create an array where mon[1]=JAN, mon[2]=FEB, etc.
   monN=split(mon, monA, "|");
}
{
  # NF is the number of fields, $NF is the very last field.
  # So split the very last field into an array on the / or ' ' chars.
  n=split($NF,a,"[/ ]")
  # convert months 1-12 into JAN-DEC by looking them up in monA.
  # then print the whole mess in the format below, just like C or shell printf.
  $NF=sprintf("%02d-%s-%d",a[2], monA[a[1]+0], a[3])
}
# When the following expression is true, lines are printed.
# 1 is always true, so it prints every line.
1

These 2 Users Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Analyzing last 2 fields of 1 row and 3rd field of next row

I have the following script that will average the last two fields of each row, but im not sure how to include the 3rd field of the following row. An example of the analysis that I need to perform from the input - (66.61+58.01+54.16)/3 awk '{sum=cnt=0; for (i=13;i<=NF;i++) { sum+=$i; cnt++... (1 Reply)
Discussion started by: ncwxpanther
1 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Uneven column to row conversion

Hi Unix Forum, I have a relatively easy question i suppose for which, however, until now i could not find a solution. I am working with a program that will give me an output file similar to the following: A 1 2 3 4 B 1 2 3 4 C 1 (9 Replies)
Discussion started by: Leander
9 Replies

3. Shell Programming and Scripting

Row to Column conversion?

I have a text file with the geneIds separated by space in each line. The number Ids in lines are different. The file is like: abc qwe tyu ghj jkl dfg sdf cvb sdk fgh tyu uio iop tyu rty eru wer rty iop asd sdf dfg fgh zxc I want to format the file like: abc qwe tyu ghj jkl ... (7 Replies)
Discussion started by: sammy777
7 Replies

4. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

5. Shell Programming and Scripting

complicated row to rows conversion

I have a file containing rows with the following format. Field1|Field2|Field3|data1:data data2:data data3:"dataA:data dataB:data" data4:data:data (and so on) I need to format the above row into multiple rows that look like this: Field1|Field2|Field3|data1|data ... (2 Replies)
Discussion started by: newreverie
2 Replies

6. UNIX for Dummies Questions & Answers

Delete a row from a file if one column containing a date is greater than the current system date

Hello gurus, I am hoping someone can help me with the required code/script to make this work. I have the following file with records starting at line 4: NETW~US60~000000000013220694~002~~IT~USD~2.24~20110201~99991231~01~01~20101104~... (4 Replies)
Discussion started by: chumsky
4 Replies

7. Shell Programming and Scripting

Julian date to Calendar date conversion

Hi all, I require to convert julian date to normal calander date in unix for eg julian date=122 now i want corresponding calander date ---------------------------------------- gr8 if give very small command/script and please explain the steps as well(imp) Thanks ... (3 Replies)
Discussion started by: RahulJoshi
3 Replies

8. Shell Programming and Scripting

Identifying specific fields in a Row

Hi, I am new to UNIX. Can some one help me to solve the below. I have a requirement to to identify the specific fields in row and also some part of the field. In my file I have a record as sundra;10.44.48.65;10thstreet TCP packet out of state: First packet isn't SYN;telno:... (3 Replies)
Discussion started by: suneel.mekala
3 Replies

9. Shell Programming and Scripting

To count distinct fields in a row

I have . dat file which contains data in a specific format: 0 3 892 921 342 1 3 921 342 543 2 4 817 562 718 765 3 3 819 562 717 761 i need to compare each field in a row with another field of the same column but different row and cont the... (8 Replies)
Discussion started by: Abhik
8 Replies

10. Shell Programming and Scripting

column to row conversion with additional pattern

Hi there, I've an input file1 as follows: 1001 1002 1003 1004 1005 I would like to have an output file2 as follows: Numbers are 1001/ 1002/ 1003/ 1004/ 1005/ Any help is appreciated. (2 Replies)
Discussion started by: kbirde
2 Replies
Login or Register to Ask a Question