Substr in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substr in shell script
# 8  
Old 05-10-2008
Hi.

The solutions posted will solve the problem nicely.

I was interested in the cause. Here it is:
Quote:
Compatibility Operators (x86 only)
The following operators are included for compatibility with
INTERACTIVE UNIX System only and are not intended to be used
by non- INTERACTIVE UNIX System scripts:

index string character-list

length string

substr string integer-1 integer-2
-- excerpt from man expr on Solaris 10
I did find that there are a number of versions of expr on Solaris. One of them works with the variations on the man page (I didn't test index):
Code:
#!/bin/bash -

# @(#) s3       Demonstrate expr: /usr/ucb/expr works on Solaris.

echo
version >/dev/null 2>&1 && version =o $(_eat $0 $1)
# set -x        # enable to debug as necessary

cat >t1 <<'EOF'
E="$1"
YEAR=${2-2008}
echo " Results for $E, length of YEAR=$YEAR is $($E length $YEAR)"

echo " $E substr:"
$E substr $YEAR 1 1
$E substr $YEAR 2 1
$E substr $YEAR 3 1
$E substr $YEAR 4 1

echo " $E string:"
$E $YEAR : '.*\(.\)$'

echo " $E match:"
$E match $YEAR '.*\(.\)$'

echo " End of test for $E "
EOF

chmod +x t1
L="/usr/xpg4/bin//expr /usr/bin/expr /usr/ucb/expr /usr/xpg6/bin/expr"
L=/usr/ucb/expr
for command in $L
do
  echo
  ./t1 "$command" 2007
done

exit 0

Producing:
Code:
$ ./s3

SunOS 5.10
GNU bash 3.00.16

 Results for /usr/ucb/expr, length of YEAR=2007 is 4
 /usr/ucb/expr substr:
2
0
0
7
 /usr/ucb/expr string:
7
 /usr/ucb/expr match:
7
 End of test for /usr/ucb/expr

So if you absolutely needed to use expr, the version in /usr/ucb seems to work well ... cheers, drl
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. UNIX for Dummies Questions & Answers

Substr

awk '/^>/{id=$0;next}length>=7 { print id, "\n"$0}' Test.txt Can I use substr to achieve the same task? Thanks! (8 Replies)
Discussion started by: Xterra
8 Replies

3. Shell Programming and Scripting

How to use if/else if with substr?

I have a command like this: listdb ID923 -l |gawk '{if (substr($0,37,1)==1 && NR == 3)print "YES" else if (substr ($0,37,1)==0 && NR == 3) print "NO"}' This syntax doesn't work. But I was able to get this to work: listdb ID923 -l |gawk '{if (substr($0,37,1)==1 && NR == 3)print "YES"}' ... (4 Replies)
Discussion started by: newbie2010
4 Replies

4. UNIX for Dummies Questions & Answers

substr

can anybody explain this code? thanks in advance..:) (6 Replies)
Discussion started by: janani_kalyan
6 Replies

5. Shell Programming and Scripting

substr from a string in Shell script

Shell Scripting Gurus, I am having a hard time :confused: 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"... (3 Replies)
Discussion started by: yajaykumar
3 Replies

6. 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

7. Shell Programming and Scripting

get substr?

Hi, I have a long string like, aabab|bcbcbcbbc|defgh|paswd123 dedededede|efef|ghijklmn|paswd234 ghghghghgh|ijijii|klllkkk|paswd345 lmlmlmmm|nononononn|opopopopp|paswd456 This string is devided into one space between substrings. This substrings are, aabab|bcbcbcbbc|defgh|paswd123... (6 Replies)
Discussion started by: syamkp
6 Replies

8. 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

9. UNIX for Dummies Questions & Answers

Substr

Hi, My input file is 41;2;xxxx;yyyyy.... 41;2;xxxx;yyyyy.... 41;2;xxxx;yyyyy.... .. .. I need to change the second field value from 2 to 1. i.e., 41;1;xxxx;yyyyy.... 41;1;xxxx;yyyyy.... 41;1;xxxx;yyyyy.... .. .. Thanks in advance. (9 Replies)
Discussion started by: deepakwins
9 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