So, I need to grep the text after a string like
Motherboard P/N : XXXXXXXXXX
Where XXXXXXXXX
is the text I want.
I don't want the Motherboard P/N :
I can't seem to make awk, cut, sed or anything work well with the P/N
Any ideas?
Explanation:
-o
Print only matching part of the line-P
Use Perl-compatible regex (PCRE) - this enables more advanced regex features\K
Don't consider the preceding as part of the match..*
Match zero or more of any character (except a newline)Why not using the pipeline mechanism with grep and sed in the following way:
where sourceFile is a file containing your text. Sed has the following regular expression:
^.*
means: from the beginning of the line, any number of characters tillMotherboard
string with some more characters, which need a comment (below)\
character in front of special characters "escapes" them, so they are not interpreted by the shell or the command, but instead are used as plain characters: a space, slash and colon.Or you could pipe the output of a command (like the useful
sudo lshw
)However, it's most likely that you will not find "Motherboard P/N : " text in the output of lshw, and the output of this example may be empty...
EDIT:
Or, as I see what you need from your comments, you could modify the regex slightly, to accommodate to "S/N" and "P/N" at the same time. Use a dot
.
in place of a character that varies between the source text lines. E.g.:...the same for
grep
Just use look-back with grep:
in this case regex would look like this:
the full grep command. use
-o
to neglect everything you don't need and-P
for pearl regex. And i like to pipe it into sort just for good measure