I have the following.

oid=. (these 0.xxx represent any random numeral)

I want to do the following

echo $oid | sed (some commands to remove 0.xxx)

and store the resulting string


in oid.

I don't mind this being done with Grep, awk or sed anything.
Everything before 0.xxx can change only thing static will be 0.
Thanks for your help!

  • This is a default form?
    – Maythux
    Jun 15, 2015 at 7:00
  • Yeah all will end with 0.xxx. I have to remove 0.xxxx. However everything before the 0 can change.
    – SamFlynn
    Jun 15, 2015 at 7:02
  • @SamFlynn In the original oid= , I see there's 10 dots. Or technically speaking - 10 fields separated by dots. Is that gonna always be the same amount ? Jun 15, 2015 at 11:52

5 Answers 5


With bash's Parameter expansion:

echo "$oid"



As you said this is a general form, you want to remove the last 6 characters from your text(.0.xxx), so you can use this:

echo "." |sed 's/.\{6\}$//'

Then if you want use with variables:



echo $oid | sed 's/.\{6\}$//'

output is:


Another solution

echo "${oid:0:${#oid}-6}"

Another solution using cut and '.' as delimeter

echo $oid | cut -d '.' -f -10

Using sed:

oid="$(echo -n . | sed -r 's/\.0\.[0-9]+$//')"

-r: makes sed interpret ERE (Extended Regular Expressions) patterns

sed command breakdown:

  • s: asserts to perform a substitution
  • /: starts the pattern
  • \.: matches a . character
  • 0: matches a 0 character
  • \.: matches a . character
  • [0-9]+: matches 1 or more digits
  • $: matches the end of the line
  • /: stops the pattern / starts the replacement string
  • /: stops the replacement string / starts the modifiers

Using awk

awk -F'.' '{for (i=2;i<=NF-2;i++) {printf "%s","."$i} }' <<< "$oid"

or all in one

awk -F'.' '{for (i=2;i<=NF-2;i++) {printf "%s","."$i} }' <<< "."



Short explanation

NF-2 – all elements without the last two elements

  • To have a new line without having to say print "" you can always say printf "%s%s","."$i,(i<NF-2?OFS:ORS). This way, it will print a ORS (normally new line) after the last item.
    – fedorqui
    Jun 15, 2015 at 14:18

Using grep:

grep -Po '.*(?=\.0\.\d+$)'

Test :

$ oid='.'

$ oid="$(grep -Po '.*(?=\.0\.\d+$)' <<<"$oid")"

$ echo "$oid"
  • grep -P will enable us to use PCRE

  • grep -o will output only the matched portion

  • .*(?=\.0\.\d+$) will match all characters prior to .0, followed by . and any number of digits at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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