easy for some

Larry McVoy lm at slovax.Eng.Sun.COM
Thu May 9 06:01:14 AEST 1991


matthew at gizmo.UK.Sun.COM (Matthew Buller - Sun EHQ - MIS) writes:
> problem: to extract text between start and end patterns in a file
> eg:-
> 
> file:
> 
> pattern1---
> 
> stuff
> stuff
> stuff
> 
> pattern2---

/bin/sh, usage shellscript start_pat stop_pat [files...]

	START=$1; shift
	STOP=$1; shift
	PRINT=
	cat $* | while read x
	do	if [ "$x" = "$STOP" ]
		then	exit 0;
		fi
		if [ "$x" = "$START" ]
		then	PRINT=yes
			continue
		fi
		if [ X$PRINT != X ]
		then	echo "$x";
		fi
	done

/bin/perl, same usage (see the notes on the ".." operator, cool thingy).

	$START = shift;
	$STOP = shift;
	while (<>) {
		if (/^$START$/../^$STOP/) {
			next if /^$START$/;	# skip starting pattern
			last if /^$STOP/;	# done if last;
			print;
		}
	}
---
Larry McVoy, Sun Microsystems     (415) 336-7627       ...!sun!lm or lm at sun.com



More information about the Comp.unix.wizards mailing list