How to eliminate entire lines with sed

Kris Stephens [Hail Eris!] krs at uts.amdahl.com
Sat Jan 26 04:51:36 AEST 1991


In article <1991Jan25.070712.17725 at bradley.bradley.edu> data at buhub (Mark Hall) writes:
>I'm having a minor problem.  I want to use a shell script that I have in my
>.profile to do some things dealing with mail.  I use checkmail and a custom
>script that keeps track of the number of new messages.  When I log in, it
>tells me how many NEW messages I have by subtracting the number of lines
>returned from the Checkmail /usr/mail/data | wc -l by the number in the
>.mailchk file I create (it only contains a single number).
>When I display the messages in my mailbox, I'd like to remove the first "x"
>lines from the listing.  Any suggestions?
>
>SUmmary:  WHat I want to do is pass the number in the .mailchk file and delete
>that number of lines from the beginning of my input by using sed.
>
>Mark hall

If your .mailchk file contains *only* the integer through which you
want to delete, ...

	:
	# This is the ksh version
	KSH=/bin/ksh
	CMD=/usr2/myid/bin/chkmail
	
	#
	# Make sure this is ksh
	#
	if [ "$RANDOM" = "$RANDOM" ]
	then
		exec $KSH $CMD $*
		exit 1	# exec failed!
	fi

	# Set important vars 'cause I'm too lazy to keep typing them
	CNTL=~/.mailchk
	MB=/usr/mail/$(logname)

	# Make the variable 'i' a decimal integer
	typeset -i10 i

	# If the control-file is empty, then we've seen nothing
	if [ ! -f $CNTL ]
	then
		i=0
	else
		i=$(<$CNTL)
	fi

	# If we've seen nothing, list the whole MB
	if [ $i -eq 0 ]
	then
		cat $MB
	else
		sed "1,${i}d" $MB
	fi
	wc -l < $MB > $CNTL
	exit

To convert to sh, you'l need to replace some of the short-and-sweet
steps with expr and cat calls and the    $(cmd)   stuff with `cmd`,
but the call to sed 3 lines above the exit is what you're looking for.

Also, the output of wc -l, on my system at least, provides a leading
space before the number.  By    typeset -i10 i    I force that blank
to be removed from the variable when     i=$(<$CNTL)   is done.  In sh,
where typeset doesn't exist, one needs to take care of the space either
on the write to $CNTL or the read from it.

...Kris
-- 
Kristopher Stephens, | (408-746-6047) | krs at uts.amdahl.com | KC6DFS
Amdahl Corporation   |                |                    |
     [The opinions expressed above are mine, solely, and do not    ]
     [necessarily reflect the opinions or policies of Amdahl Corp. ]



More information about the Comp.unix.shell mailing list