Extract column using grep

You need to use awk:

awk '{print $1,$3}' <namefile>

This simple command allows printing the first ($1) and third ($3) column of the file. The software awk is actually much more powerful. I think you should have a look at the man page of awk.

A nice combo is using grep and awk with a pipe. The following code will print column 1 and 3 of only the lines of your file that contain ‘p1’:

grep 'p1' <namefile> | awk '{print $1,$3}'

If, instead, you want to select lines by line number you can replace grep with sed:

sed 1p <namefile> | awk '{print $1,$3}'

Actually, awk can be used alone in all the examples:

awk '/p1/{print $1,$3}' <namefile> # will print only lines containing p1
awk '{if(NR == 1){print $1,$3}}' <namefile> # Will print only first line

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)