If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > String formatting?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-16-04, 16:56
JamesAvery22 JamesAvery22 is offline
Registered User
 
Join Date: Jan 2004
Posts: 70
String formatting?

I am given a string like this: "20040920"
It represents 2004 as the year 09 as the month and 20 as the day.

How can I cut that string down to 0409?
Reply With Quote
  #2 (permalink)  
Old 11-16-04, 18:35
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
echo '20040920' | sed -e 's/^....//g'

or [in ksh]:
a='20040920'
a="${a##????}"
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #3 (permalink)  
Old 11-17-04, 08:17
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Wink

Also:
echo '20040920' | cut -c3-6

__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #4 (permalink)  
Old 11-17-04, 14:12
JamesAvery22 JamesAvery22 is offline
Registered User
 
Join Date: Jan 2004
Posts: 70
what if I had jobxxx.ini and I wanted xxx? but I didnt know the length of xxx?

so it could be job1234.ini or job123.ini?

Thanks again

This is how I force it to work:

export JOBNUMBER=`echo $1 | sed -e 's@.inp@@'`
export JOBNUMBER=`echo ${JOBNUMBER} | sed -e 's@job@@'`

Im sure there is a better way though...

Last edited by JamesAvery22; 11-17-04 at 14:24.
Reply With Quote
  #5 (permalink)  
Old 11-17-04, 14:50
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Cool

Try:
Code:
a=$(echo "job1234.ini"|cut -c5-)
a="${a%.*}"

__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #6 (permalink)  
Old 11-18-04, 10:24
JamesAvery22 JamesAvery22 is offline
Registered User
 
Join Date: Jan 2004
Posts: 70
Quote:
Originally Posted by LKBrwn_DBA
Try:
Code:
a=$(echo "job1234.ini"|cut -c5-)
a="${a%.*}"


echo "job134.ini"|cut -c4-

works for me, -c5- cuts the first digit off.

That does work though, thanks

I was hoping for a one liner though =\
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On