4

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

3
  • Is not the same as this stackoverflow.com/q/974757/422353?
    – madth3
    Mar 23, 2012 at 2:35
  • how the hell is this question off topic ? someone please explain
    – mcgrailm
    Mar 23, 2012 at 2:37
  • Try piping that to awk, then you can split it up and do whatever you like to it. Mar 23, 2012 at 2:40

4 Answers 4

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

3

This works for me:

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

output:

FAB00772450

4
  • 1
    what does the 7 do ?
    – mcgrailm
    Mar 23, 2012 at 2:55
  • it prints the 7th column I think. It splits on whitespace. Mar 23, 2012 at 3:04
  • 1
    It prints the 7th field. The split is on whatever FS is (defaults to space). Mar 24, 2012 at 20:16
  • That's not a very flexible solution. Apr 27, 2022 at 10:54
1

I'd say this is a better and cleaner approach than any of the previous answers:

grep -oP 'Order Number :\s*\K[^\r\n]*' 'Extract Text Output.rtf'

or

grep -oP 'Order Number :\s*\K[A-Z0-9]*' 'Extract Text Output.rtf'

  • \K discards anything that was matched up until that point.
1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Apr 27, 2022 at 12:25
0

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"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .