sed convert date/replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed convert date/replace
# 1  
Old 06-04-2013
sed convert date/replace

Hi,

I wold like to replace this 2012 Mar 02 to this 20120302.

i have already this code:

Code:
ls -lrt | awk '{ print $8$6$7 }'| sed -e s/[0-9]*:[0-9]*/'2013'/g 
  -e s/'Jan'/01/g -e s/'Feb'/02/g -e s/'Mar'/03/g -e s/'Apr'/04/g -e s/'May'/05/g
  -e s/'Jun'/06/g -e s/'Jul'/07/g -e s/'Aug'/08/g -e s/'Sep'/09/g -e s/'Oct'/10/g
  -e s/'Nov'/11/g -e s/'Dec'/12/g

that script give back this output:
Code:
20120328
20120419
20120816
20120829
2012101
2012108
20131212
20130226
20130311
2013043
20130425
20130426
20130430
20130510
20130524
20130531
20130531
20130531
2013062
2013063
2013064

and i need to comlete it to replace this 2012101 to this 20121001.

so i need these:
Code:
20120328
20120419
20120816
20120829
20121001
20121008
20131212
20130226
20130311
20130403
20130425
20130426
20130430
20130510
20130524
20130531
20130531
20130531
20130602
20130603
20130604

I hope, Anyone can help me?

Thank You,
Snayper

ps:sorry for my bad englishSmilie

Last edited by Scott; 06-04-2013 at 06:38 AM.. Reason: Please use code tags
# 2  
Old 06-04-2013
make
Code:
awk '{ print $8$6$7 }'

to
Code:
awk '{ print "%d%s%02d",$8,$6,$7 }'

# 3  
Old 06-04-2013
@vidyadhar85

not work

i get this:
Code:
%d%s%02d
%d%s%02d 2012 Mar 28
%d%s%02d 2012 Apr 19
%d%s%02d 2012 Aug 16
%d%s%02d 2012 Aug 29
%d%s%02d 2012 Oct 1
%d%s%02d 2012 Oct 8
%d%s%02d 09:28 Dec 12
%d%s%02d 15:59 Feb 26
%d%s%02d 10:33 Mar 11
%d%s%02d 10:23 Apr 3
%d%s%02d 16:54 Apr 25
%d%s%02d 13:46 Apr 26
%d%s%02d 13:46 Apr 30
%d%s%02d 17:45 May 10
%d%s%02d 12:33 May 24
%d%s%02d 05:15 May 31
%d%s%02d 10:54 May 31
%d%s%02d 10:55 May 31
%d%s%02d 23:25 Jun 2
%d%s%02d 15:33 Jun 3
%d%s%02d 11:08 Jun 4

maybe important i use solaris.

Thank,
Snayper

Last edited by Scott; 06-04-2013 at 06:39 AM.. Reason: Code tags
# 4  
Old 06-04-2013
oops Smilie my bad it should be printf and not print Smilie
# 5  
Old 06-04-2013
now i get this:

looks like better Smilie
Code:
0002012Mar282012Apr192012Aug162012Aug292012Oct012012Oct080Dec120Feb260Mar110Apr030Apr250Apr260Apr300May100May240May310May310May310Jun020Jun030Jun04

but how can i create this file mask yyyymmdd from that?

Last edited by Scott; 06-04-2013 at 06:39 AM.. Reason: Code tags
# 6  
Old 06-04-2013
just put \n in awk...
Code:
awk '{ print "%d%s%02d\n",$8,$6,$7 }'

What do you mean by your own yyyymmdd format? just pass the output of this to sed that you have written Smilie it can be simplified however you have worked on it so you can keep it that way
This User Gave Thanks to vidyadhar85 For This Post:
# 7  
Old 06-04-2013
@vidyadhar85

Thanks Master!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to replace a parameter(variable) date value inside a text files daily with current date?

Hello All, we what we call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to... (1 Reply)
Discussion started by: pradeepp
1 Replies

2. Shell Programming and Scripting

Sed/awk command to convert number occurances into date format and club a set of lines

Hi, I have been stuck in this requirement where my file contains the below format. 20150812170500846959990854-25383-8.0.0 "ABC Report" hp96880 "4952" 20150812170501846959990854-25383-8.0.0 End of run 20150812060132846959990854-20495-8.0.0 "XYZ Report" vg76452 "1006962188"... (6 Replies)
Discussion started by: Chinmaya Kabi
6 Replies

3. 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

4. Shell Programming and Scripting

[Solved] sed - how replace date in script

Hi All, I have a requirement to find and replace old date with new date value. Below is the scenario: # In the input file, date is MM/DD/YYYY format PREV_DTE=09/15/2013 I want to replace with 09/30/2013. It should look like PREV_DTE=09/30/2013 I am using below sed command :... (4 Replies)
Discussion started by: rockygsd
4 Replies

5. Shell Programming and Scripting

sed command to replace slash in date format only

Hello experts. I haven't been able to find a solution for this using the sed command. I only want to replace the forward slash with string "FW_SLASH" only if there's a number right after the slash while preserving the original number. I have a file containing 2 entries: Original File:... (5 Replies)
Discussion started by: pchang
5 Replies

6. 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

7. 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

8. Shell Programming and Scripting

Using sed command replace date variable in unix

I need to use a shell script, using sed command how to replace date variable value in following format. 04/18/2012 11:38:55 Because the sed is treating the '/' as a parameter instead of the value of a variable, and hence there is the message as sed: command garbled: s/insert/04/18/2012... (9 Replies)
Discussion started by: jannusuresh
9 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
Login or Register to Ask a Question