LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   a new BASH question, howto get the 3rd - 5th char out of a name in a file (https://www.linuxquestions.org/questions/linux-newbie-8/a-new-bash-question-howto-get-the-3rd-5th-char-out-of-a-name-in-a-file-4175426884/)

lleb 09-12-2012 09:14 AM

a new BASH question, howto get the 3rd - 5th char out of a name in a file
 
I need to know how to get the 3rd - 5th character in this situation:

Code:

[103050_rx30@rx30 ~]$ set - `cat deaopts.cnf`
[103050_rx30@rx30 ~]$ echo $1
NDXX0000147

I am trying to generate a variable from the above file for archiving reasons. the basic file name will be the 3rd - 5th character of echo $1. The first two chars are meaningless for the file name, but do represent other bits of info that I don't need.

I'm sure there is some kind of awk or sed command that I can use to grab those characters, but sadly I just dont know enough yet to figure that out.

Thanks in advance for the help.

pan64 09-12-2012 09:17 AM

something like this:
echo $1 | sed ' s/^..\(...\).*$/\1/'

suicidaleggroll 09-12-2012 09:23 AM

Or if you want to stick with BASH:

echo ${1:2:3}

In normal-speak, extract 3 characters from variable "1", starting at position 2 (it's 0-based).

lleb 09-12-2012 09:37 AM

perfect guys, thanks.


All times are GMT -5. The time now is 10:45 PM.