The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Counting position of a character rochitsharma UNIX for Advanced & Expert Users 3 11-27-2007 06:26 PM
Change Position of word character cedrichiu Shell Programming and Scripting 6 03-12-2007 01:52 AM
How to add character in specific position of a string? victorlung Shell Programming and Scripting 5 09-01-2006 11:33 AM
Finding character position in file dhananjaysk Shell Programming and Scripting 5 03-23-2006 11:49 AM
Character position akrathi UNIX for Dummies Questions & Answers 4 10-26-2005 05:06 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-27-2008
michaeltravisuk michaeltravisuk is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 9
How to split a value according to character position

Hello all,

I have a script which picks up a series of large numbers, each of which are actually amalgamations of a series of other numbers. Unfortunately there are no separator characters so I can't use awk -F. I am looking for a way of splitting them into variables according to their character position. For example:

123456789

I would like to split this into a number of variables according their character position in the value, for example:

var1=12
var2=345
var3=678
var4=9

Is there a simple way of splitting a value according to character position with sed or some other program for example?

Many thanks.
  #2 (permalink)  
Old 05-27-2008
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 441
Hi, Did you try variable expansion in your shell?


Code:
number=123456789

echo ${number:0:2}
echo ${number:3:4}

If you have a large number of numbers to process, there is a solution with awk.
  #3 (permalink)  
Old 05-27-2008
michaeltravisuk michaeltravisuk is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 9
Many thanks for the prompt response and advice.

I have tried this and get the following back:


Code:
number=123456789

echo ${number:0:2}

0403-011 The specified substitution is not valid for this co
mmand.

echo ${number:3:4}

0403-011 The specified substitution is not valid for this co
mmand.

Is there an environmental variable or specific shell I need in order to enable this feature with the echo command?

Although there may be a number of files to process, I am currently picking them up using a for loop and putting them into a variable to process here.
  #4 (permalink)  
Old 05-27-2008
siba.s.nayak siba.s.nayak is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 166
This can be done using cut command.

Please try with this.

str="123456789"
echo $str > temp.txt
var1=`cut -c1-2 temp.txt`
var2=`cut -c3-5 temp.txt`
var3=`cut -c6-9 temp.txt`
echo $var1
echo $var2
echo $var3

Regards,
Siba
  #5 (permalink)  
Old 05-27-2008
michaeltravisuk michaeltravisuk is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 9
Excellent, that works perfectly, many thanks!
Closed Thread

Bookmarks

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0