grep file for only part of line
Solution 1
cat 'Extract Text Output.rtf' | sed -n 's/Order Number : \(.*\)\\/\1/gp'
Yields exactly what you want.
Explanation:
sed -n
suppress default output of seds/.../.../g
search and replace, g: everything/globallyOrder Number : \(.*\)\\
look for "Order Number :
" string and abackslash
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 replacementp
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/
Solution 2
This works for me:
grep "Order Number" test.txt | awk {'print $7'} | tr "\\\ " " "
output:
FAB00772450
Related videos on Youtube
mcgrailm
Hello, thanks for visiting my profile. I am a web developer at PSU. I started my carrier by learning applescript. Since then I've become familiar with PHP MySQL, javascript, jQuery as well. I have made use of other services that go hand and hand with those languages such as json,jsonp,ajax. I have some shell scripting experience and have dabbled in perl. Hopefully I have helped you with a question you or maybe you have help me with a question, thanks for stopping by.
Updated on September 18, 2022Comments
-
mcgrailm over 1 year
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
-
mcgrailm about 12 yearshow the hell is this question off topic ? someone please explain
-
user1200129 about 12 yearsTry piping that to awk, then you can split it up and do whatever you like to it.
-
-
mcgrailm about 12 yearswhat does the 7 do ?
-
user1200129 about 12 yearsit prints the 7th column I think. It splits on whitespace.
-
Scott C Wilson about 12 yearsIt prints the 7th field. The split is on whatever FS is (defaults to space).
-
Admin about 2 yearsThat's not a very flexible solution.
-
Admin about 2 yearsYour 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.