get only two letter from any string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting get only two letter from any string
# 1  
Old 01-18-2008
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  
Old 01-18-2008
Using sed, assuming it contains 6 numbers,

Quote:
echo $var |sed 's/\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\2/'
using bash,

Quote:
echo ${var:2:2}
# 3  
Old 01-18-2008
[zsh]

Code:
% v=070108
% print $v[$#v/2,$#v/2+1]
01
% v=abcd
% print $v[$#v/2,$#v/2+1]
bc
% v=12345678910
% print $v[$#v/2,$#v/2+1]
56

[bash]

Code:
echo ${v:(${#v}/2)-1:2}

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

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

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

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

8. Shell Programming and Scripting

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? (22 Replies)
Discussion started by: balamv
22 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. UNIX for Dummies Questions & Answers

If first letter of name is a-m do this if n-x do something else

so if I run echo $USER and the name is smithr i want to run command B to run because s is after m in the alphabet, but if the user is aldap I want command A to run because their first initial in the user name is in the first half of alphabet. how please? (2 Replies)
Discussion started by: glev2005
2 Replies
Login or Register to Ask a Question