Help changing date format in the nth field


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Help changing date format in the nth field
# 1  
Old 06-16-2017
Help changing date format in the nth field

Hi,

I have two (2) things that I want to do.

First is to change the date format that is in the nth field from MM/DD/YY to YY/MM/DD. Preferably, I wish I know how to make it a 4-digit year but I don't. Problem is I can only assume it is a 20 century

Second is somehow know how to figure out what is the date format on that field before changing it to YYYY/MM/DD.

I would have prefer to use awk/sed or both but I can't work out the syntax to use so I've scripted it instead. I saw some example on using awk to search and replace the nth field but I can't find an example where I have to swap the 3 date values.

Below is the test file that I am wanting to change the date format:

Code:
$: cat xx.out
Objects in the image backed up on 04/21/15 00:30:18 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 05/21/15 00:30:14 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/01/16 00:30:13 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/01/17 00:30:15 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/02/16 00:30:28 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/02/17 00:30:26 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/03/16 00:30:30 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/03/17 00:30:05 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/04/16 00:30:03 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/04/17 00:30:23 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/05/16 00:30:19 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/05/17 00:30:23 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/06/15 00:30:13 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/06/16 00:30:20 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/07/15 00:30:26 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/07/16 00:30:02 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/08/16 00:30:28 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/09/16 00:30:28 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/10/15 00:30:12 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/10/16 00:30:25 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/11/15 00:30:19 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/11/16 00:30:25 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/12/15 00:30:26 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/12/16 00:30:23 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 25/10/16 11:36:14 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 30/09/15 16:13:45 for filespace /test_vol_abcd in node BOS123456:

Below is the script:

Code:
#!/bin/ksh

while read line
do
   txt1=`echo $line | cut -c 1-33`
   txt2=`echo $line | cut -c 44-`
   date_01=`echo $line | cut -c 35-42`
   date_02=`echo $date_01 | awk -F"/" '{ printf "%s/%s/%s" , $3,$2,$1 }'`
   #echo "- date_01 = $date_01 // - date_02 = $date_02"
   echo "$line"
   echo "---> $txt1 $date_02 $txt2"
done < xx.out

And below is the test run of the script:

Code:
$: ./y.ksh
Objects in the image backed up on 04/21/15 00:30:18 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 15/21/04 00:30:18 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 05/21/15 00:30:14 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 15/21/05 00:30:14 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/01/16 00:30:13 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 16/01/21 00:30:13 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/01/17 00:30:15 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 17/01/21 00:30:15 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/02/16 00:30:28 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 16/02/21 00:30:28 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/02/17 00:30:26 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 17/02/21 00:30:26 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/03/16 00:30:30 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 16/03/21 00:30:30 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/03/17 00:30:05 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 17/03/21 00:30:05 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/04/16 00:30:03 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 16/04/21 00:30:03 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/04/17 00:30:23 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 17/04/21 00:30:23 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/05/16 00:30:19 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 16/05/21 00:30:19 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/05/17 00:30:23 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 17/05/21 00:30:23 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/06/15 00:30:13 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 15/06/21 00:30:13 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/06/16 00:30:20 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 16/06/21 00:30:20 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/07/15 00:30:26 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 15/07/21 00:30:26 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/07/16 00:30:02 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 16/07/21 00:30:02 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/08/16 00:30:28 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 16/08/21 00:30:28 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/09/16 00:30:28 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 16/09/21 00:30:28 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/10/15 00:30:12 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 15/10/21 00:30:12 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/10/16 00:30:25 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 16/10/21 00:30:25 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/11/15 00:30:19 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 15/11/21 00:30:19 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/11/16 00:30:25 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 16/11/21 00:30:25 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/12/15 00:30:26 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 15/12/21 00:30:26 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 21/12/16 00:30:23 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 16/12/21 00:30:23 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 25/10/16 11:36:14 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 16/10/25 11:36:14 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 30/09/15 16:13:45 for filespace /test_vol_abcd in node BOS123456:
---> Objects in the image backed up on 15/09/30 16:13:45 for filespace /test_vol_abcd in node BOS123456:

At the moment, I am wanting to know if there is any way I can do what the script is doing by simply using awk?

It would be a bonus if someone can advise how to be able to test a field and work out what is its date format. The reason for this is 'coz if you would check on the test data, the first two lines has a MM/DD/YY format compare to the rest of them. Somewhere along the line, someone has changed the date format Smilie

