From owner-freebsd-questions@FreeBSD.ORG Thu Jul 21 05:13:05 2011 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id C43F9106568A for ; Thu, 21 Jul 2011 05:13:05 +0000 (UTC) (envelope-from perryh@pluto.rain.com) Received: from agora.rdrop.com (agora.rdrop.com [IPv6:2607:f678:1010::34]) by mx1.freebsd.org (Postfix) with ESMTP id 78BE98FC0C for ; Thu, 21 Jul 2011 05:13:05 +0000 (UTC) Received: from agora.rdrop.com (66@localhost [127.0.0.1]) by agora.rdrop.com (8.13.1/8.12.7) with ESMTP id p6L5D2Ir037179 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Wed, 20 Jul 2011 22:13:04 -0700 (PDT) (envelope-from perryh@pluto.rain.com) Received: (from uucp@localhost) by agora.rdrop.com (8.13.1/8.12.9/Submit) with UUCP id p6L5D2YK037178; Wed, 20 Jul 2011 22:13:02 -0700 (PDT) Received: from fbsd81 ([192.168.200.81]) by pluto.rain.com (4.1/SMI-4.1-pluto-M2060407) id AA22340; Wed, 20 Jul 11 22:07:28 PDT Date: Thu, 21 Jul 2011 05:07:51 -0700 From: perryh@pluto.rain.com To: kline@thought.org Message-Id: <4e281697.2F3Hm7eEgDK4U/K6%perryh@pluto.rain.com> References: <20110721004208.GA32046@thought.org> In-Reply-To: <20110721004208.GA32046@thought.org> User-Agent: nail 11.25 7/29/05 Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=_4e281697.EA7hk6x3M9mKqgk9x+5jKYLWon8yCWwX1dMwBfLk/lsBqy4j" Cc: freebsd-questions@freebsd.org Subject: Re: scrpt help neded... X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 21 Jul 2011 05:13:05 -0000 This is a multi-part message in MIME format. --=_4e281697.EA7hk6x3M9mKqgk9x+5jKYLWon8yCWwX1dMwBfLk/lsBqy4j Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline Gary Kline wrote: > I'm looking for a script that takes on arg and lets me vi/vim > into the r esults. Let's say that I'm looking for the string > 201107 in a slew of files. the script find it with grep---not > grep -w, just grep. collect es the filenames and lines (grep -n) > and saves then temporarily, then points vim or vi at each > file+linenumbr and execs it for me. the fewer keystrokes, the > better. To edit each file that contains 201107: $ vi ` grep -l 201107 {files to be searched} ` That won't pre-position within the files, but since it's a single invocation of vi, with each subsequent file being loaded by :n, a search pattern will persist (unless/until you replace it by entering a different search pattern). At the top of the first file, you enter /201107 to find the first instance, "n" to find the second, etc. After :n -- at the top of the second file -- "n" alone will find the first instance. OTOH if you want to bring up an xterm containing _the results of the grep_ you can pipe it into the attached script. There is no manpage, but the comments and the (straightforward) parameter decoding should provide a start. (There are a few "magic numbers", which ideally should be tweaked for your X11 installation's font dimensions, but nothing horrible will happen if they are slightly off.) --=_4e281697.EA7hk6x3M9mKqgk9x+5jKYLWon8yCWwX1dMwBfLk/lsBqy4j Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="xless" #!/usr/local/bin/bash # The maxl and maxw calculations involve "magic numbers," which ideally # ought to be extracted from xterm and window-manager settings rather # than being hard-coded. Good luck figuring out a way to do that. # # The xterm font is 6w x 13h # # visible title bar height incl top frame = 29 pixels # + xterm margin inside frame = 1 pixel # + bottom frame & margin = 8 pixels (same as frame widths below) # + window-manager shadow = 1 pixel # => height available for text = screen height - 39 pixels # maxl=`(xwininfo -root | sed -n -e 's/ Height: //p' ; echo 39 - 13 / p) | dc` # # visible frame width = 7 pixels # + xterm margin inside frame = 1 pixel # * 2 sides => total width of side frames = 16 pixels # + window-manager shadow = 1 pixel # => width available for text = screen width - 17 pixels # maxw=`(xwininfo -root | sed -n -e 's/ Width: //p' ; echo 17 - 6 / p) | dc` # maxw "should" be used in conjunction with the max line length found in the # file to automatically set the width (as is already being done for the length). # Set defaults w=80 l=0 n=stdin flags="" n_is_default=1 w_is_default=1 # Handle flag params while [[ "$1" == -?* ]] ; do case "$1" in -w ) shift w="$1" w_is_default=0 ;; -w* ) w="${1#-w}" w_is_default=0 ;; -l ) shift l="$1" ;; -l* ) l="${1#-l}" ;; -n ) shift n="$1" n_is_default=0 ;; -n* ) n="${1#-n}" n_is_default=0 ;; * ) flags="$flags $1" esac shift done # Check for no params => stdin, or 1st param of "-" (explicit stdin), and # if so copy stdin to a file since there seems no way to get it passed to # the "less" which will be running in the xterm. Note that "-" will not # work as any but the first non-flag parameter. if [ "x$1" == "x" -o "$1" == "-" ] ; then cat > /tmp/xless$$ shift if [ "$l" == "0" ] ; then l="`(head -$maxl /tmp/xless$$ | fold -w$w | wc -l ; echo \"1 + d [$maxl p q] sa $maxl