substring without using expr command

Hi guys,

For some reason the terminal on my mac does not let me run string manipulations commands using the expr command. I'm not sure how to fix this so I'm requesting a "work-around" to using the expr command...

This is the string I'm working with:
"neo_opls01_1.log"

And I'm trying to find the index of the period (.) and extract the prefix. So the substring I want would be:
"neo_opls01_1"

Do you guys know how to do this without using the expr command? Or is there something I need to do to my mac to be able to use string manipulation commands with the expr command? Both suggestions would be really helpful!!

Thanks!!

Hi.

I never use expr anyway - it's a terrible command imo :smiley:

$ X="neo_opls01_1.log"   
$ echo ${X%.*}
neo_opls01_1

Thanks! Also would you happen to know to just make it:
"neo_opls01_"

basically truncate the number at the end....

Thanks again!!

$ echo ${X%%1.log}
neo_opls01_

$ echo ${X/?.log/}
neo_opls01_

If it starts getting too fancy, I'd switch to sed!

I'd agree with scottn on the expr command. Both awk and cut would work too:

$ echo "neo_opls01_1.log" |awk -F'.' '{print $1;}'
neo_opls01_1
$ echo "neo_opls01_1.log" |cut -d'.' -f1
neo_opls01_1

Thanks!

How about if I had this:

"real 449m8.133s"

And I wanted to substring it to only make it:

"449m8.133s"

Thanks again!

Well, not to be a wise guy, but did you happen to notice that the awk -F and the the cut -d argument pairs define a field delimiter, as did scottn's shell command? Figure your string seems to include one that you could use .

Try it out. Play a little. Let me know how it works. :slight_smile:

$ echo ${X##* }
449m8.133s

(not sure if you meant with, or without the quotes ("...")

Edit: notwithstanding curleb's comments... give the awk / cut a go...

I'm guessing the field delimiter would be the space? But how would I translate that into code? I'm not entirely sure how to use awk or cut commands.

Bingo! You're on your way to parsing every string in your list of what ifs...

awk -F' ' ... 

cut -d' ' ... 
$ awk
Usage: awk [-f programfile | 'program'] [-Ffieldsep] [-v var=value] [files]
$ cut
cut: b, c or f option must be specified
Usage: cut [-nsN] [-b list] [-c list] [-d delim] [-f list] [-r reclen] [-D ldelim] [file ...]