I have a rtf file that I'm using grep on like this

 grep "Order Number" 'Extract Text Output.rtf'

which results in lines that look like this

\b\fs28 \cf2 Fab Order Number : FAB00772450\

and I want the result to be just FAB00772450

I know if I use -o it will just return the word "Order Number" but that doesn't help me

migrated from stackoverflow.com Mar 24 '12 at 20:04

This question came from our site for professional and enthusiast programmers.

  • Is not the same as this stackoverflow.com/q/974757/422353? – madth3 Mar 23 '12 at 2:35
  • how the hell is this question off topic ? someone please explain – mcgrailm Mar 23 '12 at 2:37
  • Try piping that to awk, then you can split it up and do whatever you like to it. – user1200129 Mar 23 '12 at 2:40
cat 'Extract Text Output.rtf' | sed -n 's/Order Number : \(.*\)\\/\1/gp'

Yields exactly what you want.


  • sed -n suppress default output of sed
  • s/.../.../g search and replace, g: everything/globally
  • Order Number : \(.*\)\\ look for "Order Number : " string and a backslash and save anything in between to group 1; (downside of using sed is to have to escape regex's grouping operator: (...) with \(...\) )
  • \1 use group 1 as replacement
  • p print replacement if any match

This is way more flexible and generic than using hard-coded awk groups ($7).

Note 1: use .*? if you have lines formatted like this:

 \cf2 Fab Order Number : FAB00772450\ \b \cf2

This prevents regex from being greedy and stops at the first backslash. Not tested if sed supports *? and +? operators, but let's hope.

Note 2: If you have multiple parts you want to extract from a line, use multiple groups and in the replacement string you can even switch them with formatting, like .../\2 - \1/


This works for me:

grep "Order Number" test.txt | awk {'print $7'} | tr "\\\ " " "



  • what does the 7 do ? – mcgrailm Mar 23 '12 at 2:55
  • it prints the 7th column I think. It splits on whitespace. – user1200129 Mar 23 '12 at 3:04
  • 1
    It prints the 7th field. The split is on whatever FS is (defaults to space). – Scott C Wilson Mar 24 '12 at 20:16

If this format is always followed but the number of tokens is not always the same, you could pipe it through something like

sed 's/.*: //' | sed 's#\##'

This also yields "FAB00772450"

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.