0

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

mattdm
  • 6,600
  • 1
  • 26
  • 48
LanceBaynes
  • 3,087
  • 9
  • 29
  • 31

1 Answers1

1

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
larsks
  • 43,623
  • 14
  • 121
  • 180