1

Now I have strings in the form "temp:10" and I use temp=$(echo $str|awk '{split($0,array,":")} END{print array[1]}') to split which is overkilled and slow..there must be a simpler to do this?

1
  • The answer by tom is the most optimum, but one more point: awk '{split($0,array,":")} END{print array[1]}') is overkill in its own. You can use awk -F: '{print $1}' to achieve same thing.
    – anishsane
    Apr 22, 2013 at 6:43

3 Answers 3

4

Use bash's parameter expansion with suffix removal:

temp=${str%%:*}
2

There's also the read command:

$ str="temp:10"
$ IFS=: read before after <<< "$str"
$ echo "$before"
temp
$ echo "$after"
10
1

If I understand you right, you need the value before the :, temp in this example. If so, then you can use the cut command:

cut -d':' -f1
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.