Extract the letter from string


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

Hi,

Pl help me.

User is entering the values for the given input

Enter keys: secret1 secret2 secret3

can i extract the last digit of each values in the keys..
here for ex. 1 2 3

can we get this using shell script?
# 2  
Old 11-23-2009
Code:
$ echo "secret1 secret2 serect3" |  awk '{gsub(/[a-z]/,""); print $0}'
1 2 3


Last edited by mkastin; 11-23-2009 at 12:24 PM.. Reason: adding code tags
# 3  
Old 11-23-2009
Code:
read secrets

for i in $secrets
do
        str=$str" "$(echo $i | sed 's/.*\([0-9]$\)/\1/')
done

echo $str

# 4  
Old 11-23-2009
Quote:
Originally Posted by mkastin
Code:
$ echo "secret1 secret2 serect3" |  awk '{gsub(/[a-z]/,""); print $0}'
1 2 3

Hi,

Thanks for the reply.

But am getting error...

echo "secret1 secret2 serect3" | awk '{gsub(/[a-z]/,""); print $0}'
awk: syntax error near line 1
awk: illegal statement near line 1
# 5  
Old 11-23-2009
Try using gawk or nawk instead of awk. You may have to install them and I would recommend doing that if you're able.


or

use sed

Code:
echo "secret1 secret2 secret3" | sed 's/[a-z]//g'

# 6  
Old 11-23-2009
i want to get the last digit.. it may contain letters also instead number..

your command works only to extract the numbers.. but i did not want this.

Pl help
# 7  
Old 11-23-2009
Please provide a better example of input and output that you are looking for.
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