Extract a character

HI Guys,
Have a Doubt......

I have a pattern "abcdef"

and i need to extract the third character..ie(c)

How to achieve it?

echo "abcdef" | cut -c3
echo "string" | sed -n -e "s/^..\(.\).*/\1/p"

or

t=abcdef
r=echo ${t%${t#???}}
echo ${r#??}

Hi,

You can also do this in the foll. way:

echo "abcdef" | awk '{print substr( $0, 3, 1 )}'

Note: You can use substr() in many ways , for extracting no. of characters

        Just specify start_no. and No. of char. in 2nd and 3rd. field respectively.

Regards
JAGDISH

[quote=vino;302132533]

echo "string" | sed -n -e "s/^..\(.\).*/\1/p"

Sometimes get confused with regular pattern.

after getting the 3 character from the beginning [ ........\(.\)........]

what does the last expression .* does here..

[quote="bishweshwar,post:5,topic:174571"]

the first 2 characters are specified as ^..
the third character is mentioned as \(.\)
and the remaining characters starting from 4th character to till the end of the string is blocked by .*

and the third character is printed using "\1"

[quote=matrixmadhan;302132635]

[quote=bishweshwar;302132634]

the first 2 characters are specified as ^..
the third character is mentioned as \(.\)
and the remaining characters starting from 4th character to till the end of the string is blocked by .*

Is there any other way...even...the expression .* how it blocks the rest of characters.

[quote="bishweshwar,post:7,topic:174571"]

[quote=matrixmadhan;302132635]

.* - refers to greedy match as you might know

since the first 3 characters are consumed as ( 2 characters and a third character ) all the remaining characters in the form .* would be blocked.

Does that answer your question ?

THanks Guys!!!!!!!!!!!!!!!!!!!!!!!!!

if you use bash

# var="abcdef"
# echo ${var:2:1}
c

With zsh:

% var="abcdef"
% print ${var[3]}
c