Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
    
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
add comment

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

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

3 Answers

up vote 3 down vote accepted
cat 'Extract Text Output.rtf' | sed -n 's/Order Number : \(.*\)\\/\1/gp'

Yields exactly what you want.

Explanation:

  • 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/

share|improve this answer
add comment

This works for me:

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

output:

FAB00772450

share|improve this answer
    
awesome thank you –  mcgrailm Mar 23 '12 at 2:54
    
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 Wilson Mar 24 '12 at 20:16
add comment

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"

share|improve this answer
add comment

Your Answer

 
discard

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.