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.


This worked for me

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



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.

  • 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')
  • 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 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.