substr from a string in Shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting substr from a string in Shell script
# 1  
Old 08-05-2008
substr from a string in Shell script

Shell Scripting Gurus,
I am having a hard time Smilie trying to figure out what I am doing wrong in my script. Below is the script snippet. It gives an error when it tries to execute the expression.

a=`expr substr $stringZ 5 10`


#!/bin/bash
echo "Hello"
stringZ="abcABC123ABCabc"
echo $stringZ
echo "Length is ${#stringZ}"
echo `expr "$stringZ" : '.*'`
a=`expr substr $stringZ 5 10`
echo $a

output :

Hello
abcABC123ABCabc
Length is 15
15
expr: syntax error

$


What am I doing wrong? Why is it giving syntax error when it executes
a=`expr substr $stringZ 5 10`

Can one of you gurus out there help me fix this?

Thanks a lot!
Ajay.
# 2  
Old 08-05-2008
Dunno why you get the syntax error from expr, but you could use bash's builtin substring facility.

Code:
a=${stringZ:5:10}

# 3  
Old 08-05-2008
I think this should rather have been addressed by era,
the herder of Randal's useless use of cat awards?
I would here say, contender for useless use of echo and backticks awardSmilie
Quote:
echo `expr "$stringZ" : '.*'`
But I noticed one difference.
The nifty bash substr expansion seems to start counting chars from naught
(which seems logical to programmers)
whereas the expr substr seems indexing from 1 (which I would say is more Fortran style, if I remember correctly)
Code:
$ str=abcABC123ABCabc
$ echo ${str:5:10}
C123ABCabc
$ expr substr $str 5 10
BC123ABCab

# 4  
Old 08-06-2008
Thanks a lot!!!

Era and Buffoonix,

Thanks a lot! Your inputs helped me in fixing my problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Substr/Instr in shell script or extract part of text

Hi, I need to extract part of a text to two variables text is "PL/SQL procedure successfully completed. ERROR ----------------------------------------------------------------- Test Error Message PLUSVAR ---------- 1" I want "Test Error Message" in one variable and "1" in another variable.... (11 Replies)
Discussion started by: vedavrath
11 Replies

2. Shell Programming and Scripting

Cut string substr

friends as I can cut the first three characters in a string example: 400_FACTURACION_CANJES_20151217.txt Result 400 (2 Replies)
Discussion started by: tricampeon81
2 Replies

3. Shell Programming and Scripting

How to get the last value from a string in shell script?

I have to get the last value from a string, below is the example string String -> Here is the test string 12233 O/P -> 12233 String -> Hello world 500 O/P -> 500 String -> 300 O/P -> 300 Please help (2 Replies)
Discussion started by: vel4ever
2 Replies

4. UNIX for Dummies Questions & Answers

Comparing a String variable with a string literal in a Debian shell script

Hi All, I am trying to to compare a string variable with a string literal inside a loop but keep getting the ./testifstructure.sh: line 6: #!/bin/sh BOOK_LIST="BOOK1 BOOK2" for BOOK in ${BOOK_LIST} do if then echo '1' else echo '2' fi done Please use next... (1 Reply)
Discussion started by: daveu7
1 Replies

5. Shell Programming and Scripting

Help on shell script (string operations)

Hey everyone. So the background of the problem is that the ps3 does not support the mkv container, but DOES support the avi one. Here is the script to convert one file with the name hardcoded in: #!/bin/sh mencoder -oac... (2 Replies)
Discussion started by: wua05
2 Replies

6. Shell Programming and Scripting

copy substr in existing string in Perl

Any clue to write something to a particular location in Perl? Suppose $line = ‘abc cde 1234” How to write ( example string "test") on location 4 without parsing the whole line. Output should be $line = ‘abctest 1234” this is not search and replace. just to add substring into... (3 Replies)
Discussion started by: jaivipin
3 Replies

7. Shell Programming and Scripting

How to use substr to return data into a shell script variable?

I'm writing a shell script in which I need to be able to pull a portion of the file name out. I'm testing with the following code: x="O1164885.DAT" y=`ls -ltr *${x}|awk '{print substr($0,3)}'` echo ${x}|awk '{print substr($0,3)}' echo "y="$y I can echo it to the screen just fine but I... (3 Replies)
Discussion started by: ttunell
3 Replies

8. Shell Programming and Scripting

Substr in shell script

hi, i am using the following script to get the last digit of YEAR OY=`expr substr $YEAR 2 1` it is showing syntax Is this wrong or i need to change anything I am running this in sun solaris unix (7 Replies)
Discussion started by: gjithin
7 Replies

9. Shell Programming and Scripting

substr() thru awk Korn Shell Script

Hi, I am new stuff to learn substr() function through awk for writing the Korn shell script. Is there a way to copy from XXXX1234.ABCDEF to XXX1234 file names without changing both data files? I appreciate your time to response this email. Thanks, Steve (4 Replies)
Discussion started by: sbryant
4 Replies

10. Shell Programming and Scripting

Trouble using substr function with Bourne shell script

Hi, I'm a newbie to UNIX scripting and I'm having some trouble compiling my script. I'm using the Bourne Shell and cannot seem to use the substr function correctly. I'm trying to extract the last two digits of a year that's stored in a variable based off of a condition. I've searched the... (4 Replies)
Discussion started by: E2004
4 Replies
Login or Register to Ask a Question