Bash – how to preform a Mid Function in Bash

bashunixvb

I'm trying parse the text of a string in bash which I then can throw a case selection at, but I'm having the biggest trouble trying to figure how to do it. In VB I know its real easy like

someString = "Hey there"
Select Case Mid(someString,1,5)
 Case "Hey t" 'Do something
 Case "Go aw" 'Do something else
 Case 'Else
  'String is unrecognized
End Select 

I can't seem to find anything that shows how I can do a string manipulation function which is equivalent to the Mid function. Do I use sed, I don't believe I can use awk or cut since I'm not trying to trim a string based upon a pattern, but surely it has to be just as simple in Unix.

Best Answer

I'm not sure I understand your question, since I think 'cut' will work just as you want it to.

someString="Hey there"

case `echo $someString | cut -c1-5` in
    "Hey t") echo Do something ;;
    "Go aw") echo Do something else ;;
    *) echo String is unrecognized ;;
esac