Change date 08212010 to 20100821


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change date 08212010 to 20100821
# 1  
Old 08-22-2010
Change date 08212010 to 20100821

Hi,

I am working in sh shell. I have a file containing dates in the following format:

08212010
08222010
08232010
...

I would like to quickly convert it to:
20100821
20100822
20100823

It would be great if there is a very efficient way to perform this task using a single line command most preferably.

Thanks in advance.
# 2  
Old 08-22-2010
This should work:
Code:
perl -pe 's/(.{4})(.*)/\2\1/' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 08-22-2010
Great! thanks a lot! Wondering if same possible using sed with any minor change.
# 4  
Old 08-22-2010
Quote:
Originally Posted by axed
Great! thanks a lot! Wondering if same possible using sed with any minor change.
Sed needs alot of escaping..
Code:
sed 's/\(.\{4\}\)\(.*\)/\2\1/' file

Which is one of the reasons to use Perl Smilie
This User Gave Thanks to bartus11 For This Post:
# 5  
Old 08-22-2010
Thanks again so much.
# 6  
Old 08-22-2010
Using the Bash shell:

Code:
$
$ cat f3
08212010
08222010
08232010
$
$
$ while read D; do echo ${D:4}${D:0:4}; done <f3
20100821
20100822
20100823
$
$

And yet another way in Perl:

Code:
$
$ cat f3
08212010
08222010
08232010
$
$ perl -lne 'print reverse unpack A4A4' f3
20100821
20100822
20100823
$
$

tyler_durden
# 7  
Old 08-22-2010
posixy
Code:
while read d; do
  echo ${d#????}${d%????}
done < infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to change existing date to current date in a filename?

Suppose i have a list of files in a directory as mentioned below 1. Shankar_04152019_ny.txt 2. Gopi_shan_03122019_mi.txt 3. Siva_mourya_02242019_nd.txt .. . . . . 1000 . Jiva_surya_02282019_nd.txt query : At one shot i want to modify the above all filenames present in one path with... (4 Replies)
Discussion started by: Shankar455
4 Replies

2. Shell Programming and Scripting

Change Date Input :-

I have Below Input :- X1=03 ### Hour Y1=20160405 ## Date Z1=3 ## I want to Back 3 Hour Output List=03 02 01 Y1=20160405 Input:- X1=02 ### Hour Y1=20160405 ## Date Z1=4 ## I want to Back 4 Hour Output:- List=02 01 24 23 Y1=20160404 ### Date Will Change (13 Replies)
Discussion started by: asavaliya
13 Replies

3. Shell Programming and Scripting

Change the content of files but not change the date

I have 100 files in a directory , all the files have a word "error" and they are created in different date . Now I would like to change the word from "error" to "warning" , and keep the date of the files ( that means do not change the file creation date after change the word ) , can advise what can... (7 Replies)
Discussion started by: ust3
7 Replies

4. Shell Programming and Scripting

Change the content of files but not change the date

I have 100 files in a directory , all the files have a word "error" and they are created in different date . Now I would like to change the word from "error" to "warning" , and keep the date of the files ( that means do not change the file creation date after change the word ) , can advise what can... (0 Replies)
Discussion started by: ust3
0 Replies

5. UNIX for Dummies Questions & Answers

Date change to tomorrow

I have this code in a script that says it is calculating current date: cur_date=`date -u +%m/%d/%Y` If the script is run today during the day, it returns 12/11/2008 If the script is run today during the night, it returns 12/12/2008 Why does it return tomorrow's date if it was run at... (2 Replies)
Discussion started by: mkoay
2 Replies

6. UNIX for Dummies Questions & Answers

Change date to uppercase

Hello, I'm trying to take a 3 character date and change it to uppercase, does anyone know how to do that? Currently, all commands that I know of for changing strings/variables to uppercase change the command itself to uppercase, not the output. Here is what I've tried: date="date... (2 Replies)
Discussion started by: tekster757
2 Replies

7. Shell Programming and Scripting

How to Change the Format of a Date

Hi All, this is my second post, last post reply was very helpful. I have a data that has date in DD/MM/YYYY (07/11/2008) format i want to replace the backslash by a dot(.) so that my awk script can read it inside the C shell script that i have written. i want to change 07/11/2008 to... (3 Replies)
Discussion started by: asirohi
3 Replies

8. Shell Programming and Scripting

Change of date format

I want to chnage the date format from the file format like below to WT;T15D;0000007208;;20080401;3;0;0;3;;B;ZZZZZZ; WT;T25D;0000007208;;20080401;6;0;0;6;;B;ZZZZZZ; WT;T5D;0000007208;;20080401;123;0;0;123;;B;ZZZZZZ; to WT;T15D;0000007208;;04/01/200804;3;0;0;3;;B;ZZZZZZ;... (2 Replies)
Discussion started by: svenkatareddy
2 Replies

9. UNIX for Dummies Questions & Answers

Move A File With Same Date,don't Change The Desitination Dir Date

Assume, I created one file three years back and I like to move the file to some other directory with the old date (Creation date)? Is it possible? Explain? (1 Reply)
Discussion started by: jee.ku2
1 Replies

10. UNIX for Dummies Questions & Answers

change date on OpenBoot

hi all, I wonder how to change the date and timezone in OpenBoot prompt. Appreciate any idea. Thanks in advance (1 Reply)
Discussion started by: andrec
1 Replies
Login or Register to Ask a Question