Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

http://pastebin.com/raw.php?i=rCLH4r4M
I have a text file: "hogyaza.txt".
I want to convert the "normal dates" in it [only at the line starting] to unix time. i know how to "convert" one line:

$ ONELINE='2011-01-24 19:13:19ASDF 01 24 19:13:19 router daemon.info dnsmasq-dhcp[1140]: DHCPDISCOVER(br-lan) 192.168.1.201 00:1b:37:a8:74:f1'; date +%s -d "$(echo $ONELINE | awk -F "ASDF" '{print $1}')"
1295892799
$
But i need it in this format:
1295892799ASDF 01 24 19:13:19 router daemon.info dnsmasq-dhcp[1140]: DHCPDISCOVER(br-lan) 192.168.1.201 00:1b:37:a8:74:f1
or better, this format:
1295892799 19:13:19 router daemon.info dnsmasq-dhcp[1140]: DHCPDISCOVER(br-lan) 192.168.1.201 00:1b:37:a8:74:f1
here's the file:
$ cat hogyaza.txt
2011-01-24 19:13:19ASDF 01 24 19:13:19 router daemon.info dnsmasq-dhcp[1140]: DHCPDISCOVER(br-lan) 192.168.1.201 00:1b:37:a8:74:f1
2011-01-24 19:13:19ASDF 01 24 19:13:19 router daemon.info dnsmasq-dhcp[1140]: DHCPOFFER(br-lan) 192.168.1.201 00:1b:37:a8:74:f1
2011-01-24 19:13:19ASDF 01 24 19:13:19 router daemon.info dnsmasq-dhcp[1140]: DHCPREQUEST(br-lan) 192.168.1.201 00:1b:37:a8:74:f1
2011-01-24 19:13:19ASDF 01 24 19:13:19 router daemon.info dnsmasq-dhcp[1140]: DHCPACK(br-lan) 192.168.1.201 00:1b:37:a8:74:f1
$

Does anybody know how to convert the e.g.: "^2011-01-24 19:13:19" dates to unix format in the txt?
Thank you!
p.s.: it's needed on an openwrt router, so no perl :((( no space for it on the flash

share|improve this question
    
What version of AWK (Busybox, gawk, other)? If you have gawk can you use features specific to it or do you need portability? –  Dennis Williamson Jan 24 '11 at 19:41
    
Please don't cross-post. –  Dennis Williamson Jan 24 '11 at 19:47

1 Answer 1

up vote 1 down vote accepted

If you know how to convert one line, then a simple loop will handle all the lines for you:

while read line; do
    orig_date=$(echo "$line" | awk -F "ASDF" '{print $1}')
    new_date=$(date +%s -d "$orig_date")

    echo "$line" | awk -F "ASDF" -vdate=$new_date -vOFS="ASDF" '{$1=date; print}'
done < hogyaza.txt

This gives output that looks like:

1295914399ASDF 01 24 19:13:19 router daemon.info dnsmasq-dhcp[1140]: DHCPDISCOVER(br-lan) 192.168.1.201 00:1b:37:a8:74:f1
1295914399ASDF 01 24 19:13:19 router daemon.info dnsmasq-dhcp[1140]: DHCPOFFER(br-lan) 192.168.1.201 00:1b:37:a8:74:f1
1295914399ASDF 01 24 19:13:19 router daemon.info dnsmasq-dhcp[1140]: DHCPREQUEST(br-lan) 192.168.1.201 00:1b:37:a8:74:f1
1295914399ASDF 01 24 19:13:19 router daemon.info dnsmasq-dhcp[1140]: DHCPACK(br-lan) 192.168.1.201 00:1b:37:a8:74:f1
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.