[PLUG] How to pull out a particular line from a textfile

Russell Evans revans at e-z.net
Thu Sep 19 04:01:55 UTC 2002


I find myself using recycling so I try for easy to change, easy to copy into a
function in another script. I also tend to need to capture when thing don't
happen correctly. YMMV

#!/bin/sh

FILE=somefile
LINELIST="1 2"
MIN=2 

declare -a WHATIWANT

DoWhatEver () {
	echo
}	


if [ -f $FILE ] ; then
	WHATIWANT=( `cat $FILE` )
else
	echo "$FILE does not exist"
	exit 1
fi

if [ ${#WHATIWANT[@]} -ge $MIN ] ; then
	for i in $LINELIST ; do
		if [ ${#WHATIWANT[4i]} != 0 ] ; then
			DoWhatEver ${WHATIWANT[$i]}
		else
			echo "Line $i is blank"
			exit 2
		fi
	done
else 
	echo "Content not found in $FILE" 
	exit 3
fi
exit 0

Thank you
Russell




On Wed, 18 Sep 2002 15:06:30 -0700 (PDT), Matt Alexander said:

> Oooh!!  awk...  I love awk.  awk it is.
>  
>  
>  
>  On Wed, 18 Sep 2002, Don Buchholz wrote:
>  
>  > here are 2 ways to do it ...
>  >
>  > --------------------------------
>  > #!/bin/bash
>  > EMAIL=`head -1 $file`
>  > SUBJECT=`head -2 $file | tail -1`
>  > --------------------------------
>  > #!/bin/bash
>  > EMAIL=`awk 'NR==1 {print;}`
>  > SUBJECT=`awk 'NR==2 {print;}`
>  > --------------------------------





More information about the PLUG mailing list