Path: utzoo!utgpu!water!watmath!clyde!att!rutgers!noao!arizona!rupley From: rupley@arizona.edu (John Rupley) Newsgroups: comp.sources.wanted Subject: Re: A column oriented (as in cut) grep command Summary: Do it with awk (1) -- correction 1.0 |-) Message-ID: <7251@megaron.arizona.edu> Date: 30 Sep 88 09:47:11 GMT References: <1725@teksce.SCE.TEK.COM> <871@dlhpedg.co.uk> Organization: U of Arizona CS Dept, Tucson Lines: 24 In article <871@dlhpedg.co.uk>, cl@datlog.co.uk (Charles Lambert) writes: > In article <1725@teksce.SCE.TEK.COM> johnhi@teksce.SCE.TEK.COM (John Higley) writes: > > > >Does anybody know of a version of grep that can be limited to certain columns > >in a text file? What I am thinking about is something that would have > >options such as -c and -f in the cut command. > > > >That reminds me, it would also be nice if it could tell the line number that > >the match was found on. > > "Awk" is designed to do this kind of job. An awk(1) command to find a pattern > in column two and print the line numbers would look something like this: ^^^^^^ > awk '$2 ~ /egrep-pattern/ {printf "%d: ", NR; print}' file To get _column_ 2, filename and line number, try: awk '{print FILENAME ":" NR ":" substr($0, column, 1)}' column=2 foo John Rupley internet: rupley@megaron.arizona.edu uucp: ..{cmcl2 | hao!ncar!noao}!arizona!rupley Dept. Biochemistry, Univ. Arizona, Tucson AZ 85721