Himanshu Dubey Himanshu Dubey - 1 year ago 74
Linux Question

Read perticular characters from a word using shell script in linux

I have a variable values stored in environment variables.
when I do

echo $myvar

I can see the value. Below is the example

~% echo $myvar

Now, I would like to read 8th/13th/14th character from this variable value and print the value


Please help. Thanks in advance.

Answer Source

If you use BASH:

myvar="abcdefghijkl73"; echo ${myvar:7:1}${myvar:12:1}${myvar:13:1}

See "Parameter expansion" section in man bash For the general POSIX shell you need to use external utility to do this:

echo $myvar | cut -c 8,13,14

This uses cut utility (more information about this utility you can find in info coreutils

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download