Jane Dallaway

Jane Dallaway

Jane Dallaway  //  Data loving developer/leader/product shaper, storyline curator/creator, life-long learner, photographer, dog owner, reader, crafter, gardener and occasional snowboarder

This blog contains all sorts of odds and ends, from event reviews, stuff about my storyline project, bits of craft, through thoughts on learning, to photography stuff, and general inspiration things. It's a bit all over the place with no real theme, but then so am I!

Email: jane @ dallaway.com
Also at:    

NTS: Use of grep to count instances of a certain phrase in a file

I got an email from Geni (more here) today asking how many people I had in my GEDCOM file to import. A GEDCOM file is a formatted text file with a format similar to the following (taken from the wikipedia example):
0 @I1@ INDI
1 NAME Bob /Cox/
1 SEX M
1 FAMS @F1@
1 CHAN
2 DATE 11 FEB 2006
0 @I2@ INDI
1 NAME Joann /Para/
1 SEX F
1 FAMS @F1@
1 CHAN
2 DATE 11 FEB 2006
0 @I3@ INDI
1 NAME Bobby Jo /Cox/
1 SEX M
1 FAMC @F1@
1 CHAN
2 DATE 11 FEB 2006
0 @F1@ FAM
1 HUSB @I1@
1 WIFE @I2@
1 MARR
1 CHIL @I3@

To get the number of people I would want to import, I wanted to count the number of instances of "1 NAME" entries. I knew that Richard would be able to provide a nice easy answer that wouldn't involve lots of scripting. And of course he pointed me in the direction of grep and wc.

The finished product is grep '1 NAME' /Users/jane/Documents/file.GED | wc -l which searches for '1 NAME' amongst the file /Users/jane/Documents/file.GED and then pipes the output into the wc command specifying -l to get the line count.

Filed under  //  nts   unix  

Comments (0)