LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-06-2012, 07:15 AM   #1
beckss
Member
 
Registered: Nov 2008
Posts: 31

Rep: Reputation: 0
Need help with string manipulation. (sed or awk)


Hi.
I need a help with transform strings (for example 12Jun2011) to mm/dd/yyyy format.
I can make extraction of Jun by setting case clause, but when I try to use awk with match I can't transfer to it substitution (I mean $month)

stas=12Jun2012
echo $stas | awk 'match($0,"Jun"){print substr($0,0,RSTART-1)}'
--
12
echo $stas | awk 'match($0,"Jun"){print substr($0,RSTART+3)}'
--
2012
But I need use Jun as parameter for next calculation.

Please advice and thanks for all.
Best regards, Stas
 
Old 03-06-2012, 07:20 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,563

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
How about:
Code:
 date -d 12Jun2011 '+%d/%m/%Y'
 
Old 03-06-2012, 07:24 AM   #3
beckss
Member
 
Registered: Nov 2008
Posts: 31

Original Poster
Rep: Reputation: 0
Hi
Thanks for you quick replay.
Not need case, not need addtional calculations.
Thank you again.
Stas.
 
Old 03-06-2012, 07:35 AM   #4
beckss
Member
 
Registered: Nov 2008
Posts: 31

Original Poster
Rep: Reputation: 0
Hi
But addtional question about awk/sed
If in the future I need use parameter (as I described) how I can make this.
Thanks and best regards, Stas.
 
Old 03-06-2012, 07:54 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,563

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Use -v option in awk
 
Old 03-06-2012, 08:58 AM   #6
beckss
Member
 
Registered: Nov 2008
Posts: 31

Original Poster
Rep: Reputation: 0
Can you give me small example for this option.
Thanks Stas
 
Old 03-06-2012, 09:25 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
If the day-of-month is always two characters:
Code:
stas=12Jun2012

day_of_month=${stas:0:2}
mon=${stas:2:3}
yyyy=${stas:5:4}

Last edited by catkin; 03-06-2012 at 12:01 PM. Reason: moth -> month
 
Old 03-06-2012, 10:56 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,563

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Code:
stas=12Jun2012

awk -vdate=$stas 'BEGIN{print date}'
 
  


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
[SOLVED] sed or awk conditional string replacement tsdjim Linux - Newbie 3 01-23-2012 12:14 PM
awk gsub() command - string (column) manipulation - substitution casperdaghost Linux - Newbie 1 03-08-2010 02:12 AM
Using sed/awk to replace a string at a given position in anoopvraj Linux - Newbie 6 05-30-2009 07:59 AM
Remove everything up to the last numbers of a string w/ sed or awk OutThere Linux - General 4 04-23-2009 07:01 PM
grep, sed, awk or tr - searching words in a string hal8000b Programming 2 03-06-2009 08:04 PM


All times are GMT -5. The time now is 12:12 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