🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: ca.unix
Article #1114

Re: grep question

#1114
From: Hiro Sugawara
Date: Wed, 10 May 1995 00:00
38 lines
1965 bytes
In article <3ooo33$q17@isp1.isp.net> scooper@denkart.com (Steven M. Cooper) writes:
>Dan (kolohe@rahul.net) wrote:
>
>: Grep "string" *  returns lines containing the string preceded with the
>: filename:
>:               filea:"xxxxxxxxxxstringxxxxxxxxxxxx"
>:               fileb:"xxxxxxxxxxxxxxxxxxstringxxxx"
>:               etc.
>
>: Unfortunately, 'head -20 * |grep "string"  will not return the file name
>: because grep operates on the results from 'head'.  Anyone know of a way
>: to restrict grep to search only the first 20 lines of a file OR a way
>: to make the head/grep piped command display the filename?
>
>As is typical with UNIX, there are dozens of ways to do this, all of
>which are cryptic.  One way would be to "cut" out the first field of
>grep's output, telling it that the "field" is delimited by a colon.
>"cut" is a good way to do vertical slicing; "head" and "tail" do
>horizontal slices.  Anyway, try:
>
>grep "string" * | cut -f1 -d:
>
>There are options to grep if you only want a file listed once.

I think the original poster seeks a way to RETAIN the file name portion.
I'd recommend writing a small awk program looking like the following:

FNR <= 10 && $0 ~ /string/ { print FILENAME ":" $0 }

Sorry, I'm still an awk beginner and I may be worng.
Good luck!

hiro
-- 
--
Hiro Sugawara       hsugawar@us.oracle.com   +1(415)506-9336
Oracle Corporation      MPD - Massively Parallel Development
500 Oracle Parkway, Box 659107, Redwood Shores, CA 94065 USA
Technical Details
Message-ID: <3or510$q88@inet-nntp-gw-1.us.oracle.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <3ohlb8$44h@hustle.rahul.net> <3ooo33$q17@isp1.isp.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10183f,c3c7cc1ab80bf082
X-Google-Attributes: gid10183f,public
From: hsugawar@us.oracle.com (Hiro Sugawara)
Subject: Re: grep question
Date: 1995/05/10
Message-ID: <3or510$q88@inet-nntp-gw-1.us.oracle.com>
X-Deja-AN: 102501309
distribution: world
references: <3ohlb8$44h@hustle.rahul.net> <3ooo33$q17@isp1.isp.net>
organization: Oracle Corporation
newsgroups: ca.unix