How to extract specific data from the table?

0

Here is the table...

Group   Name            Designation
2       (John)          Front End Developer
12      (Jim)           Back End Developer
8       (Jill)          Full Stack Developer
21      (Jack)          Front End Developer
2       (James)         Front End Developer
12      (Jane)          Full Stack Developer

I want to extract person names belong to same group. Here John and James belong to group 2. What (combination of) bash commands or script should I use to display the following output

John
James

I used different types of grep combinations. But doesn't seem to work.

Nayab Basha Sayed

Posted 2016-06-15T04:29:37.037

Reputation: 53

Let's see the various combinations you tried. – Michael Vehrs – 2016-06-15T06:03:04.210

I tried this... grep -w "2" file.txt | awk '{print $2}' This printed names along with braces like this... (John) (James) I couldn't get rid of those parenthesis – Nayab Basha Sayed – 2016-06-15T08:25:19.843

Answers

0

You could use sed like this:

sed -n '/^2 /s/.*(\([^)]\+\)).*/\1/p' file.txt

Or awk like this:

awk -F "[()]" '/^2 / {print $2}' file.txt

The first solution replaces the line with the string enclosed in parentheses before printing it. The second solution uses parentheses as field separators and then only prints field two (the enclosed string).

Michael Vehrs

Posted 2016-06-15T04:29:37.037

Reputation: 255

Hi @Michael above both commands didn't produce any results. But I used this. grep -w "2" file.txt | awk -F "[()]" '{print $2}'. Worked fine. Thanks for the answer. – Nayab Basha Sayed – 2016-06-15T13:07:42.723

Presumably, your file contains tabs instead of blanks, then. – Michael Vehrs – 2016-06-15T17:19:07.740