9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] grep: always show path with '-n'
@ 2022-02-02 23:01 ori
  2022-02-03  5:14 ` unobe
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: ori @ 2022-02-02 23:01 UTC (permalink / raw)
  To: 9front

With the new 'g', I noticed that sometimes
we'd fail to show the filename; this happens
because 'g' now uses xargs, and if there's
exactly one file picked up by xargs, the
Hflag does not get set.

This change makes grep now print the filename
if Hflag *or* Nflag is set, which is what I'd
expect.

Before:

	% grep -n -- Nflag main.c
	106: 		flag |= Nflag;		/* count only */

After:

	% grep -n -- Nflag main.c
	main.c:106: 		flag |= Nflag;		/* count only */

And it still does something sensible for stdin:

	% grep -n asdf
	asdf
	stdin:1: asdf

Does this make sense to people?


diff 251c3cfd610abd169676852d301a2aa1267c0e57 uncommitted
--- a/sys/src/cmd/grep/main.c
+++ b/sys/src/cmd/grep/main.c
@@ -180,7 +180,7 @@
 			count++;
 			if(flag & (Cflag|Sflag|Llflag|LLflag))
 				goto cont;
-			if(flag & Hflag)
+			if(flag & (Hflag|Nflag))
 				Bprint(&bout, "%s:", file);
 			if(flag & Nflag)
 				Bprint(&bout, "%ld: ", lineno);
@@ -219,7 +219,7 @@
 			count++;
 			if(flag & (Cflag|Sflag|Llflag|LLflag))
 				goto conti;
-			if(flag & Hflag)
+			if(flag & (Hflag|Nflag))
 				Bprint(&bout, "%s:", file);
 			if(flag & Nflag)
 				Bprint(&bout, "%ld: ", lineno);


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] grep: always show path with '-n'
  2022-02-02 23:01 [9front] grep: always show path with '-n' ori
@ 2022-02-03  5:14 ` unobe
  2022-02-03  7:04 ` Steve Simon
  2022-02-05  4:58 ` 有澤 健治
  2 siblings, 0 replies; 13+ messages in thread
From: unobe @ 2022-02-03  5:14 UTC (permalink / raw)
  To: 9front


Makes sense to me.

Quoth ori@eigenstate.org:
> With the new 'g', I noticed that sometimes
> we'd fail to show the filename; this happens
> because 'g' now uses xargs, and if there's
> exactly one file picked up by xargs, the
> Hflag does not get set.
> 
> This change makes grep now print the filename
> if Hflag *or* Nflag is set, which is what I'd
> expect.
> 
> Before:
> 
> 	% grep -n -- Nflag main.c
> 	106: 		flag |= Nflag;		/* count only */
> 
> After:
> 
> 	% grep -n -- Nflag main.c
> 	main.c:106: 		flag |= Nflag;		/* count only */
> 
> And it still does something sensible for stdin:
> 
> 	% grep -n asdf
> 	asdf
> 	stdin:1: asdf
> 
> Does this make sense to people?
> 
> 
> diff 251c3cfd610abd169676852d301a2aa1267c0e57 uncommitted
> --- a/sys/src/cmd/grep/main.c
> +++ b/sys/src/cmd/grep/main.c
> @@ -180,7 +180,7 @@
>  			count++;
>  			if(flag & (Cflag|Sflag|Llflag|LLflag))
>  				goto cont;
> -			if(flag & Hflag)
> +			if(flag & (Hflag|Nflag))
>  				Bprint(&bout, "%s:", file);
>  			if(flag & Nflag)
>  				Bprint(&bout, "%ld: ", lineno);
> @@ -219,7 +219,7 @@
>  			count++;
>  			if(flag & (Cflag|Sflag|Llflag|LLflag))
>  				goto conti;
> -			if(flag & Hflag)
> +			if(flag & (Hflag|Nflag))
>  				Bprint(&bout, "%s:", file);
>  			if(flag & Nflag)
>  				Bprint(&bout, "%ld: ", lineno);
> 


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] grep: always show path with '-n'
  2022-02-02 23:01 [9front] grep: always show path with '-n' ori
  2022-02-03  5:14 ` unobe
@ 2022-02-03  7:04 ` Steve Simon
  2022-02-03 16:47   ` ori
  2022-02-05  4:58 ` 有澤 健治
  2 siblings, 1 reply; 13+ messages in thread
