Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to pull a piece of text from a file to use as input for the next command. The word always starts with "JID_" and then some random numbers/characters after that.

For example :

            Selector: InstanceID = JID_001264193601, __cimnamespace = root/dcim

I want to assign a variable $JID to that string of characters. How do I pull it from the line ? sed or grep preferrably, but any method is fine.

share|improve this question

This worked for me

(03:45 PM):(Jacob@houston)~$ egrep -o "JID_[0-9]+" bah | cut -d _ -f2


share|improve this answer

Ok, I just hacked at it for a few minutes and came up with this :

grep JID job.txt | sed -e "s/.*InstanceID = //" -e "s/,.*//"

With output of this :


I think that will work.

share|improve this answer
I think he just wants to output the characters after JID_ – einstiien Jan 22 '10 at 21:28

This command:

var=$(sed -n 's/.*InstanceID = JID_\([0-9]\+\),.*/\1/p')
echo $var

will output this:


for the given input.

If you want to keep the "JID_":

var=$(sed -n 's/.*InstanceID = \(JID_[0-9]\+\),.*/\1/p')
share|improve this answer
I want to know the \1/p at the last line of both commands you mentioned what it does? – Registered User Mar 11 '11 at 14:20
@RegisteredUser: The \1 brings forward the string that was captured by the first (in this case only) \(\) before the middle slash (in this case it's a sequence of one or more digits). The p after the final slash cause the result to be printed since the -n option turns off automatic printing. Using these together prevents any output of lines that do not match the regular expression between the first and middle slashes. – Dennis Williamson Mar 11 '11 at 14:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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