As you use pipes on the command line, I guess you will like
the command line switches -e, -p and -n: They allow you to
write a short perl program on the commandline, right into
your pipes.
Here are three short solutions for your question. They differ
in that the first one prints a final space at the end of the
results while the other solutions don't:
xcommand|perl -wne '/(\S+)/ and print "$1 "'
xcommand|perl -we 'print join " ", map{/(\S+)/; $1} <>'
xcommand|perl -we 'print join " ", map{(split" ", $_)[0]}<>'
Let's explain the tools we used in these three solutions:
The -w switch does the bulk of the work: It tells me when
I did something wrong.
The -n switch in the first example builds a loop around
our main program to process the input line by line:
while(<>){ ... the code goes here ... }.
The -e switch reads the next command line argument and
executes it as the perl program.
In the first solution, the program simply says
/(\S+)/;: "Search {//} for at one or more {+}
non-white-space characters {\S} and remember them all {()}".
print "$1 ": print what you have remembered
and a space. You remember, this is done for every line of
the input.
The second solution does almost the same thing. Instead of
the -n switch, we use map the list of all input
lines <> and join the results by spaces. Thus,
we do not print an extra space after the last field.
The third solution uses the split function
instead of a regular expression.
Note: In a Dos-Window (UUUHHHH-OOOOHHH-EEEEEKS), the examples
will not work as given because there the "shell" messes
the quotation marks up. You have to use ouble quotes (") around
your code (and you can't use them inside)!
|