Shell – Return Portion of Line After Matching Pattern

grepshelltext processing

I have a file (file_name) which contains exactly one occurance of the string Result:, at the start of a line. I want to print all the characters after the string Result: in that line until I encounter a space. Which shell command should I use?

grep "Result: " file_name | tail -c +9 

is not working.

Best Answer

The simplest way would be to use awk.

awk '/^Result: / {print $2}' file_name

That matches lines that begin with Result:, and prints the second field in the file, as defined by the default $IFS, which is whitespace.

Related Question