From: Steve Simon @ 2022-02-03  7:04 UTC (permalink / raw)
  To: 9front

the “traditional” way to achieve this is for g(1) to always pass /dev/null to grep to ensure there are always multiple files.

-Steve


On 3 Feb 2022, at 5:34 am, ori@eigenstate.org wrote:

With the new 'g', I noticed that sometimes
we'd fail to show the filename; this happens
because 'g' now uses xargs, and if there's
exactly one file picked up by xargs, the
Hflag does not get set.

This change makes grep now print the filename
if Hflag *or* Nflag is set, which is what I'd
expect.

Before:

   % grep -n -- Nflag main.c
   106:        flag |= Nflag;        /* count only */

After:

   % grep -n -- Nflag main.c
   main.c:106:        flag |= Nflag;        /* count only */

And it still does something sensible for stdin:

   % grep -n asdf
   asdf
   stdin:1: asdf

Does this make sense to people?


diff 251c3cfd610abd169676852d301a2aa1267c0e57 uncommitted
--- a/sys/src/cmd/grep/main.c
+++ b/sys/src/cmd/grep/main.c
@@ -180,7 +180,7 @@
           count++;
           if(flag & (Cflag|Sflag|Llflag|LLflag))
               goto cont;
-            if(flag & Hflag)
+            if(flag & (Hflag|Nflag))
               Bprint(&bout, "%s:", file);
           if(flag & Nflag)
               Bprint(&bout, "%ld: ", lineno);
@@ -219,7 +219,7 @@
           count++;
           if(flag & (Cflag|Sflag|Llflag|LLflag))
               goto conti;
-            if(flag & Hflag)
+            if(flag & (Hflag|Nflag))
               Bprint(&bout, "%s:", file);
           if(flag & Nflag)
               Bprint(&bout, "%ld: ", lineno);


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] grep: always show path with '-n'
  2022-02-03  7:04 ` Steve Simon
@ 2022-02-03 16:47   ` ori
  2022-02-03 19:04     ` Nicola Girardi
  0 siblings, 1 reply; 13+ messages in thread
From: ori @ 2022-02-03 16:47 UTC (permalink / raw)
  To: 9front

Quoth Steve Simon <steve@quintile.net>:
> the “traditional” way to achieve this is for g(1) to always pass
> /dev/null to grep to ensure there are always multiple files.
> 

That's certainly *an* option, but it
seems surprising that 1 vs n args are
treated differently for '-n', and I
think it makes sense to make it
consistent.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] grep: always show path with '-n'
  2022-02-03 16:47   ` ori
@ 2022-02-03 19:04     ` Nicola Girardi
  2022-02-04  0:47       ` ori
                         ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Nicola Girardi @ 2022-02-03 19:04 UTC (permalink / raw)
  To: 9front

Quoth ori@eigenstate.org:
> Quoth Steve Simon <steve@quintile.net>:
> > the “traditional” way to achieve this is for g(1) to always pass
> > /dev/null to grep to ensure there are always multiple files.
> > 
> 
> That's certainly *an* option, but it
> seems surprising that 1 vs n args are
> treated differently for '-n', and I
> think it makes sense to make it
> consistent.

I've always wondered about that behavior for '-n'.  Does anyone know
the historical reason for omitting the file name when there's only one
file to search?


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] grep: always show path with '-n'
  2022-02-03 19:04     ` Nicola Girardi
@ 2022-02-04  0:47       ` ori
  2022-02-04  0:48       ` ori
  2022-02-04 12:39       ` Alex Musolino
  2 siblings, 0 replies; 13+ messages in thread
From: ori @ 2022-02-04  0:47 UTC (permalink / raw)
  To: 9front

