LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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: 39

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
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

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

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: 39

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
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

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

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: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

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

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


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:06 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration