LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Sed or Awk question, looking for parsing help (https://www.linuxquestions.org/questions/linux-software-2/sed-or-awk-question-looking-for-parsing-help-445772/)

rwartell 05-17-2006 02:11 PM

Sed or Awk question, looking for parsing help
 
Hi, I have the following problem. I need a script that will find in a .csv file dates of format
Mon Mar 05 18:34:38 1982
and change them to this format inside the file:
03051982
Can this be done with sed? Does it have the ability to find a something using regular expressions and then modify it rather than replacing it? Or must this be done in awk? Any answers would be awesome. Thanks a lot.

berbae 05-17-2006 05:33 PM

A possibility, though maybe not the best one:
Code:

sed '{s/[A-Z][a-z]\{2\} Jan \([0-9]\{2\}\) [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} \([0-9]\{4\}\)/01\1\2/g}
{s/[A-Z][a-z]\{2\} Feb \([0-9]\{2\}\) [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} \([0-9]\{4\}\)/02\1\2/g}
{s/[A-Z][a-z]\{2\} Mar \([0-9]\{2\}\) [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} \([0-9]\{4\}\)/03\1\2/g}
{s/[A-Z][a-z]\{2\} Apr \([0-9]\{2\}\) [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} \([0-9]\{4\}\)/04\1\2/g}
{s/[A-Z][a-z]\{2\} May \([0-9]\{2\}\) [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} \([0-9]\{4\}\)/05\1\2/g}
{s/[A-Z][a-z]\{2\} Jun \([0-9]\{2\}\) [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} \([0-9]\{4\}\)/06\1\2/g}
{s/[A-Z][a-z]\{2\} Jul \([0-9]\{2\}\) [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} \([0-9]\{4\}\)/07\1\2/g}
{s/[A-Z][a-z]\{2\} Aug \([0-9]\{2\}\) [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} \([0-9]\{4\}\)/08\1\2/g}
{s/[A-Z][a-z]\{2\} Sep \([0-9]\{2\}\) [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} \([0-9]\{4\}\)/09\1\2/g}
{s/[A-Z][a-z]\{2\} Oct \([0-9]\{2\}\) [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} \([0-9]\{4\}\)/10\1\2/g}
{s/[A-Z][a-z]\{2\} Nov \([0-9]\{2\}\) [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} \([0-9]\{4\}\)/11\1\2/g}
{s/[A-Z][a-z]\{2\} Dec \([0-9]\{2\}\) [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} \([0-9]\{4\}\)/12\1\2/g}' file.csv >newfile.cvs

Regards

firstfire 05-17-2006 11:59 PM

I think Perl is better solution. Use `map' datatype, for example %monts={"Jan" => 01, "Feb" => 02} or smth. like this.
Regular expression for whole date string: /\w* \w* \d{2} \d{2}:\d{2}:\d{2} \d{4}/.
You can refer to month as $2 and substitute month number with $months[$2].


All times are GMT -5. The time now is 10:22 PM.