UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 30th, 2006, 07:12 AM
Verletto Verletto is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2006
Location: Sweden
Posts: 14 Verletto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 56 m 46 sec
Reputation Power: 0
Post Substr command

Hello,

I´m experiencing difficulties trying to make the following lines work properly (ksh):

{ while read LINE ; do
LENGTH=$(expr length "$LINE")
END=`expr $LENGTH - 1`
VAR=$(expr substr "$LINE" 9 "$END" )
...
} < $FILE

I want it to put into VAR the string "$LINE[9,$END]". Even though the command works, it seems to ignore my end range variable "$END" and returns "$LINE[9,$LENGTH]" instead.

Any suggestions about the syntax of the end range for this substring command? Thank you by advance!

Oh and this is my first post so hello to everybody.

Reply With Quote
  #2  
Old May 30th, 2006, 09:02 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 117 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 42 m 44 sec
Reputation Power: 6
Smile

Try this:

while read LINE ; do
LENGTH=$(expr length "$LINE")
let LEN=LENGTH-9
VAR=$(expr substr "$LINE" 9 "$LEN" )
echo $VAR
done << !
12345678ABCD
!

Comments:
1. You do not need the braces for input redirection. The while loop ends in corresponding done. After it you can place the necessary redirection.

2. substr needs the required length as its last argumnet, not the index of the last character required.

3. In ksh you can use the let statement for your calcalations.

4. Have a fun.

Ragards

Reply With Quote
  #3  
Old June 2nd, 2006, 01:56 AM
Verletto Verletto is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2006
Location: Sweden
Posts: 14 Verletto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 56 m 46 sec
Reputation Power: 0
Thank you very much!


Quote:
Originally Posted by zlutovsky
Try this:

while read LINE ; do
LENGTH=$(expr length "$LINE")
let LEN=LENGTH-9
VAR=$(expr substr "$LINE" 9 "$LEN" )
echo $VAR
done << !
12345678ABCD
!

Comments:
1. You do not need the braces for input redirection. The while loop ends in corresponding done. After it you can place the necessary redirection.

2. substr needs the required length as its last argumnet, not the index of the last character required.

3. In ksh you can use the let statement for your calcalations.

4. Have a fun.

Ragards

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Substr command


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT