If you have columns of data separated by arbitrary amounts of
whitespace, the command you're looking for is awk, rather than grep.
awk '{print $1}' will print the first column of every line. $2 for
the second, etc.
$NF (short for "Number of Fields") for the last column, $(NF-1) for
the second-to-last, etc.
$0 is the whole line.
you can stick /pattern/ in front of the {...} block to run it only for
lines matching that pattern, e.g.
awk '/[0-9]/ {print $1}' will print the first column of all lines
that contain a digit anywhere in the line. Note that this means it is
never necessary to do grep | awk. :)
On 3/28/07, Richard Rönnbäck <email@hidden> wrote:
I have been trying really hard to find the proper grep syntax for what I
guess is really simple, but I just can't get it right
The text I like to grep is made up of two or more columns numbers separated
by one or more spaces. The following columns may consist of any combination
of numbers and characters .
What I need to do is to get only the first column, which unfortunately may,
or may not, begin with spaces, like this:
12345 647890 kdghjblrttrep 9845698476 lkjlfkdj
So by writing
grep "^[0-9]*" -o
I get all the first columns of those lines that doesn't begin with spaces,
but if I try to include one of more spaces like this
grep "^ *[0-9]*" -o
it gives my all columns containing numbers, besides what I really want is
the numbers of the first columns but without the spaces