Segregate alpha and digits


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Segregate alpha and digits
# 1  
Old 06-24-2013
Segregate alpha and digits

I have a variable containing values like 10d2, 7a7, a8 or d6 (i.e. <digits><alpha><digits>. Out of these leading digits may not be there. Out of this, I want three variables. The first having value Inital digits, the second one will be alpha and the third will be trailing digits. (In the previous example var1 will be 10, var2 will be d and var3 will be 2. In case of a8, var1 will be null, var2 will be a and var3 will be 9)

Any help using sed/awk?
# 2  
Old 06-24-2013
You don't need sed or awk for this, just use a standards conforming shell (such as bash or ksh):
With the following script saved in a file (tester):
Code:
#!/bin/ksh
for i in "$@"
do      var1=${i%%[[:alpha:]]*}
        var3=${i##*[[:alpha:]]}
        var2=${i%$var3}
        var2=${var2#$var1}
        echo "$i -> :$var1:$var2:$var3:"
done

made executable:
Code:
chmod +x tester

and invoked as:
Code:
./tester 123abc456 xyz789 135mno  MIDDLE

the output produced is:
Code:
123abc456 -> :123:abc:456:
xyz789 -> ::xyz:789:
135mno -> :135:mno::
MIDDLE -> ::MIDDLE::

# 3  
Old 06-25-2013
Thanks for the help.

Lesson learnt: First look for a straightforward solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed / awk script to delete the two digits from first 3 digits

Hi All , I am having an input file as stated below 5728 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r03_q_reg_20_/Q 011 611 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r04_q_reg_20_/Q 011 3486... (4 Replies)
Discussion started by: kshitij
4 Replies

2. Shell Programming and Scripting

How to segregate a section from big file?

Hello, I need to know all IP range (ip_prefix), associated with us-west-2 region only from this link - https://ip-ranges.amazonaws.com/ip-ranges.json (it can be opened in wordpad for better visibility) Please suggest, how would I do it. If vi, awk or sed is needed, I have downloaded it on my... (7 Replies)
Discussion started by: solaris_1977
7 Replies

3. Shell Programming and Scripting

Segregate files based on the time they are created

Hi All, I have scenario where I need to zip huge number of DB audit log files newer than 90 days and delete anything older than that. If the files are too huge in number,zipping will take long time and causing CPU spikes. To avoid this I wanted to segregate files based on how old they are and... (2 Replies)
Discussion started by: veeresh_15
2 Replies

4. UNIX for Advanced & Expert Users

Segregate file content using sed backreference

I have some text like EU1BTDAT:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD' EU1BTDATEST:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD' EU1CLOSEDATES:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD' EU1DATED:ASSGNDD... (8 Replies)
Discussion started by: gotamp
8 Replies

5. Shell Programming and Scripting

How can i segregate?

I have this file which contains 91886,000,MiniC2-00,1.9.12,aML,en 91886,000,MiniC2-00,1.9.12,aML,en 91886,000,MiniC2-00,1.9.12,aML,en 91886,000,MiniC2-00,1.9.12,aML,en 91886,000,MiniC2-00,1.9.12,aML,en 91886,000,MiniC2-00,1.9.12,aML,en 91886,000,MiniC2-00,3.0,aML,en... (6 Replies)
Discussion started by: nikhil jain
6 Replies

6. Shell Programming and Scripting

Segregate a number from a String in the log

Hi, I have below lines in the log file. how to segregate and assign '3904' to a variable. XCMM018I Return Code: 8 Feedback: 0 :&PNUM=3904: (3 Replies)
Discussion started by: johnjs
3 Replies

7. Shell Programming and Scripting

Find filenames with three digits and add zeros to make five digits

Hello all! I've looked all over the internet and this site and have come up a loss with an easy way to make a bash script to do what I want to do. I have a file with a naming convention as follows: 2012-01-18 string of words here 123.jpg 2012-01-18 string of words here 1234.jpg 2012-01-18... (2 Replies)
Discussion started by: Buzzman25
2 Replies

8. Shell Programming and Scripting

help: single digits inflated to 2 digits

Hi Folks Probably an easy one here but how do I get a sequence to get used as mentioned. For example in the following I want to automatically create files that have a 2 digit number at the end of their names: m@pyhead:~$ for x in $(seq 00 10); do touch file_$x; done m@pyhead:~$ ls file*... (2 Replies)
Discussion started by: amadain
2 Replies

9. Shell Programming and Scripting

segregate the file based on matching patterns

print 'test' SETUSER 'dbo' go create proc abc as /Some code here/ go SETUSER go print 'test1' SETUSER 'dbo' go Create Procedure xyz as /some code here/ go SETUSER go print 'test2' SETUSER 'dbo' (2 Replies)
Discussion started by: mad_man12
2 Replies
Login or Register to Ask a Question