user3659048
user3659048

Reputation: 47

Grep and print only matching word and the next words

I have a text file, install.history

Wed June 20 23:16:32 CDT 2014, EndPatch, FW_6.0.0, SUCCESS

I would only need to print out the word starting from EndPatch to the end that is FW_6.0.0, SUCCESS
The command below that I have only prints out EndPatch, so what do I need to do so that it prints out the remaining of the words so that my result would be:

EndPatch, FW_6.0.0, SUCCESS

Here is the command that I have:

  grep -oh "EndPatch[[:alpha:]]*" 'install.history'

Upvotes: 4

Views: 13164

Answers (4)

perreal
perreal

Reputation: 97918

This is arguably, easier to do with sed:

sed -n 's/.*EndPatch, //p' install.history

to get the word after EndPatch:

sed -n 's/.*EndPatch, \([^,]*\).*/\1/p' install.history

or:

sed -n 's/.*EndPatch, //p' install.history | cut -d, -f

Upvotes: 3

Avinash Raj
Avinash Raj

Reputation: 174696

You could try the below grep command to get everything from EndPatch upto the last,

grep -oP 'EndPatch, (.*)$' file

or

grep -o 'EndPatch.*$' file

Example:

$ grep -oP 'EndPatch, (.*)$' file
EndPatch, FW_6.0.0, SUCCESS
$ grep -o 'EndPatch.*$' file
EndPatch, FW_6.0.0, SUCCESS

Or

You could try the below command to get all the characters which was just after to EndPatch,

$ grep -oP 'EndPatch, \K(.*)$' file
FW_6.0.0, SUCCESS

Upvotes: 1

Jotne
Jotne

Reputation: 41446

You can use awk

awk -F "EndPatch, " '{print FS$2}' file
EndPatch, FW_6.0.0, SUCCESS

Upvotes: 0

rjv
rjv

Reputation: 6766

You need grep -e for regular expression matching

grep -oh 'install.history' -e "EndPatch[[:alpha:]]*"

Upvotes: 0

Related Questions