Input file file.txt has dates in month/day/year format.
Awk solution:
- A newbie tutorial on Unix Awk
- Awk if else
- Convert date format in unix using awk and sed
$ cat file.txt
3/4/2013
3/10/2013
10/4/2013
12/10/2012
Required: Add prefix 0 to first and second field if its a single digit. Awk solution:
$ awk 'BEGIN {FS=OFS="/"}
{
if (length($1) == 1) $1="0"$1
if (length($2) == 1) $2="0"$2
{ print }
}' file.txt
Output:
03/04/2013 03/10/2013 10/04/2013 12/10/2012Related posts:
- A newbie tutorial on Unix Awk
- Awk if else
- Convert date format in unix using awk and sed
6 comments:
awk -F/ '{ printf "%02d/%02d/%d\n", $1, $2, $3 }' file.txt
@Derek Schrock, Thanks for the one liner.
hi all, how do i format the date command so it displays day and month in single digits i.e 8 instead of 08 ?? am using the command (in a ksh) ...
Hi Guru's I've a variable "COUNT", which has the total number of records. I need to print the contents of COUNT in 8 digit form to a file. If count is ...
Similar to Derek's code except hard coding in printf
awk -F"/" ' { printf("%02d%s%02d%s%d\n",$1,FS,$2,FS,$3) } ' file.txt
perl -ple 's{((?<=^)|(?<=/)).(?=/)}{0$&}g' file.txt
Post a Comment