# Switching columns (dates)



## abdelilah (Sep 24, 2013)

Hi everyone,

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


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


----------



## fonz (Sep 24, 2013)

One way would be to use cut(1), for example in something like this:

```
#!/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.


----------



## jalla (Sep 24, 2013)

sed(1)() or perl would be the usual way of doing this. Something like

```
echo "130924" | perl -ne 's/(\d\d)(\d\d)(\d\d)/$3$2$1/;print'
240913
```
or, using date(1)()

```
date -j -f "%y%m%d" "130924" "+%d%m%y"
240913
```


----------



## abdelilah (Sep 24, 2013)

How can I make use of it, supposing my file is named date.txt? Thank you.


----------



## SirDice (Sep 24, 2013)

You can use something like this:

```
#!/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:

```
#!/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.


----------



## abdelilah (Sep 24, 2013)

Shouldn't it be `cat date.txt | ./myscript.sh` instead of `cat date.txt | myscript.sh` because it keeps saying

```
-bash: myscript.sh: command not found
```
and when I execute the script I get the message:

```
line 7: syntax error: unexpected end of file
```

Please help.


----------



## SirDice (Sep 24, 2013)

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.


----------



## abdelilah (Sep 24, 2013)

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.


----------



## fonz (Sep 24, 2013)

abdelilah said:
			
		

> what should I use instead of bash


/bin/sh


----------



## _martin (Sep 24, 2013)

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'`

```
220512
220512
240512
240512
140612
140612
180612
180612
```


----------



## jrm@ (Sep 24, 2013)

Is this related to your [post="233853"]other question[/post]?  If so, just change the line 
	
	



```
stat -f "%Sm %N" -t "%Y%m%d%H%M%S" ${file}
```
 to something like 
	
	



```
stat -f "%Sm %N" -t "%d/%m/%y %H:%M:%S" ${file}
```

For details see strftime(3).


----------



## abdelilah (Sep 25, 2013)

Thanks to all of you for you help, it did work.


----------

