Re: grabbing a piece of file

From: Chris F.A. Johnson (c.f.a.johnson_at_rogers.com)
Date: 09/25/03


Date: 25 Sep 2003 20:10:35 GMT

On Thu, 25 Sep 2003 at 07:06 GMT, Francesco wrote:
> Hi all,
> I'd like to write a program that collect a piece of a file. I should start
> with the first occurrence of an Id and finish with the last occurrence of
> it.
> Let me explain better: supposing the id is 999999
>
> ..............
> ..............
> 9999999 from here
> .............
> .............
> 9999999 to here
> ..............
>
> Can anybody give me a clue how to do it ?

    There are many ways; the best depends on several factors,
    including file size and whether the pattern you are searching for
    is on a line by itself or not.

    If the file is not too large to fit into memory:

[ $# -lt 2 ] && { echo "USAGE: ${0##*/} pattern file" >&2; exit; }
pattern=${1}
file=`cat "$2"` ## bash2: file=`< "$2"`
file="${file%"$pattern"*}$pattern"
file="$pattern${file#*"$pattern"}"
echo "$file"

    Otherwise there is Ed Morton's awk solution or this (which will
    probably be a bit slower than Ed's script):

[ $# -lt 2 ] && { echo "USAGE: ${0##*/} pattern file" >&2; exit; }
eval list=\"`grep -n "$1" "$2" | cut -d: -f1`\"
first=${list%% *}
last=${list##* }
sed -n "$first,$last p" "$2"

    ...though the while thing can probably be put into a sed script
    which would speed it up.

    If the pattern is not on a line by itself, then both my grep/sed
    script and Ed's awk script will return the entire line containing
    the first and last instances of the pattern.

-- 
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License


Relevant Pages

  • Re: PHP web design patterns
    ... use observer pattern terminology). ... functions and, my goal, one script that handles all the processing. ... the html pages being the view and the processing script ... and some guidelines from "Database Design for Mere Mortals," which I ...
    (alt.php)
  • Re: Lotus 123 conditionally changing cell colours.....
    ... This is a macro command, not a script command. ... pattern is an offset number from 0 through 63 that specifies a pattern. ...
    (comp.os.os2.apps)
  • Re: Perl script to extract data from webpage? (knucklehead newbie).
    ... Your $& is a special perl variable that represents the string matched by ... the last successful pattern match...which in the case of your example ... I have this running now using a script I found elsewhere, ...
    (comp.lang.perl)
  • Re: pipes ifs and suggestions
    ... I'm upon my first script i' writing mainly for exercitation than utility. ... It should search for a given pattern in a given text file and show the ... See the man pages for the details, in particular for xargs which is a bit ... echo error: ...
    (comp.unix.shell)
  • Re: A character string matches a hex numeric pattern
    ... In an awk script I have a pattern ... that the string "Note:" should not have matched the ... I repeat: anchor the pattern to the beginning of the string: ...
    (comp.lang.awk)