1833457 Members
2760 Online
110052 Solutions
New Discussion

use grep command

 
SOLVED
Go to solution
Anwar D Singh
Occasional Contributor

use grep command

Hi,

how would I do the following commands?

Here is the file.

1. need to print where the persons first name starts with S.

2. Print lines where birthdays are in December.

3. print phone number in the 293 area code.

4. Last name begins with K or k

5. print lines preceded by a line where the salary is a six-figure digit.

Ephram Hardy:293-259-5395:235 CarltonLane, Joliet, IL 73858:8/12/20:56700
James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Barbara Kertz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/1/46:268500
Lesley Kirstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
William Kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Sir Lancelot:837-835-8257:474 Camelot Boulevard, Bath, WY 28356:5/13/69:24500


Your time and help is greatly appreciated.


2 REPLIES 2
Robin Wakefield
Honored Contributor
Solution

Re: use grep command

Hi Anwar,

1. grep ^S file
2. awk -F: '$4 ~ "^12"{print}' file
3. awk -F: '$2 ~ 293{print}' file
4. grep -i "^[^ ]* K" file
or
awk '$2 ~ "^[Kk]"{print}' file
5. awk -F: '$NF>=100000{getline;print}' file

Not all use grep as you can see.

Rgds, Robin
Brian Kinney
Frequent Advisor

Re: use grep command

If you wanted to avoid grep altogether, here's the answer for question 1.

awk '$1~/^S/{print $0}' file

Robin did a great job on the rest of them.


Now, for grins, let's combine questions 1-4 into a one line command (using awk, what else?)

awk '$1~/^S/ && $2~/^[Kk]/{print $0}' |
awk -F: '$2~/^293/ && $4~/^12/{print $0}'

Robin uses "print" while I use "print $0" (either I am too frequent of a user of shift, or I am just too "old school" with awk.)
"Any sufficiently advanced technology can be indistinguishable from magic" Arthur C. Clarke. My corollary - "Any advanced technology can be crushed with a sufficently large enough rock."