Assign a variable the nth character of a string.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assign a variable the nth character of a string.
# 1  
Old 03-06-2013
Assign a variable the nth character of a string.

I see a millioin ways to do this with echo, but what I wan to do is assign a variable the "nth" character of an incoming parameter to a ksh script.

$1 will be "pia"

I need to assign the first character to stmttype.
# 2  
Old 03-06-2013
Code:
#! /bin/ksh
stmttype=${1:0:1}
echo $stmttype

Code:
# ./test.sh pia
p
#

# 3  
Old 03-06-2013
Solution

I found that this works

Code:
bankid=$1
stmttype=$2
jdd=`date '+%j'`

stid=$(expr substr $2 1 1)

echo "ID: $stid"

# 4  
Old 03-06-2013
Depending on your shell, substr may be needed yes. ${stringSmilieffset:length} works in BASH and modern KSH, but if you're stuck with an old Solaris SH or the like, that feature's not there.
# 5  
Old 03-06-2013
RedHat

for old ksh and sh

Code:
#! /bin/ksh 
stmttype=`echo $1 | cut -c1` 
echo $stmttype


Last edited by sam05121988; 03-06-2013 at 12:31 PM.. Reason: new lines didn't show up first time
# 6  
Old 03-06-2013
Try:
Code:
echo "${1%%${1#?}}"

==edit==
as pointed out by alister, it is better to put double quotes around the inner expansion (and only one % is required in this case):
Code:
echo "${1%"${1#?}"}"


Last edited by Scrutinizer; 03-07-2013 at 05:37 AM.. Reason: improvement
# 7  
Old 03-06-2013
Code:
echo "${1%%${1#?}}"

Why would you do that instead of 'cut'?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print last character of hostname and assign to a variable ?

Hi How to pass echo output to a variable ? Does below awk command will get the last character of hostname and assign to a variable - "svr" ? svr=$( echo `hostname` | awk '{print substr($0,length,1)}' ) Thanks. Please use CODE tags when dsplaying code segments, sample input, and sample... (7 Replies)
Discussion started by: Lim
7 Replies

2. Shell Programming and Scripting

Remove a character and assign result to a variable

I am reading lines from a file that contain a number sign (#) before a three or four digit number: #1043 #677 I can remove the '#' and get just the number. However, I then want to assign that number to a variable and use it as part of a path further on in my program: /mydir/10/1043 for... (5 Replies)
Discussion started by: KathyB148
5 Replies

3. Shell Programming and Scripting

To find nth position of character in string

Hi guyz i want to know nth position of character in string. For ex. var="UK,TK,HK,IND,AUS" now if we see 1st occurance of , is at 3 position, 2nd at 6,..4th at 13 position. 1st position we can find through INDEX, but what about 2nd,3rd and 4th or may be upto nth position. ? In oracle we had... (2 Replies)
Discussion started by: Jonty Immortal
2 Replies

4. Shell Programming and Scripting

Problem while assign string (words with tab) to a variable

Hi, I have a String(words with tab space) in a file ->file1.txt 0xxxx 11 test $aa$ 8.43 when i read the file and assign to variable value=$(cat file1.txt) echo $value i get the output without tab spaces. 0xxxx 11 test $aa$ 8.43 How to assign string... (2 Replies)
Discussion started by: nanthagopal
2 Replies

5. Shell Programming and Scripting

need to assign a string to a variable

Hello Experts, In my script i am using the below syntax /usr/bin/head -1 /opt/chumma.txt | /usr/bin/cut -d " " -f3 output of this one is --> Monday I want to store this String in a variable (i.e) j or k... Give me some idea experts. Thanks in advance. (7 Replies)
Discussion started by: natraj005
7 Replies

6. Shell Programming and Scripting

Bash assign string to variable

Hi ,I am trying to assign string to variable ,but it doesn't work Also could you show me different ways to use grep,(I am trying to get the first,second and first column form file,and I am counting the chars) let name=`grep "$id" product | cut -c6-20` (25 Replies)
Discussion started by: lio123
25 Replies

7. Shell Programming and Scripting

How to assign the Pattern Search string as Input Variable

guys, I need to know how to assing pattern matched string as an input command variable. Here it goes' My script is something like this. ./routing.sh <Server> <enable|disable> ## This Script takes an input <Server> variable from this line of the script ## echo $1 | egrep... (1 Reply)
Discussion started by: raghunsi
1 Replies

8. Shell Programming and Scripting

read and assign each character from the string to a variable

How... can I read input by a user character by cahracter. And assign each character from the string to a variable? Any help would be greatly appreciated! Thank you! (1 Reply)
Discussion started by: Tek-E
1 Replies

9. Shell Programming and Scripting

How to assign variable from a string having blanks in between

Hi All, I am new to bash scripting. I need your help to removing spaces from a string and assign them to different variables. Iwant to call script with one command line argument which is file name which containes different attributes their type and different values eg ... (1 Reply)
Discussion started by: flextronics
1 Replies

10. UNIX for Dummies Questions & Answers

How do I get the nth character from a string?

How to I get the nth character from a string in shell script. For instance, I have a string London. I want to get, say the first character (L) from the string. How do I do this in unix shell? Thankx (4 Replies)
Discussion started by: toughman
4 Replies
Login or Register to Ask a Question