Skip to contentSkip to conversation

using grep with both -l and -n

DISCUSSION Jeffrey Holle 2 participants 2 messages Feb 20, 2004

lore
Share & tools
lore
#0 lore raw

I've actually got two questions about grep.

First, can the -l, which just prints the matching filenames, and -n, which includes the matched line number using the syntax "filename:lineno", be combined to affect.

What I find is that adding -n makes inclusion of -l a noop.

The reason I ask is I'm using grep to generate .gdbinit files. the syntax that -n emits is right on, except for the extra stuff that is normally suppressed by -l.

The second question is there a way to defeat the default behavior of not including the filename when -n is used and only one file is included in the search? In this case, it prints just the lineno.

#1 lore raw
Quoted 3 lines · Jeffrey Holle

The reason I ask is I'm using grep to generate .gdbinit files. the syntax that -n emits is right on, except for the extra stuff that is normally suppressed by -l.

Would this work for you:
awk '{print $1}'

Quoted 3 lines

The second question is there a way to defeat the default behavior of not including the filename when -n is used and only one file is included in the search? In this case, it prints just the lineno.

Add /dev/null to the list of files.

mrc

Signature · 3 lines
     Mike Castle      [email protected]      www.netcom.com/~dalgoda/
    We are all of us living in the shadow of Manhattan.  -- Watchmen
fatal ("You are in a maze of twisty compiler features, all different"); -- gcc