SHELLdorado Newsletter 1/2000 - February 13, 2000

The "SHELLdorado Newsletter" covers UNIX shell script related
topics. To subscribe to this newsletter, leave your e-mail
address at the SHELLdorado homepage:

"Heiner's SHELLdorado" is a place for UNIX shell script
programmers providing

     Many shell script examples,
     shell scripting tips & tricks + more...

 o  What's new at the SHELLdorado?
 o  Shell Tip: Useful print-text-and-read-line feature (ksh)
 o  Shell Tip: Include current date in file name
 o  Q&A: How to use "sed" to insert/replace strings

>> What's new at the SHELLdorado?

 o  The SHELLdorado web pages have been redesigned for

     -	Simpler navigation - the SHELLdorado main sections are
	always just one mouse click away

     -	More consistency - all pages now have the same "look
	and feel"

     -	Greater accuracy - periodical link-checking, and
	automatic tagging of changes (i.e. new shell scripts)
	will result in more accurate and up-to-date information

 o  New shell scripts were added to the "Scripts" section

>> Shell Tip: Useful print-text-and-read-line feature (ksh)

    A common task in a shell script is "print text, read
    line", i.e.

    	echo "Please enter your name: "
	read name

    The Korn shell (KSH) has a standard (but little used)
    feature to simplify this:



    	read name?'Please enter your name: '

    This KSH idiom has another advantage to "echo": The cursor
    is placed directly after the prompt string (not in the
    next line).

>> Shell Tip: Include current date in file name

    Sometimes it's useful to include the current date in the file
    name, i.e. to keep different versions of daily changing files
    (log files, ...).

    In these cases the "date" command may be used to provide
    a date in a convenient format, i.e.

    	$ date "+%Y-%m-%d"

    writes a day in ISO format year-month-day, i.e. "2000-02-13"
    (enter "man date" or "man 3 strftime" to get a list of all
    supported "%X" format specifiers).

    To rename the file "logins.log" to "logins.2000-02-13",
    (where "2000-02-13" will be the current date), we just have
    to write a script with the following content:

    	mv logins.log logins.`date +%Y-%m-%d`

>> Shell Tip: News headlines from the web

    Many sites provide up-to-date news headlilnes, some of them
    are updated many times a day. Wouldn't it be nice to just
    get the headlines, without starting up a browser?

    The following script does just this: it gets the current
    headlines from C|Net ( directly via the Web,
    and prints them to the screen:

	wget -q -O - |
		grep -i 'font.*size.*+1.*/news/.*html' |
		striphtml |
		grep -v '[ap]\.m\.'

    Example output:

    	SEC chaiman urges investor caution
	U.S. investors, lulled into a false sense of security [...]

	Is Silicon Valley losing its appeal?
	For the first time in five years [...]

	HotBot's time warp
	For some, the dates appearing on Lycos site's [...]

    With the help of some programs (like "wget" and "striphtml")
    it's easy to get the most current information from the web.
    A larger example on how to get current news headlines is
    available at the "SHELLdorado":

    The programs "wget" and "striphtml" are available here:

>> Q&A: How to use "sed" to insert/replace strings

    "Sed" ("stream editor") is a powerful tool to edit data

    The following examples show some common shell scripting
    problems, and their solutions using "sed".

    The first examples show, how to use "sed" to change text
    within a line using the "s" ("substitute") command of "sed:

     1. Insert text at the start of a line:
	    sed 's/^/NEW TEXT/'

	Example: prepend an "> " to each of the following lines
	(i.e. to "quote" an email):

	    this is a test.

	The following command:

	    sed 's/^/> /'

	produces this result:

	    > Hi,
	    > this is a test.

     2. Append text at the end of a line:
	    sed 's/$/NEW TEXT/'

	Example: append an exclamation mark ("!") at the end
	of the following lines:

	    this is a test

	The following command:

	    sed 's/$/!/'

	produces the result:

	    this is a test!

     3. Change text somewhere in the line:
	    sed 's/PATTERN/NEW TEXT/g'

	Example: in the following lines, replace each occurrence
	of the word "cool" with "extraordinary":

	    SED is cool!

	The command

	    sed 's/cool/extraordinary/g'

	gives the result:

	    SED is extraordinary!

     4. Insert/append text within a line

	To insert/append to an existing text (instead of changing
	it), the "&" character may be used to represent the
	matched string in the replacement text:

	Example: prepend the words "at least" to each single
	digit number (i.e. "1", "2", ...) in the following lines:

	    this text contains 3 examples
	    this line has 4 words

	The command

	    sed 's/[0-9]/at least &/g'

	applied to the lines above results in:

	    this text contains at least 3 examples
	    this line has at least 4 words

	(The "&" within the replacement text will be substituted
	with the matched pattern)

    The examples above only change existing lines. If a complete
    new line should be added before/after a line matching a
    pattern, other "sed" commands can be used:

    5. Insert a new line before a line matching "PATTERN":
	sed '/PATTERN/i\

    6. Append a new line after a line matching "PATTERN":

	sed '/PATTERN/a\

If you want to comment on the newsletter, or even want to submit
an article of your own, send a mail to

To unsubscribe send a mail with the body "unsubscribe" to