Any advice much appreciated. Thanks in advance.
# 2  
Old 06-16-2017
Try
Code:
awk '{split ($8, T, "/"); $8=T[3] "/" T[1] "/" T[2]} 1' file2
Objects in the image backed up on 15/04/21 00:30:18 for filespace /test_vol_abcd in node BOS123456:
Objects in the image backed up on 15/05/21 00:30:14 for filespace /test_vol_abcd in node BOS123456:

Bonus question: You can't. How would you tell month from day in 11/10/17?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

2. Shell Programming and Scripting

Changing date format

how do i change the following to show me what the date was 7 days ago? date +%Y-%m-%d (1 Reply)
Discussion started by: SkySmart
1 Replies

3. Shell Programming and Scripting

[Solved] Need help changing a field from MM/DD/YY to DD/MM/YY format

Hi, I need help changing a field from MM/DD/YY to DD/MM/YY format. Suppose a file a.csv. The record is "11/16/09","ABC"," 1","EU","520892414","1","600","31351000","1234567","ANR BANK CO. LTD" "11/16/09","PQR"," 2","EU","520892427","1","600","31351000","5467897","ANR BANK CO.... (4 Replies)
Discussion started by: Gangadhar Reddy
4 Replies

4. Shell Programming and Scripting

Changing the date format

Hi all, I have a file with below data af23b|11-FEB-12|acc7 ad23b|12-JAN-12|acc4 as23b|15-DEC-11|acc5 z123b|18-FEB-12|acc1 I need the output as below:-(date in yyyymmdd format) af23b|20120211|acc7 ad23b|20120112|acc4 as23b|20111215|acc5 z123b|20120218|acc1 Please help me on this.... (7 Replies)
Discussion started by: gani_85
7 Replies

5. UNIX for Advanced & Expert Users

Changing the date format

Hi All, I am new to this forum, could any one help me out in resolving the below issue. Input of the flat file contains several lines of text for example find below: 5022090,2,4,7154,88,,,,,4/1/2011 0:00,Z,L,2 5022090,3,1,6648,88,,,,,4/1/2011 0:00,Z,,1 5022090,4,1,6648,88,,,,,4/1/2011... (0 Replies)
Discussion started by: av_sagar
0 Replies

6. Shell Programming and Scripting

how I can add a constant to a field without changing the file format

Hi, I need to edit a file Protein Data Bank (pdb) and then open that file with the program VMD but when I edit the file with awk, it changes pdb format and the VMD program can not read it. I need to subtract 34 to field 6 ($ 6). this is a pdb file : ATOM 918 N GLY B 103 -11.855 8.675... (8 Replies)
Discussion started by: bio_
8 Replies

7. UNIX for Dummies Questions & Answers

Changing from Excel date format to MySQL date format

I have a list of dates in the following format: mm/dd/yyyy and want to change these to the MySQL standard format: yyyy-mm-dd. The dates in the original file may or may not be zero padded, so April is sometimes "04" and other times simply "4". This is what I use to change the format: sed -i '' -e... (2 Replies)
Discussion started by: figaro
2 Replies

8. Shell Programming and Scripting

Changing date format

Hi, I have a column in a table of Timestamp datatype. For Example : Var1 is the column 2008-06-26-10.10.30.2006. I have Given query as date(var1) and time (var1) I got the file as in the below format : File1: Col1 Col2 2008-06-02|12.36.06 2008-06-01|23.36.35 But the problem is... (7 Replies)
Discussion started by: manneni prakash
7 Replies

9. UNIX for Dummies Questions & Answers

Changing the format of date

Hi, There are lots of threads about how to manipulate the date using date +%m %....... But how can I change the default format of the commad date? $ date Mon Apr 10 10:57:15 BST 2006 This would be on fedora and SunOs. Cheers, Neil (4 Replies)
Discussion started by: nhatch
4 Replies

10. Shell Programming and Scripting

Changing the date format

Hi, I know there is a Q/A section and lots of posts regarding date command here. I am sorry to start a new thread. I am very new to shell scripting (actually i am working on my first program), so please forgive my ignorance. I could not find an answer to my problem else where so i posted it... (10 Replies)
Discussion started by: Dream86
10 Replies
Login or Register to Ask a Question