buggrep - Bugs: bug #19337, Whish: Include Option to view...

 
 

You are not allowed to post comments on this tracker with your current authentication level.

bug #19337: Whish: Include Option to view first line of file/stream

Submitter:  None
Submitted:  Sun 18 Mar 2007 12:05:55 PM UTC
   
 
Category:  None Severity:  3 - Normal
Item Group:  None Status:  None
Privacy:  Public Assigned to:  None
Open/Closed:  Closed

Fri 04 Dec 2009 07:25:14 PM UTC, comment #5: 

The right solution is sed '1b;/foo/b;d' or something like that

Paolo Bonzini <bonzini>
Fri 14 Mar 2008 06:53:07 AM UTC, comment #4: 


> ps aux | head -1; ps aux | grep foo


Another way to provide this is to include a grep pattern that will match the first line.  Example:

  ps aux | grep -e ^USER -e foo

> ps aux | (head -1; grep foo)
> but that didn't work consistently for me across all systems I
> work on. I suspect it only works with the most recent versions
> of coreutils. (If someone knows why, please add to the
> discussion.)


That is almost certainly due to head using libc's stdio library which on different systems is buffering input.  After head has read it the data is no longer available for grep to read.

Bob Proulx <rwp>
Group Member
Thu 13 Mar 2008 11:47:44 PM UTC, comment #3: 

I've often wanted this functionality and would welcome a 'head' option to grep. I feel it is similar to the A and B options in that it provides additional context. '--head=2' would include the first two lines of STDIN. '--head' would assume a default of 1 line.

In SQL, if I do:
 
  SELECT * from EMP;

I get column headers followed by the data. If I filter the rows by adding a WHERE clause, I still get the column headers. Likewise, if I filter the output of 'ps aux', I still want the column headers.

I did come up with a workaround:

$ ps aux | (read line; echo "$line"; grep foo)

On some systems, this can be shortened to:

$ ps aux | (head -1; grep foo)

but that didn't work consistently for me across all systems I work on. I suspect it only works with the most recent versions of coreutils. (If someone knows why, please add to the discussion.)

Allen Halsey

Allen Halsey <ahalsey>
Thu 19 Apr 2007 06:37:02 PM UTC, comment #2: 

Hi,

Yes, i know head - but this methode has some drawbacks:

 - recursive search, within files with different headers not easy
 - needs to start the same command twice
   - not possible, if the output is different each start
   - tackes twice as long (if the execution takes very long)
 - more keystrokes :)

ciao, Daniel Weigl

Daniel Weigl <danwei>
Sun 18 Mar 2007 02:03:52 PM UTC, comment #1: 

The utitility 'head' provides the desired functionality. For example, The desired output from

$ ps aux | grep --head foo


can be achieved using:

ps aux | head -1; ps aux | grep foo

Other shell controls can be used in combination with head to achieve this functionality when searching multiple files.

Tony Abou-Assaleh <taa>
Group administrator
Sun 18 Mar 2007 12:05:55 PM UTC, original submission:  

Hello,

Would it be possible to add a command line flag, to set grep, that it would show the first (or the n-first) line(s) of a file (or stream) with matching items?

eg, if you have a command wich has a table-like output, you could easily include the header, so you know which column means what...

eg:
$ ps aux | grep --head foo

(so, --head would mean to include first line, and maybe --head5 the first 5...)

Imho this wouldnt brak any backward compatibility - or?

tia Daniel Weigl - DanielWeigl-at-gmx.at

Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by bonzini (Posted a comment)
  • -email is unavailable- added by rwp (Posted a comment)
  • -email is unavailable- added by ahalsey (Posted a comment)
  • -email is unavailable- added by danwei (Posted a comment)
  • -email is unavailable- added by taa (Posted a comment)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

     

    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2009-12-04 bonzini Open/ClosedOpen Closed

    Back to the top

    Powered by Savane 3.13-bb6a.
    Corresponding source code