How to split a value according to character position


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to split a value according to character position
# 1  
Old 05-27-2008
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  
Old 05-27-2008
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  
Old 05-27-2008
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  
Old 05-27-2008
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  
Old 05-27-2008
Excellent, that works perfectly, many thanks!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. UNIX for Beginners Questions & Answers

Split by Position

I am on AIX. I need to use AWK to split the source file based on a character at a certain position. Position 75 with a value of 'R' should go in one output file and the rest should go in another file. I need proper names for the output files. Source FileName : abc_xyz_pqr_a_1_yymmdd... (15 Replies)
Discussion started by: techedipro
15 Replies

3. Shell Programming and Scripting

Split string by position

Hello, I have a file, where the strings in the lines are with fixed length. Like this 1234ABXX234ABC123456 And I want to split that line as it is colored. The output to be like this: 1234A;BX;X234;ABC1234;56 I am trying now with substring, but I believe there is a better way. Can you... (7 Replies)
Discussion started by: apenkov
7 Replies

4. Shell Programming and Scripting

Split file based on distinct value at specific position

OS : Linux 2.6x Shell : Korn In a single file , how can I identify all the Uniqe values at a specific character position and length of each record , and simultaneously SPLIT the records of the file based on each of these values and write them in seperate files . Lets say : a) I want to... (4 Replies)
Discussion started by: kumarjt
4 Replies

5. Shell Programming and Scripting

Find character and Replace character for given position

Hi, i want find the character '-' in a file from position 284-298, if it occurs i need to replace it with 'O ' for the position in the file. How to do that using SED command. thanks in advance, Sara (9 Replies)
Discussion started by: Sara183
9 Replies

6. Shell Programming and Scripting

Get character position within variable

Hi all let's say i have a file named 1234_v2_abcd i need to find the position of the character located after _v, in above case this would be character number 2 the count of the characters before _v can change, but i always have a number after _v can anybody help :) (4 Replies)
Discussion started by: gigagigosu
4 Replies

7. Shell Programming and Scripting

Identify the position of character

Hi, Can some one guide me to identify the position of a character using index in UNIX. I have a record like "17/11/2010 15:16:39;reject;10.44.48.65;daemon alert; src: 10.44.48.112; dst: 172.21.52.88" . I need to identify the value which comes after _src:_ (_ denotes space). I am able to... (15 Replies)
Discussion started by: suneel.mekala
15 Replies

8. Shell Programming and Scripting

insert character in particular position.

I want to insert space in 7th position of all the lines usign vi editor or sed command Input file 12345689010 abcdefghijk . . Output file 123456 89010 abcdef ghijk . . (7 Replies)
Discussion started by: Jairaj
7 Replies

9. Shell Programming and Scripting

URG!! Last position of a character

Hi. If I have files with names like abcd.20080625.1234.abc.XYZ abcd.20080625.1234.abc.XYZW how can I get the XYZ or XYZW part? is there something like an Index() function but in reverse order? I was thinking that if I can get the position of the last dot... (7 Replies)
Discussion started by: mrodrig
7 Replies

10. UNIX for Dummies Questions & Answers

Character position

Hi , I am required to view the fixed postion file very often . I am looking for the utility like this if the file has a one or multile line abcdefghijklmnopqr Utility should make my file look like this 12345678910111213141516-------------------------- abcdefghijk l m n o p q r ... (4 Replies)
Discussion started by: akrathi
4 Replies
Login or Register to Ask a Question