For the examples given, I prefer cut. But for the general problem expressed by the question, the answers above have minor short-comings. For instance, when you don't know how many spaces are between the words (cut), or whether they start with a space or not (cut,sed), or cannot be easily used in a pipeline (shell for-loop). Here's a perl example that is fast, efficient, and not too hard to remember:
| perl -pe 's/^\s*(\S+\s+){2}//'
Perl's -p
operates like sed's. That is, it gobbles input one line at a time, like -n
, and after dong work, prints the line again. The -e
starts the command-line-based script. The script is simply a one-line substitute s///
expression; substitute matching regular expressions on the left hand side with the string on the right-hand side. In this case, the right-hand side is empty, so we're just cutting out the expression found on the left-hand side.
The regular expression, particular to Perl (and all PLRE derivatives, like those in Python and Ruby and Javascript), uses \s
to match whitespace, and \S
to match non-whitespace. So the combination of \S+\s+
matches a word followed by its whitespace. We group that sub-expression together with (...)
and then tell sed to match exactly 2 of those in a row with the {m,n}
expression, where n is optional and m is 2. The leading \s*
means trim leading whitespace.