LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 05-17-2006, 02:11 PM   #1
rwartell
LQ Newbie
 
Registered: May 2006
Posts: 2

Rep: Reputation: 0
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.
 
Old 05-17-2006, 05:33 PM   #2
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
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
 
Old 05-17-2006, 11:59 PM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 623

Rep: Reputation: 364Reputation: 364Reputation: 364Reputation: 364
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].
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I have a question about awk or sed sqp1982 Programming 9 03-28-2006 05:37 AM
Difficult sed/awk question 3saul Linux - Software 2 03-04-2006 02:49 AM
Simple question about sed or awk setianusa Programming 2 09-16-2005 03:57 PM
Simple bash/awk/sed scripting question R00ts Programming 4 04-16-2005 02:55 AM
sed parsing question ncblues Linux - Newbie 5 01-03-2005 06:36 AM


All times are GMT -5. The time now is 03:27 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration