![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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$
|
|
#3
|
|||
|
|||
|
Thanks Murphy.
But I am working on ksh shell. The substitution doesnt seem to work on ksh. |
|
#4
|
||||
|
||||
|
(removed)
Just saw I missed the year part ... |
|
#5
|
||||
|
||||
|
It works with ksh93 (a.k.a. /usr/dt/bin/dtksh on Solaris).
|
|
#6
|
|||
|
|||
|
A solution with sed:
Code:
sed 's!.*_\(..\)-\(..\)-\(..\)..*!\1/\2/\3!' |
|
#7
|
|||
|
|||
|
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
$
|
|||
| Google The UNIX and Linux Forums |