get substring from string variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers get substring from string variable
# 1  
Old 01-13-2012
get substring from string variable

Hi Dears,

How to use variable in string location of expression ${string:index:length} to get substring from string?

I encounter error "bad substitution" when I use expression ${$var:0:5} to get first 5 characters from $var.

Could you please help me out of this? Thanks!
# 2  
Old 01-13-2012
Code:
 
${var:0:5}

Code:
$ var="TESTING"

$ echo ${var:0:5}
TESTI

# 3  
Old 02-08-2012
I know this is an old post, but here is a cheat sheet on string operations for future searchers: Reference Cards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting substring within string between 2 token within the string

Hello. First best wishes for everybody. here is the input file ("$INPUT1") contents : BASH_FUNC_message_begin_script%%=() { local -a L_ARRAY; BASH_FUNC_message_debug%%=() { local -a L_ARRAY; BASH_FUNC_message_end_script%%=() { local -a L_ARRAY; BASH_FUNC_message_error%%=() { local... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Replace substring from a string variable

Hi, Wish to remove "DR-" from the string variable (var). var="DR-SERVER1" var=`echo $var | sed -e 's/DR-//g'` echo "$var" Expected Output: However, I get the below error: Can you please suggest. (4 Replies)
Discussion started by: mohtashims
4 Replies

3. Shell Programming and Scripting

Extracting substring from variable

Hi All, I'm facing issue wherein I have 2 character string like 'CR' and 'DR' and I want to extract just 1st character but am unable to do it. I tried below options but they are returning me 2nd character only, var="CR" echo ${var:1} returns just "R" echo ${var:0} returns "CR" ... (5 Replies)
Discussion started by: arvindshukla81
5 Replies

4. Shell Programming and Scripting

Help with string and substring also I/O

#!/bin/sh PRINTF=/usr/bin/printf PASSWD=/etc/passwd $PRINTF "Enter a UserID\n" read USERID if ; then $PRINTF "$USERID does not exist, please contact IT service\n" exit 1 fi USERHOME=`grep "^$USERID:" $PASSWD | awk -F : '{print $6}'` USERSHELL=`grep "^$USERID:"... (1 Reply)
Discussion started by: ikeQ
1 Replies

5. Shell Programming and Scripting

get substring from string

Hi All, Problem Description: XML_REP_REQUEST=`CONCSUB "$LOGIN" "SQLAP" "$RESP_NAME" "$USRNM" WAIT="Y" "CONCURRENT" "APPLICATION_SHORT_NAME" "CP_SHORT_NAME"` echo Report Request: $XML_REP_REQUEST --to print value in log file While execution the value of 'XML_REP_REQUEST' is 'Prozess... (5 Replies)
Discussion started by: suman.g
5 Replies

6. UNIX for Dummies Questions & Answers

how to take the substring value into a variable

I am trying something like below. But unfortunately i am missing sthg here. $ HOSTEXT=hostname | awk '{print substr ($0,7)}' $ echo $HOSTEXT $ Suggestioins/Solutions will very much be appreciated. Thanks in advance, best-dyno. (2 Replies)
Discussion started by: best-dyno
2 Replies

7. UNIX for Dummies Questions & Answers

How to get the substring from the string

Hi All, Can anybody help me to get the substring from the given string. (3 Replies)
Discussion started by: Anshu
3 Replies

8. Shell Programming and Scripting

getting a substring from a string

hi all, I am trying to extract SUBSTRINGS out of a string using ksh. The string is "SAPR3K.FD0.FA.TJ.B0010.T050302" I tried using a= `expr substr $stringZ 1 2` which is giving me a syntax error, donno why?? any ideas why its not working?? I also tried echo "welcome" | awk '{... (3 Replies)
Discussion started by: maradona
3 Replies

9. Shell Programming and Scripting

substring of a variable

I'm working in: #!/bin/ksh My question: I have a variable latest_bus_date, which in in the format YYYYMMDD. I would like to create a new variable that is in the format of MMDDYY using the data stored in latest_bus_date. $latest_bus_date='20020304' The new variable should have: 030402... (2 Replies)
Discussion started by: blt123
2 Replies

10. Programming

can i get a substring from a string?

for example, the string a is "abcdefg", can i get a substring "bcd" (from ato a) from string a? thank you (4 Replies)
Discussion started by: dell9
4 Replies
Login or Register to Ask a Question