Switching columns (dates)

Hi everyone,

Our date format is something like DD/MM/YY, after doing an extraction it end up like this (YY/MM/DD):

Code:
120522
120524
120614
120618
120702
120926
121003
130315
130717

Can you please show me how to switch columns in order to obtain a DD/MM/YY, I have tried cat but it didn't work.

Thank you in advance.
 
One way would be to use cut(1), for example in something like this:
Code:
#!/bin/sh

for foo in $*
do
  echo `echo $foo|cut -c 5-6``echo $foo|cut -c 3-4``echo $foo|cut -c 1-2`
done

exit 0
But there might be more elegant solutions.
 
sed(1)() or perl would be the usual way of doing this. Something like
Code:
echo "130924" | perl -ne 's/(\d\d)(\d\d)(\d\d)/$3$2$1/;print'
240913
or, using date(1)()
Code:
date -j -f "%y%m%d" "130924" "+%d%m%y"
240913
 
You can use something like this:
Code:
#!/bin/sh

while read line 
do
  date -j -f "%y%m%d" "${line}" "+%d%m%y"
end

Then use it cat date.txt | myscript.sh

Or in one go:
Code:
#!/bin/sh

cat date.txt | while read line 
do
  date -j -f "%y%m%d" "${line}" "+%d%m%y"
end

http://www.grymoire.com/Unix/Sh.html

Depending on the size of the date.txt file it may be faster to do it all in Perl.
 
Shouldn't it be cat date.txt | ./myscript.sh instead of cat date.txt | myscript.sh because it keeps saying
Code:
-bash: myscript.sh: command not found
and when I execute the script I get the message:
Code:
line 7: syntax error: unexpected end of file

Please help.
 
Well, I didn't quite check the syntax. I just typed it from the top of my head. Have a look on the site I linked.

And for the love of everything that is holy, don't use use bash for such a simple script.
 
Excuse my ignorance but what should I use instead of bash since I wan't to have output in order to check the outcome of the script? Thanks.

P.S. as mentioned in the first post the purpose is to convert a whole file not just a single date.
 
Here's the sed(1) one (your dates are in date.txt file):

cat date.txt |sed 's/\([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)/\3\2\1/p'
Code:
220512
220512
240512
240512
140612
140612
180612
180612
 
Is this related to your [post="233853"]other question[/post]? If so, just change the line
Code:
stat -f "%Sm %N" -t "%Y%m%d%H%M%S" ${file}
to something like
Code:
stat -f "%Sm %N" -t "%d/%m/%y %H:%M:%S" ${file}

For details see strftime(3).
 
Back
Top