awk - problems by converting date-format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - problems by converting date-format
# 1  
Old 01-09-2014
awk - problems by converting date-format

Hi

i try to change the date-format from DD/MM/YYYY into MM/DD/YY.
Input-Data:
Code:
...
31/12/2013,23:40,198.00,6.20,2,2,2,1,11580.0,222
31/12/2013,23:50,209.00,7.30,2,2,3,0,4380.0
01/01/2014,00:00,205.90,8.30,2,2,3,1,9360.0,223
...

Output-Data should be:
Code:
...
12/31/13,23:40,198.00,6.20,2,2,2,1,11580.0,222
12/31/13,23:50,209.00,7.30,2,2,3,0,4380.0
01/01/14,00:00,205.90,8.30,2,2,3,1,9360.0,223
...

i try to do it with awk ....
Code:
awk -F'[,|/]' '{ printf "%2d/%02d/%02d,%s,%s,%s\n" $2,$1,$3,$NF}'

but awk "ran out" -- i have still not understand, what awk do with the tripple %s. can someone help me please.

Thanks in advance,
IMPe
# 2  
Old 01-09-2014
"%2d/%02d/%02d,%s,%s,%s\n" is expecting 6 arguments, and you only have 4 (and 11 or 12 fields on the lines).

One way might be to use split rather than having multiple field separators:
Code:
awk '{split($1,d,"/"); $1=d[2]"/"d[1]"/"d[3]; print}' FS=OFS=, file

This User Gave Thanks to CarloM For This Post:
# 3  
Old 01-09-2014
Quote:
Originally Posted by CarloM
"%2d/%02d/%02d,%s,%s,%s\n" is expecting 6 arguments, and you only have 4 (and 11 or 12 fields on the lines).

One way might be to use split rather than having multiple field separators:
Code:
awk '{split($1,d,"/"); $1=d[2]"/"d[1]"/"d[3]; print}' FS=OFS=, file

Hi CarloM,

Thanks a lot for your fast reply. Your "split-version" is working pretty good, but the the Year has still four numbers.
It is possible, to combine it with something like substr(d[3],3,2) ?
Code:
awk '{split($1,d,"/"); $1=d[2]"/"d[1]"/"(substr(d[3],3,2)); print}' FS=OFS=, file

only give me the formated date in MM/DD/YY.
I'm still try to become more familliar with awk and i suppose split will help me several times more in the future.

Thanks,
IMPe

Last edited by IMPe; 01-09-2014 at 09:44 AM..
# 4  
Old 01-09-2014
I suspect d[3] may actually be the rest of the line. Try setting FS & OFS separately.
Code:
awk '{split($1,d,"/"); $1=d[2]"/"d[1]"/"(substr(d[3],3,2)); print}' FS=, OFS=, file

This User Gave Thanks to CarloM For This Post:
# 5  
Old 01-09-2014
Could be done with simple sed statements like:
Code:
sed 's!\(..\)/\(..\)!\2/\1!' yourfile

or
Code:
sed 's!\(...\)\(...\)!\2\1!' yourfile

This User Gave Thanks to ctsgnb For This Post:
# 6  
Old 01-09-2014
CarloM and ctsgnb, to both of you a big thank you.
Quote:
Originally Posted by ctsgnb
Could be done with simple sed statements like:
Code:
sed 's!\(..\)/\(..\)!\2/\1!' yourfile

or
Code:
sed 's!\(...\)\(...\)!\2\1!' yourfile

ctsgnb --> this sed-command will change the "position" of dd/mm to mm/dd but it will not reduce YYYY to YY. finally i want to compare the time-stamp with data from another file, so i need the year-format absolutly like YY.

Thanks,
IMPe
# 7  
Old 01-09-2014
Code:
 sed 's#\(..\)/\(..\)/..\(..\)#\2/\1/\3#' file

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting text files to xls through awk script for specific data format

Dear Friends, I am in urgent need for awk/sed/sh script for converting a specific data format (.txt) to .xls. The input is as follows: >gi|1234|ref| Query = 1 - 65, Target = 1677 - 1733 Score = 8.38, E = 0.6529, P = 0.0001513, GC = 46 fd sdfsdfsdfsdf fsdfdsfdfdfdfdfdf... (6 Replies)
Discussion started by: Amit1
6 Replies

2. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

3. Shell Programming and Scripting

Converting the date format

Hi All, I am new to this forum. Could anyone help me to resolve the following 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... (6 Replies)
Discussion started by: av_sagar
6 Replies

4. UNIX for Advanced & Expert Users

Converting the date format in a flat file

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... (0 Replies)
Discussion started by: av_sagar
0 Replies

5. UNIX for Dummies Questions & Answers

Format Date in awk

I have an assignment in a Linux class I am taking. It has multiple scripts. Basicly when it runs it asks the user name and shows information about the user from the /etc/passwd and /etc/shadow files. The one I need help with is the one that reads the /etc/shadow file. I need to format the date into... (1 Reply)
Discussion started by: kazulk
1 Replies

6. Shell Programming and Scripting

converting Date format

Dear Friends, We have date in output as MM/DD/YYYY format and I want to convert it in DD/MM/YYYY format. e.g. 12/31/2010 to 31/12/2010 We have checked forum for the solution but we are not getting matching query. Please guide us Thanks Anushree. (6 Replies)
Discussion started by: anushree.a
6 Replies

7. Shell Programming and Scripting

Converting windows format file to unix format using script

Hi, I am having couple of files which i used to copy from windows to Linux, so now in case of text files (CTRL^M) appears at end of line. I know i can convert this windows format file to unix format file by running dos2unix. My requirement here is that i want to do it automatically using a... (5 Replies)
Discussion started by: sarbjit
5 Replies

8. Shell Programming and Scripting

converting date format

Hi Please anyone to help to write script to convert date to 'yyyy-mm-dd'(If column is date convert) because my file has large and lot of date colums. If file is small ,using awk command to achive. cat file1|awk 'BEGIN {FS=OFS='|'}... (7 Replies)
Discussion started by: mohan705
7 Replies

9. UNIX for Dummies Questions & Answers

Converting the File Creation Date to a new format

I need to capture a file's creation/modification date and time and convert this to a different format, whilst I can easily get the existing format from a ls -l | awk ' { print $......}' or a cut command I do not know how to convert it to a desired format? I should add that at present the ls -l... (1 Reply)
Discussion started by: barney_clough
1 Replies
Login or Register to Ask a Question