Quoth Nicola Girardi <ng@0x80.stream>:
> Quoth ori@eigenstate.org:
> > Quoth Steve Simon <steve@quintile.net>:
> > > the “traditional” way to achieve this is for g(1) to always pass
> > > /dev/null to grep to ensure there are always multiple files.
> > > 
> > 
> > That's certainly *an* option, but it
> > seems surprising that 1 vs n args are
> > treated differently for '-n', and I
> > think it makes sense to make it
> > consistent.
> 
> I've always wondered about that behavior for '-n'.  Does anyone know
> the historical reason for omitting the file name when there's only one
> file to search?
> 

It removes a processing step to remove a prefix,
I guess.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] grep: always show path with '-n'
  2022-02-03 19:04     ` Nicola Girardi
  2022-02-04  0:47       ` ori
@ 2022-02-04  0:48       ` ori
  2022-02-04 22:00         ` Nicola Girardi
  2022-02-04 12:39       ` Alex Musolino
  2 siblings, 1 reply; 13+ messages in thread
From: ori @ 2022-02-04  0:48 UTC (permalink / raw)
  To: 9front

Quoth Nicola Girardi <ng@0x80.stream>:
> Quoth ori@eigenstate.org:
> > Quoth Steve Simon <steve@quintile.net>:
> > > the “traditional” way to achieve this is for g(1) to always pass
> > > /dev/null to grep to ensure there are always multiple files.
> > > 
> > 
> > That's certainly *an* option, but it
> > seems surprising that 1 vs n args are
> > treated differently for '-n', and I
> > think it makes sense to make it
> > consistent.
> 
> I've always wondered about that behavior for '-n'.  Does anyone know
> the historical reason for omitting the file name when there's only one
> file to search?
> 

It removes a processing step to remove a prefix,
I guess.

This doesn't change that behavior -- it just makes
'-n' always prefix 'file:line' instead of just 'line'.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] grep: always show path with '-n'
  2022-02-03 19:04     ` Nicola Girardi
  2022-02-04  0:47       ` ori
  2022-02-04  0:48       ` ori
@ 2022-02-04 12:39       ` Alex Musolino
  2 siblings, 0 replies; 13+ messages in thread
From: Alex Musolino @ 2022-02-04 12:39 UTC (permalink / raw)
  To: 9front

> I've always wondered about that behavior for '-n'.  Does anyone know
> the historical reason for omitting the file name when there's only one
> file to search?

Perhaps the original grep only searched one file.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] grep: always show path with '-n'
  2022-02-04  0:48       ` ori
@ 2022-02-04 22:00         ` Nicola Girardi
  2022-02-05 16:57           ` hiro
  0 siblings, 1 reply; 13+ messages in thread
From: Nicola Girardi @ 2022-02-04 22:00 UTC (permalink / raw)
  To: 9front

Quoth ori@eigenstate.org:
> Quoth Nicola Girardi <ng@0x80.stream>:
> > Quoth ori@eigenstate.org:
> > > Quoth Steve Simon <steve@quintile.net>:
> > > > the “traditional” way to achieve this is for g(1) to always pass
> > > > /dev/null to grep to ensure there are always multiple files.
> > > > 
> > > 
> > > That's certainly *an* option, but it
> > > seems surprising that 1 vs n args are
> > > treated differently for '-n', and I
> > > think it makes sense to make it
> > > consistent.
> > 
> > I've always wondered about that behavior for '-n'.  Does anyone know
> > the historical reason for omitting the file name when there's only one
> > file to search?
> > 
> 
> It removes a processing step to remove a prefix,
> I guess.
> 
> This doesn't change that behavior -- it just makes
> '-n' always prefix 'file:line' instead of just 'line'.

Sure, by the way, I don't oppose the change, it makes sense to me.
I was just curious because it seems like it should've been done long ago.
Perhaps people have so far worried about breaking scripts.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] grep: always show path with '-n'
  2022-02-02 23:01 [9front] grep: always show path with '-n' ori
  2022-02-03  5:14 ` unobe
  2022-02-03  7:04 ` Steve Simon
