Truncate string variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Truncate string variable
# 1  
Old 05-10-2010
Truncate string variable

Hi,

I want to truncate a string variable, returned in the script. In perl I used the below and it worked.

BRNo=BR12345
$BR = substr($BRNo, 2, 7)

How can I do it in sh.

Thanks !
# 2  
Old 05-10-2010
Code:
echo 'BR12345' | sed 's/^..\(.....\).*/\1/'

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 05-10-2010
Code:
$ expr $BRNo : '..\(.....\)'
12345
$ expr $BRNo : '..\(.\{5\}\)'
12345

# 4  
Old 05-10-2010
Two other ways for substrings :
Code:
$ Str=123456789
$ s1=$(expr substr "${Str}" 2 3)
$ echo $s1
234
$ s2=${Str:1:3}    # bash
$ echo $s2
234
$

Jean-Pierre.

Last edited by aigles; 05-10-2010 at 05:01 PM.. Reason: Added missign assignment statement to s1 variable
# 5  
Old 05-10-2010
Code:
brno=br12345
br=`echo $brno|cut -c3-7`

# 6  
Old 05-10-2010
Quote:
Originally Posted by aigles
Two other ways for substrings :
Code:
$ Str=123456789
$ echo $s1
234
$ s2=${Str:1:3}    # bash
$ echo $s2
234
$

Jean-Pierre.
Jean, You missed the assignment statement to s1 variable
# 7  
Old 05-10-2010
in ksh, just use a variable declaration:

Code:
typeset -L5 x

x=aslkdhflaalkshdlkhasd
echo $x
aslkd

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if string variable is a subset of another string variable

Below is my ksh shell script where I need to check if variable fileprops is a subset of $1 argument. echo "FILE PROPERTY: $fileprops" echo "PARAMETER3: $1" if ; then echo "We are Good. $line FILE is found to be INTACT !! " else echo... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

How to truncate a string to x number characters?

Hello: I have a large file which contains lines like the following: 1/t123ab, &Xx:1:1234:12345:123456@ABCDEFG... at -$100.00% /t is a tab, spaces are as indicated the string "&Xx:1:1234:12345:123456$ABCDEFG..." has a slightly variable number of numbers and letters, but it always starts... (9 Replies)
Discussion started by: Tectona
9 Replies

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

4. Shell Programming and Scripting

truncate string to integer or decimal

From strings stored in variables, I need to isolate and use the first numerical value contained within them. I will need to know how to produce an integer as well as a floating point decimal. It needs to work on any string regardless of what types of characters (if any) are preceding or following... (3 Replies)
Discussion started by: bradlecat
3 Replies

5. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

6. Shell Programming and Scripting

Truncate all characters and numbers in string - using perl

hi, I have an data from file where it has 20110904 234516 <<hdd-10#console|0c.57,passed,5,28,READ,0,20822392,8,5,4,0,40,0,-1,0,29909,25000,835,3.3,0,0,0,0,implied,0,0,2011/9/5-2:3:17,2011/9/5-2:3:47,X292_0F15,TAP ,NQ09,J40LTG\r\r\n I want to remove characters till #console| i.e want... (1 Reply)
Discussion started by: asak
1 Replies

7. Shell Programming and Scripting

Appending string, variable to file at the start and string at end

Hi , I have below file with 13 columns. I need 2-13 columns seperated by comma and I want to append each row with a string "INSERT INTO xxx" in the begining as 1st column and then a variable "$node" and then $2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13 and at the end another string " ; COMMIT;" ... (4 Replies)
Discussion started by: Vaddadi
4 Replies

8. Shell Programming and Scripting

Truncate Specific Parts of a String

How do you truncate specific parts of a string. Example: 1 This is the string Goal: This is the string As you can see I'm trying to simply remove the first two characters of the string the number one and the space between the one and the word "this." Your help is appreciated. ... (8 Replies)
Discussion started by: royarellano
8 Replies

9. Shell Programming and Scripting

truncate a string from the end

hi, guys. I have a question. If I have a long string like this: 8.0K:/home/test/brownj How can I get a substring which starts from the last slash to the end of the string, so in this case, it will be brownj Thank you very much for you time in advance -Keyang (4 Replies)
Discussion started by: daikeyang
4 Replies

10. Shell Programming and Scripting

truncate/round down 2nd string print both

I have a file with a name and then a number: Apple 1.1 Apple 1.5 Apple 1.9 Banana 1.3 Banana 2.7 Banana 3.3 I'd like to print out the first string followed by the second number rounded down or truncated: Apple 1 Apple 1 Apple 1 Banana 1 Banana 2 Banana 3 I was trying: (3 Replies)
Discussion started by: dcfargo
3 Replies
Login or Register to Ask a Question