Extract the letter from string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract the letter from string
# 8  
Old 11-23-2009
bash example

try this :
Code:
#!/bin/bash
read -p "Enter your words : "
for I in $REPLY
do
    let L=${#I}-1
    echo ${I:$L:1}
done

# 9  
Old 11-23-2009
Code:
$ echo "secret1 secret2 serect3" |  awk 'BEGIN {RS=" "; ORS=" "} {key=substr($1,length($1)); print key}'

This should return whatever is in the last space of each string...
# 10  
Old 11-23-2009
Quote:
Originally Posted by mkastin
Code:
$ echo "secret1 secret2 serect3" |  awk 'BEGIN {RS=" "; ORS=" "} {key=substr($1,length($1)); print key}'

This should return whatever is in the last space of each string...
here, the great advantage of awk is that the first index in a string is 1 (0 in bash).Smilie
# 11  
Old 11-23-2009
Code:
echo "secret1 secret2 secret3" | sed 's/\w*\(\w\)/\1/g'



---------- Post updated at 23:01 ---------- Previous update was at 22:43 ----------

Code:
secrets="secret1 secret2 secret3" 
for i in $secrets; do
  echo ${i#${i%?}}
done

# 12  
Old 11-23-2009
Quote:
Originally Posted by Scrutinizer
Code:
echo "secret1 secret2 secret3" | sed 's/\w*\(\w\)/\1/g'

Can you explain the w's for me? Smilie
# 13  
Old 11-23-2009
Hi,

\w matches any "word" character. A "word" character is any letter or digit or the underscore character.
# 14  
Old 11-24-2009
for example,

Enter the names: Test hai hello

I need the last digits of all the inputs..
here " tio " [last digits of Test hai hello]

Hope, you can understand my req. Thanks for your help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace specific letter in a file by other letter

Good afternoon all, I want to ask how to change some letter in my file with other letter in spesific line eg. data.txt 1 1 1 0 0 0 0 for example i want to change the 4th line with character 1. How could I do it by SED or AWK. I have tried to run this code but actually did not... (3 Replies)
Discussion started by: weslyarfan
3 Replies

2. Shell Programming and Scripting

Issue catching/reading 2 digits and 3 letter string between brackets

Hello I'm writing a handler for ffmpeg, and having troubles to catch some exceptions that may occour with certain files. In order to parse for video & subtitle maps, i've had to make the raw data easier to handle, until now this worked well, but basicly i've just been lucky... The input... (1 Reply)
Discussion started by: sea
1 Replies

3. Shell Programming and Scripting

Counting all words that start with a capital letter in a string using python dictionary

Hi, I have written the following python snippet to store the capital letter starting words into a dictionary as key and no of its appearances as a value in this dictionary against the key. #!/usr/bin/env python import sys import re hash = {} # initialize an empty dictinonary for line in... (1 Reply)
Discussion started by: royalibrahim
1 Replies

4. Shell Programming and Scripting

check index of a string by finding a letter in it

i would like to search for a letter in a string and get its index position. example: name='john' pos=$(expr index $name o) the result will be equal to 2 (2nd position) how do you make this thing not case sensitive? example: name='john' pos=$(expr index $name O) the... (1 Reply)
Discussion started by: kokoro
1 Replies

5. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Shell Programming and Scripting

Trying to take a string and break each letter into a separate variable

I am trying to make a script that takes a word and each letter up and turns it into a separate variable. My code currently does not work but I feel I just need to tweak one thing that I am unsure of. (ex: if forum was typed in letter1=f; letter2=o; letter3=r;...) Thank you count=1; ... (7 Replies)
Discussion started by: crimputt
7 Replies

7. Shell Programming and Scripting

change each letter of a string

Is there a way to change each letter of a string to the next one in the alphabet, so that a becomes b and f becomes g, and digits become one unit bigger - 4 becomes 5 and 9 becomes 0. I want to change strings like ben123 to cfo234. (5 Replies)
Discussion started by: locoroco
5 Replies

8. Shell Programming and Scripting

First Letter in a String to be capitalized

Is there a way to cnvert first letter alone in a string to upper case. For eg: diamond should be converted to Diamond. Thanks in Advance, Kinny (6 Replies)
Discussion started by: kinny
6 Replies

9. Shell Programming and Scripting

Finding a letter in a string

How to check whether a particular string contains dot or not? Here I can not use grep as the string is not in a file. I will get this string from user input Thanks, (2 Replies)
Discussion started by: balamv
2 Replies

10. Shell Programming and Scripting

get only two letter from any string

I want get middle two latter of any string. Input: var="070108" output: var1="01" please help. (2 Replies)
Discussion started by: rinku
2 Replies
Login or Register to Ask a Question