@ 2022-02-05  4:58 ` 有澤 健治
  2 siblings, 0 replies; 13+ messages in thread
From: 有澤 健治 @ 2022-02-05  4:58 UTC (permalink / raw)
  To: 9front

Hello,

On 2022/02/03 8:01, ori@eigenstate.org wrote:
> And it still does something sensible for stdin:
>
> 	% grep -n asdf
> 	asdf
> 	stdin:1: asdf
>
> Does this make sense to people?
Isn't it logically better:

% grep -n asdf
asdf
:1: asdf



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] grep: always show path with '-n'
  2022-02-04 22:00         ` Nicola Girardi
@ 2022-02-05 16:57           ` hiro
  2022-02-05 17:06             ` Stanley Lieber
  0 siblings, 1 reply; 13+ messages in thread
From: hiro @ 2022-02-05 16:57 UTC (permalink / raw)
  To: 9front

it should be a no-brainer.

i have my own g, obviously it doesn't suffer from the shortcoming
discussed here. i'm shocked it took people so long to notice.

also remember that 'g' originally was meant more as an example of
personalization.
everybody could have their own 'g', it wasn't really meant as a system utility.

system utilities otoh aren't short single-letter scripts, leaving
those names free for dumping conveniently into them your most common
repetitive tasks...

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] grep: always show path with '-n'
  2022-02-05 16:57           ` hiro
@ 2022-02-05 17:06             ` Stanley Lieber
  2022-02-05 18:00               ` hiro
  0 siblings, 1 reply; 13+ messages in thread
From: Stanley Lieber @ 2022-02-05 17:06 UTC (permalink / raw)
  To: 9front

On February 5, 2022 4:57:09 PM UTC, hiro <23hiro@gmail.com> wrote:
>it should be a no-brainer.
>
>i have my own g, obviously it doesn't suffer from the shortcoming
>discussed here. i'm shocked it took people so long to notice.
>
>also remember that 'g' originally was meant more as an example of
>personalization.
>everybody could have their own 'g', it wasn't really meant as a system utility.
>
>system utilities otoh aren't short single-letter scripts, leaving
>those names free for dumping conveniently into them your most common
>repetitive tasks...
>

i imported g from p9p. i believe it was one of rsc's scripts. we've updated it several times. i don't think i've used it since it was imported, though...

sl

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] grep: always show path with '-n'
  2022-02-05 17:06             ` Stanley Lieber
@ 2022-02-05 18:00               ` hiro
  0 siblings, 0 replies; 13+ messages in thread
From: hiro @ 2022-02-05 18:00 UTC (permalink / raw)
  To: 9front

i never ever even ran it but i liked the idea. iirc he must have
presented this lesson on 9fans at some point

On 2/5/22, Stanley Lieber <sl@stanleylieber.com> wrote:
> On February 5, 2022 4:57:09 PM UTC, hiro <23hiro@gmail.com> wrote:
>>it should be a no-brainer.
>>
>>i have my own g, obviously it doesn't suffer from the shortcoming
>>discussed here. i'm shocked it took people so long to notice.
>>
>>also remember that 'g' originally was meant more as an example of
>>personalization.
>>everybody could have their own 'g', it wasn't really meant as a system
>> utility.
>>
>>system utilities otoh aren't short single-letter scripts, leaving
>>those names free for dumping conveniently into them your most common
>>repetitive tasks...
>>
>
> i imported g from p9p. i believe it was one of rsc's scripts. we've updated
> it several times. i don't think i've used it since it was imported,
> though...
>
> sl
>

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2022-02-05 19:12 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-02 23:01 [9front] grep: always show path with '-n' ori
2022-02-03  5:14 ` unobe
2022-02-03  7:04 ` Steve Simon
2022-02-03 16:47   ` ori
2022-02-03 19:04     ` Nicola Girardi
2022-02-04  0:47       ` ori
2022-02-04  0:48       ` ori
2022-02-04 22:00         ` Nicola Girardi
2022-02-05 16:57           ` hiro
2022-02-05 17:06             ` Stanley Lieber
2022-02-05 18:00               ` hiro
2022-02-04 12:39       ` Alex Musolino
2022-02-05  4:58 ` 有澤 健治

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).