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.
# 8  
Old 03-06-2013
Quote:
Originally Posted by hanson44
Code:
echo "${1%%${1#?}}"

Why would you do that instead of 'cut'?
Far more efficient to run a shell builtin or string operator than an external application -- especially for tiny amounts of data. It's not a meaningless gripe. whenever we have someone asking "please speed up my script", bad habits like echo | cut/awk/sed are almost always the culprit. Put them in a loop and the penalty adds up quickly. In my early programming days I once wrote a linewrapper which processed text at the blazing speed of 5 kilobytes per second...

Unfortunately, on shells without fancy operators for strings, you're limited in what you can do.
# 9  
Old 03-06-2013
Quote:
Originally Posted by Scrutinizer
Try:
Code:
echo "${1%%${1#?}}"

With the sample data provided by the OP, that will work. However, if this is used in a more generalized capacity (if not by the OP, then perhaps by someone who has read this thread), it should be noted that an argument containing pattern matching metacharacters (*, ?, [, etc) will yield unintended results. To protect against this, an additional set of quotes is necessary.

Also, since this task requires that either the data be free of metacharacters or that any metacharacters be quoted, the use of %% isn't needed. % will behave identically.
Code:
echo "${1%"${1#?}"}"

It's possible that construct may still have issues with strings beginning with a dash.

Perhaps the following would be the simplest, most robust, and most portable approach which retains the efficiency of a sh builtin:
Code:
printf '%.1s' "$1"

Regards,
Alister

Last edited by alister; 03-06-2013 at 07:12 PM..
These 2 Users Gave Thanks to alister For This Post:
# 10  
Old 03-07-2013
Wonderful alister! Double quotes within parameter braces make pattern characters literal ( and can be used even within other double quotes around the variable ), thank you for your insightful and insight giving comment!

--
With the printf suggestion, there would be a problem with multi-byte characters.

Last edited by Scrutinizer; 03-07-2013 at 05:45 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 11  
Old 03-07-2013
Quote:
Originally Posted by Scrutinizer
With the printf suggestion, there would be a problem with multi-byte characters.
True. The format string spec for the precision flag specifies bytes not characters. Thank you for making that clear.

Regards,
Alister
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