Visit Our UNIX and Linux User Community

Solved: Date: Convert m to mm

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Solved: Date: Convert m to mm
# 1  
Old 08-26-2013
Question Solved: Date: Convert m to mm

Hi all,

I've searched through the forum and can't find a thread specific to this question:

I have a CSV file with a date column, the dates I'm given from the original file look like: "m/dd/yyyy" (UNLESS it's a double digit month, like October, where it would then show up as "mm/dd/yyyy"). Is there an easy way where I could add a "0" to the "m" if the month has a single digit?

I know I can grab the column using awk, I just don't know how I can perform the if statement to add the "0" prefix to the month from the value I'd grab (which in this case, would by $4 as far as awk is concerned). Any pointer in the right direction sure would be appreciated, if this is doable.

# 2  
Old 08-26-2013
try awk -F, -vOFS=, '$4 ~ /^.\//{$4="0"$4}1' input

mute@thedoctor:~$ cat input
mute@thedoctor:~$ awk -F, -vOFS=, '$4 ~ /^.\//{$4="0"$4}1' input

This User Gave Thanks to neutronscott For This Post:
# 3  
Old 08-26-2013
Neutron, that was perfect. Thanks so much, I sure appreciate your prompt response Smilie

Just out of curiosity, can you explain to me how that command knows to insert a 0 if the month isn't a double digit? I don't quite follow the logic in that statement. Thank you for your time.

Last edited by mtucker6784; 08-26-2013 at 04:18 PM.. Reason: Asking about clarification of statement---
# 4  
Old 08-26-2013
The secret is here:

$4 ~ /^.\/

which means that field 4 begins (^) with any character (.) (i.e. a number) followed by a slash (escaped) (\/). If field 4 started with two digits, the match would fail.

$ echo ",3/" | awk -F, '$2 ~ /^.\// { print "Yes" }' 
$ echo ",03/" | awk -F, '$2 ~ /^.\// { print "Yes" }'

This User Gave Thanks to Scott For This Post:
# 5  
Old 08-26-2013
Originally Posted by mtucker6784
how that command knows to insert a 0 if the month isn't a double digit?
$4 ~ /^.\//{$4="0"$4}1

$4 ~ Looks only for records number 4 which is the date part
/^.\// The action is only performed in string with one character followed by / char, that excludes 10/ (two digits followed by /)
$4="0"$4 if so, add an extra 0 to the record
1 re-read what just added.

Last edited by Aia; 08-26-2013 at 04:35 PM.. Reason: grammar correction
This User Gave Thanks to Aia For This Post:
# 6  
Old 08-26-2013

Aha! I see. Thank you, Scott. Is there a way to mark this thread as "solved"? Thanks!
# 7  
Old 08-26-2013
Originally Posted by mtucker6784
Is there a way to mark this thread as "solved"?

Previous Thread | Next Thread
Test Your Knowledge in Computers #685
Difficulty: Medium
Reverse Polish notation (RPN), also known as Polish programming notation.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert a date stored in a variable to epoch date

I am not able to pass date stored in a variable as an argument to date command. I get current date value for from_date and to_date #!/usr/bin/ksh set -x for s in server ; do ssh -T $s <<-EOF from_date="12-Jan-2015 12:02:09" to_date="24-Jan-2015 13:02:09" echo \$from_date echo... (7 Replies)
Discussion started by: raj48
7 Replies

2. Shell Programming and Scripting

[Solved] Convert Row To column

Hi Folks, I am using db2 command -> db2 list tablespace show detail Tablespace ID = 10 Name = TSCDDHLMSUM Type = Database managed space Contents = All permanent data.... (5 Replies)
Discussion started by: ckwan
5 Replies

3. Shell Programming and Scripting

Convert date arg to sql date

Doing a bcp load to sybase and need to convert datearg which comes in as 20130501 to 2013-05-01 which is the best way to do this (4 Replies)
Discussion started by: tasmac
4 Replies

4. Shell Programming and Scripting

[Solved] Replace yesterday date with today's date except from the first line

Hello, I have a file like this: 2012112920121130 12345620121130msABowwiqiq 34477420121129amABamauee e7748420121130ehABeheheei in case the content of the file has the date of yesterday within the lines containing pattern AB this should be replaced by the current date. But if I use... (3 Replies)
Discussion started by: Lilu_CK
3 Replies

5. Shell Programming and Scripting

[Solved] need to convert decimal to integer

Using below command awk 'NR==FNR{A=$1;next} {sum+=($2*A)}END{OFMT="%20f";print int(sum)}' Market.txt Product.txt answer:351770174.00000 how to convert this to 351770174. when i try with below command i am getting different result. awk 'NR==FNR{A=$1;next}... (3 Replies)
Discussion started by: katakamvivek
3 Replies

6. UNIX for Dummies Questions & Answers

Unable to convert date into no. using date -d +%s syntax in ksh shell

hi friends, I m trying to write a script which compares to dates. for this i am converting dates into no using synatx as below v2=`date | awk '{print $2,$3,$4}'` v3=`date +%s -d "$v2"` this syntax is working in bash shell ,but fails in ksh shell. please suggest on this. (12 Replies)
Discussion started by: Jcpratap
12 Replies

7. Shell Programming and Scripting

Convert date

If I pass the value in the form YYYY/MM/DD(20100719) how to convert in the form 19\/Jul\/2010 (1 Reply)
Discussion started by: sandy1028
1 Replies

8. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

9. Shell Programming and Scripting

convert Julian date to calender date

Hi, I have script in unix which creates a julian date like 126 or 127 I want convert this julian date into calender date ex : input 127 output 07/may/2007 or 07/05/2007 or 07/05/07 rgds srikanth (6 Replies)
Discussion started by: srikanthus2002
6 Replies

Featured Tech Videos