Change Position of word character

Hi,

I have following format in file aaa with content below, and would like to seek help from forumer about how to change and swap the position on 2nd field.

5874957|901125|
95874960|650614|
95874966|870308|

901125 to be changed as 25-11-1990 for eg

Can someone help please ?? :slight_smile:

Regards,

The change from 901125 to 25-11-1990 results in a valid date. What happens if you have 040101 ? Would it become 01-01-2004 or 01-01-1904 ?

Using current century,

date +%C

should be ideal

anyhow OP should clarify

based on the above assumption,

awk -F"|" -v var=`date +%C` '{ printf "%d|%d-%d-%d%d|\n", $1, substr($2, length($2) - 1, 2), substr($2, length($2) - 3, 2), var, substr($2, 0, 2) }' aaa

Plz Give a try on this.

sed 's/\(.*|\)\([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)\(|\)/\1\4-\3-19\2\6/' filename

Note:This will change 04 to 1904,not 2004

Thnx.
Dennis

.* results in a greedy match. If there are more 2 delimiters, then it would pick up the last field.

Your sed statement should look like

sed -e "s/\([^|].*|\)\(..\)\(..\)\(..\)/\1\4-\3-$(date +%C)\2/g" in.txt

if you have Python and you know the language, here's an alternative

#!/usr/bin/python
# assumption: doesn't take care of dates before 1970.
import time
for line in open("file"):
    line=line.strip().split("|")
    first,second = line[0],line[1]
    t = time.strptime(second , "%y%m%d")
    thedate = time.strftime("%d-%m-%Y",t)
    print "%s|%s" % (first,thedate)

output:

# ./test.py
5874957|25-11-1990
95874960|14-06-2065
95874966|08-03-1987