The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to find substring position in a given string balareddy Shell Programming and Scripting 2 01-29-2007 07:06 AM
How to get the substring from the string Anshu UNIX for Dummies Questions & Answers 3 06-15-2006 04:52 AM
Removing substring from a string dmilks Shell Programming and Scripting 1 07-28-2005 12:32 PM
getting a substring from a string maradona Shell Programming and Scripting 3 03-09-2005 09:05 AM
can i get a substring from a string? dell9 High Level Programming 4 11-06-2001 06:13 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-24-2008
Registered User
 

Join Date: Jan 2008
Posts: 8
Substring/Instring of a string with datestamp

Hi,

I have just started working on unix today. I want to get the instring of a string (filename).

Eg. JAN_BILS_PRINT_01-01-08.txt

Now i want to extract the datestamp from the file and convert the date to the format mm/dd/yyyy.

How do i do this? Please hel pme with this.

Regards,
Saurabh
Reply With Quote
Forum Sponsor
  #2  
Old 01-24-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,061
using bash shell.

Code:
bash-3.2$ F="JAN_BILS_PRINT_01-01-08.txt"
bash-3.2$ F=${F:15:8}; F=${F//-//}; F=${F:1:5}20${F:6}; echo $F
01/01/2008
bash-3.2$
Reply With Quote
  #3  
Old 01-24-2008
Registered User
 

Join Date: Jan 2008
Posts: 8
Thanks Murphy.

But I am working on ksh shell. The substitution doesnt seem to work on ksh.
Reply With Quote
  #4  
Old 01-24-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,933
(removed)
Just saw I missed the year part ...
Reply With Quote
  #5  
Old 01-24-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,933
Quote:
Originally Posted by bhalotias View Post
Thanks Murphy.

But I am working on ksh shell. The substitution doesnt seem to work on ksh.
It works with ksh93 (a.k.a. /usr/dt/bin/dtksh on Solaris).
Reply With Quote
  #6  
Old 01-24-2008
Moderator
 

Join Date: Feb 2007
Posts: 2,329
A solution with sed:

Code:
sed 's!.*_\(..\)-\(..\)-\(..\)..*!\1/\2/\3!'
Regards
Reply With Quote
  #7  
Old 01-24-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,061
An AWK solution

Code:
$ echo "JAN_BILS_PRINT_01-01-08.txt" | awk '{ printf("%s/%s/20%s", substr($0,16,2), substr($0,19,2), substr($0,22,2)) }'
01/01/2008
$
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0