Yes, and that way is awk. Read the manpage for it, and see where that gets you. It's a bit of a beast, though, kind of a 274-in-1 tool, and you only need about 3 of its features.
Or, depending on how complex your needs are, you could just write a little program in C or perl or your language of choice