user2268507
user2268507

Reputation:

Wrap piped unix commands in outer awk

I currently have the following:

grep -o 'Dr.*,\|Hon.*,\|Mr.*,\|Ms.*,\|Mrs.*,' parliament.txt | sed 's/,$//'

which leaves me with output such as:

Mr Andrew Wilkie
Mr Tony Windsor
Mr Ken Wyatt
Mr Tony Zappia

I want to print the original line based on a condition such as if the person's last name ended in a particular character? I'm guessing that I would have to use awk for this?

I'm quite new to Unix and so am not sure how this might be done.

Here are a few lines from the file:

NOTE: The last name will not always be in the third column

Ms Jill Hall, Member for Shortland
Mr Luke Hartsuyker, Member for Cowper
Mr Alex Hawke, Member for Mitchell

If I were looking for people whose last name ended in e, then I would print:

Mr Alex Hawke, Member for Mitchell

Thanks for your help!

Upvotes: 0

Views: 59

Answers (2)

Kalanidhi
Kalanidhi

Reputation: 5092

Try this,

sed -n '/^.*e,/p' filename 

Upvotes: 0

John1024
John1024

Reputation: 113924

It appears that the honorific starts in the first column and the last name ends just before the first comma. In that case, to find last names ending in e:

$ grep -E '^(Dr|Hon|Mr|Ms|Mrs)[^,]+e,' parliament.txt
Mr Alex Hawke, Member for Mitchell

The key addition is [^,]+e, which matches any number on non-commas, followed by e, followed by a comma.

I used -E to avoid all those backslashes. If your grep doesn't support extended regex, you will need to add the backslashes back in.

To get the whole of the original line, not just the part that matches, the -o option has been removed.

Upvotes: 1

Related Questions