AlphaNumeric String Operations


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers AlphaNumeric String Operations
# 1  
Old 01-05-2007
Data AlphaNumeric String Operations

Hi Smilie

I am writing a ksh

I have a string of general format

A12B3456CD78

the string is of variable length

the string always ends with numbers (here it is 78.. it can be any number of digits may be 789 or just 7)

before these ending numbers are alphabets (here it is CD can even be C alone or CDX ..that is length can be varied)

before the CD is a string of variable length that always ends in digits here A12B3456 ....

i want to extract each of these seperately ..
ie
part one A12B3456
part two CD
part three 78

this is a bit complicated for me Smilie ... plz help me out
# 2  
Old 01-05-2007
Code:
echo A12B3456CD78 | sed "s/\([0-9]*\)$/\\
\1/" | sed "1 s/\([a-zA-Z]*\)$/\\
\1/"

# 3  
Old 01-05-2007
Code:
[/tmp]$ cat ./try.ksh
#! /bin/ksh

in=A12B3456CD78

f3=$(echo $in | tr '[A-Z]' ' ')
f3=${f3##* }
in=${in%$f3}
f2=$(echo $in | tr '[0-9]' ' ')
f2=${f2##* }
f1=${in%$f2}
echo "$f1"
echo "$f2"
echo "$f3"

[/tmp]$ ./try.ksh
A12B3456
CD
78
[/tmp]$

# 4  
Old 01-05-2007
Code:
# !/opt/third-party/bin/zsh
                                                                                 
str="A12B3456CD78"
                                                                                 
only_num=$(echo $str | tr '[A-Za-z]' ' ' | awk '{print $NF}')
only_alpha=$(echo $str | tr '[0-9]' ' ' | awk '{print $NF}')
echo $str $only_num $only_alpha | awk '{ print (substr ($0,0,length($1) - ( length($2) + length($3) ))), "\n", $2, "\n", $3 }'
                                                                                 
exit 0

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generate a string of alphanumeric characters

Hi, I want a script of a code that will allow me to generate all possible combinations of alphanumberica characters of length 12 such that each string will contain numbers and either small or capital letters. For example a string may look like this: 123AB45cd678. (11 Replies)
Discussion started by: faizlo
11 Replies

2. UNIX for Beginners Questions & Answers

Need to compare numbers in alphanumeric string

Hi, I will be having file names like below, 1420SP1.01804 1420SP1.01805D 1420SP1.01805 1420SP1.01806D 1420SP1.01806 1420SP1.01901D 1420SP1.01901 1420SP1.01902D 1420SP1.01902 1420SP1.01903D 1420SP1.01903 1420SP1.01904 1420SP1.01905 From this, I need to list file names which is... (3 Replies)
Discussion started by: Sumanthsv
3 Replies

3. Shell Programming and Scripting

how to insert space in alphanumeric string

Hi everyone, I want help to insert space between digits and letters in a alphanumeric string. INPUT TRY234TER PHY1TYR EXPECTED OUTPUT TRY 234 TER PHY 1 TYR The lines always begin with the letters and the alphabets will be a three letter combination before and after the number. The... (2 Replies)
Discussion started by: kaav06
2 Replies

4. Shell Programming and Scripting

parse a mixed alphanumeric string from within a string

Hi, I would like to be able to parse out a substring matching a basic pattern, which is a character followed by 3 or 4 digits (for example S1234 out of a larger string). The main string would just be a filename, like Thisis__the FileName_S1234_ToParse.txt. The filename isn't fixed, but the... (2 Replies)
Discussion started by: keaneMB
2 Replies

5. Shell Programming and Scripting

String operations

Can you give me some suggestions to split below string into three parts using shell scripts.. Script has to print all alphabets before the number, then number and then all alphabets after the number.. input: chris martin 200173 845747 mech engineer output: chris martin 200173 845747 mech... (6 Replies)
Discussion started by: nram_krishna@ya
6 Replies

6. Shell Programming and Scripting

String operations

Hi All, can you tell me how to drop all preceding zeros in a number. For example, if i have a numbers like 000876838347 and 0000007854762543..how to make them as 876838347 and 7854762543. (6 Replies)
Discussion started by: nram_krishna@ya
6 Replies

7. Shell Programming and Scripting

ksh - test if string contains alphanumeric...

Okay I will let users input spaces as well :) I am having a mental block. I have done a couple of searches but havent found anything that I understand (the likes of :alpha: and awk). Basically I want to give the user an option to enter some text which will go down as a field within a flat... (3 Replies)
Discussion started by: tugger
3 Replies

8. Shell Programming and Scripting

With Regex Spliting the string into Alphanumeric and Numeric part

Hi there With shell script I'm trying to split the string into two parts. One is alphanumeric part, the other one is a numeric part. dummy_postcode_1 = 'SL1' --> res_alpha = 'SL' and res_numeric = '1' dummy_postcode_2 = 'S053' --> res_alpha = 'S' and res_numeric = '053' ... (1 Reply)
Discussion started by: ozgurgul
1 Replies

9. Shell Programming and Scripting

String Operations

Hi All, Query 1 : I want to know how we can get a count of multipe occurrences of a particular expression in another string. For Eg. If my string is " 12" and i need to count the number of spaces preceeding 12 Query 2 : Also want to know how we can change the alignment of a... (9 Replies)
Discussion started by: Rohini Vijay
9 Replies

10. Shell Programming and Scripting

matching alphanumeric string

how to match an alphanumeric string like the following. i have to do like the following. if the input line is the data is {clock_91b} i have to replace that with the string was ("clock_91b") i tried like $line =~ s/the data is\s+\{(+)\}/the string was \(\"$1\"\)/ which... (4 Replies)
Discussion started by: sskb
4 Replies
Login or Register to Ask a Question