Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Using sed, how to print all lines where the birthdays are in November or December?

Assuming input file name "datebook" as follows:

Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300
Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400
Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92743:7/25/53:85100
Fred Fardbarkle:674-843-1385:20 Parak Lane, Duluth, MN 23850:4/12/23:780900
Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Paco Gutierrez:835-365-1284:454 Easy Street, Decatur, IL 75732:2/28/53:123500
Ephram Hardy:293-259-5395:235 CarltonLane, Joliet, IL 73858:8/12/20:56700
ABE LINCOLN:813-555-0123:1549 Cabin Drive, Springfield, IL 61801:2/12/09:79000
James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
share|improve this question
    
Are the dates in M/D/Y or D/M/Y format? –  grawity Oct 4 '12 at 17:16

2 Answers 2

egrep '^[^:]*:[^:]*:[^:]*:(11|12)/'
sed -rn '/^[^:]*:[^:]*:[^:]*:(11|12)\//p'
  • /regex/p – print lines matching a regex
  • ^ – beginning of line
  • [^:]+ – first, second, third fields (any number of any characters except :)
  • : – field separators
  • (11|12)\/ – either 11 or 12, followed by a /
awk -F: '$4 ~ /^(11|12)\//'
perl -aF: -ne 'print if $F[3] =~ /^(11|12)\//'
share|improve this answer
1  
Could you please explain the sed command in details? how come we have so many [^:]+ ? Thank you –  Steve Oct 4 '12 at 17:35
    
Could you please read a regexp tutorial? [], ^, * are explained in many places. thank you –  grawity Oct 4 '12 at 17:41
1  
Could have done shorter? I'd have written ^([^:]+:){3}(11|12), or even (looking at the format) :(11|12)\/. –  m4573r Oct 4 '12 at 17:48
1  
or, anchor at the end: /:1[12][^:]*:[^:]*$/ –  glenn jackman Oct 4 '12 at 23:15
1  
Your side-by-side comparison demonstrates the advantages of a language that knows what a field is (awk and perl) versus ones that just see streams of characters (egrep and sed). –  Isaac Rabinovitch Oct 5 '12 at 5:41

Not sed, but grep is easier and more appropriate.

grep -E "(11|12)/\d{1,2}/\d{1,2}" steve.txt

(11|12) means match either 11 or 12 and \d{1,2} means match 1 or 2 digits.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.