johnhi@teksce.SCE.TEK.COM (John Higley) (09/23/88)
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. Some people might suggest piping the output of cut into grep, but that loses the other sections of the line which are needed to determine what line it found. That reminds me, it would also be nice if it could tell the line number that the match was found on. Thanks for listening to my dreams. John Higley johnhi@teksce.SCE.TEK.COM
cl@datlog.co.uk (Charles Lambert) (09/28/88)
I'm posting this, rather than mailing to the originator, because you awk(1) experts may want to correct it. 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 Putting this in a shell script with appropriate argument-passing would give you the tool you need. ---------- Charlie
rupley@arizona.edu (John Rupley) (09/30/88)
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