problem extracting substring in korn shell


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers problem extracting substring in korn shell
# 1  
Old 08-15-2007
problem extracting substring in korn shell

hi all, I have read similiar topics in this board, but i didn' t find the posting which is the same with the problem i face.. I try to extract string from the end. i try to do this:

num=abcdefghij
num2=${num:-5}
echo $num2 #this should print the last 5 characters (fghij)
but it doesn;t work.. the error message appears: 0403-011 The specified substitution is not valid for this command.
# 2  
Old 08-15-2007
Code:
num=abcdefghij
num2=$(echo $num | sed 's/.*\(.\{5\}\)$/\1/')
echo num2

# 3  
Old 08-15-2007
One way:

$ num=abcdefghij
$ num2=${num#${num%?????}}
$ echo $num2
fghij
$
# 4  
Old 08-15-2007
thanks bro.. it works.....
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help with Korn Shell script for substring printing

Hi all, I am new to scripting. I have a file with colon separated values called mylist.txt cat mylist.txt 192.123.76.89:lmprod89 162.122.20.28:lmtstserver28 10.80.32.139:hewprod139 . . using our internal os utility (called mvsping) we need to check all these servers if they are... (6 Replies)
Discussion started by: kraljic
6 Replies

2. UNIX for Dummies Questions & Answers

If statement for substring within string (korn)

Hi all, Just looking for a simple if statement that searches for a substring within a varaible, and then performs some function. Problem is that I need it to work in Korn shell $var = *string* does not work in Korn i="xxxxxx00.00yyyyy.zzzzz" want to find 00.00 (2 Replies)
Discussion started by: jgrosecl
2 Replies

3. Shell Programming and Scripting

Korn Shell for pattern matching and extracting

Guys, i'm new to shell scripting. Here's what i need. I need a shell script which would read a file containing only 1 line which never changes. File containts - SQL_Mgd_Svc_ELONMCL54496 |EMEA\brookkev, EMEA\fieldgra, EMEA\tidmamar, EMEA\attfiste, EMEA\baldogar, EMEA\clarkia2, EMEA\conwasha,... (9 Replies)
Discussion started by: butterfly20
9 Replies

4. Shell Programming and Scripting

self-learning! my korn shell problem

i am a beginner i m learning shell by myself i have problem writing a korn shell that takes an absolute pathname to directory and display all ordinary files in the directory ordered by their length. i was thinking use grep ls sort and sed. maybe, i m wrong! can someone tell me? (2 Replies)
Discussion started by: babuda0059
2 Replies

5. Shell Programming and Scripting

korn shell display lenght problem!!! i got stuck on this

Using the KSH, write a shell script called display_by_length, which takes an absolute pathname to a directory and displays all ordinary files in the directory ordered by their length; for each file listed, display the name of the file and its length - nothing else. Extend this script to take an... (1 Reply)
Discussion started by: babuda0059
1 Replies

6. Shell Programming and Scripting

AIX Korn shell sed -s problem

In a Korn shell script I have, cat ../header | sed -e 's/flag1/$cnumb/g' > header.txt The header is short {{Company flag1}} But the result in header.txt is {{Company $cnumb}} The value of $cnumb is 120. I am trying to get the value of $cnumb into the header. I have tried /'$cnumb'/g,... (10 Replies)
Discussion started by: jcarrott
10 Replies

7. Shell Programming and Scripting

korn shell automation problem!

I got the task writting Korn Shell script to automate the tuxedo login so that users neednot have to enter options manually. I have done that using expect tool from the Unix but my manger told me its not secure so you have to do that using Kornshell without using Expect. Here is the way to login to... (0 Replies)
Discussion started by: pareshan
0 Replies

8. Shell Programming and Scripting

Problem with Korn Shell

My Korn shell script below is giving me the following error: ./test.ksh: 0403-057 syntax error at line 7 : 'then' is not matched. Can anyone provide a quick solution as to why the error is occurring? Thanks. #!/usr/bin/ksh typeset -i RecCount typeset -i RecCount2 RecCount=`db2 -x "select... (23 Replies)
Discussion started by: sasaliasim
23 Replies

9. Shell Programming and Scripting

shell script for extracting out the shortest substring from the given starting and en

hi all, i need an urgent help for writing a shell script which will extract out and print a substring which is the shortest substring from the given string where first and last character of that substring will be given by the user. for e.g. if str="abcdpqracdpqaserd" now if the user gives 'a'... (18 Replies)
Discussion started by: pankajd
18 Replies

10. Shell Programming and Scripting

problem with set command in korn shell

I have to copy an array to a temp variable and back after doing some functions. I am trying to see if it is possible to do without while loops.My closest try was set -A temp ${THE_ARRAY} # restore array after some actions set -A THE_ARRAY ${temp} The problem with above is that, the new... (1 Reply)
Discussion started by: vijay1985
1 Replies
Login or Register to Ask a Question