1

I use a file as a little db table. The content is like this:

client1    john doe    engineer    cal
client2    jane doe    nurse       ny
client3    ali veli    doctor      ny

I want to find profession of client3. There is a "space" between name and surname, there is a "tab" between other columns. First column is unique so I can use this colums as private key.

I want to use a command for finding profession or state of a client by using a command like this:

grep -i client3 | "select 3rd column"

What can I use isntead of "select 3rd column"?

2 Answers 2

3

"cut" command can be used for these situations. For this example we can use grep -i client3 | cut -f 3 to find profession of client3. Same way we can see client2's name and surname at the same time by use grep -i client2 | cut -f 2 Because there is only one "space" character between name and surname and cut -f command use "tabs" as a separator.

0

An alternative solution using awk.

grep -i "client3" | awk '{print $3}'

In the awk command, it asked to print the third column ($3) of the line. In this case, awk was considering default delimiter/separator. To specify exactly the tab (or others) as delimiter:

grep -i "client3" | awk 'BEGIN{FS="\t"}{print $3}'

or

grep -i "client3" | awk -F'\t' '{print $3}'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .