MS Word: replace spaces to non-breaking

To insert it manually, use Ctrl+Shift+Space


Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Normal")
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = "^s"
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

WHICH command for DOS

If You need the analog of Unix ‘which’ command and do not have Resource Kit, the following batch file will help.
The code was found on www.ss64.com
Published with the permission of the author, Clay Calvert.


@echo off
SETLOCAL
(set WF=)

:: Look for file in the current directory

for %%a in ("" %PATHEXT:;= %) do (
if not defined WF if exist "%~1%%~a" set WF=%CD%\%~1%%~a)

:: Look for file in the PATH

for %%a in ("" %PATHEXT:;= %) do (
if not defined WF for %%g in ("%~1%%~a") do (
if exist "%%~$PATH:g" set WF=%%~$PATH:g))

:: Store the Help Error message

if NOT "%~x1"=="" goto:END
for /f "delims=." %%a in ('help.exe *') do set HlpErr=%%a

:: If command missing from Help, goto end

help.exe "%~1" |find "%HlpErr%">nul && goto:END

:: Listed in help so find file in System32

for %%a in (exe com) do (
if /i "%WF%"=="%windir%\system32\%~1.%%a" goto:END)

:: File not in System32, so must be an internal command

set WF="%~1" is an internal command

:END
if defined WF (echo.%WF%) else (
echo The file: "%~1" was not found)

:: Credits:
:: Clay Calvert
:: alt.msdos.batch.nt

JavaScript: build table dynamically


<script type="text/javascript">
function buildTable(n_rows,n_columns) {

var the_body = document.getElementsByTagName("body")[0];

var new_table = document.createElement("table");
var new_tbody = document.createElement("tbody");

var new_row, new_col, new_text ;

for(row=1;row<=n_rows;row++) {
new_row = document.createElement("tr");
new_row.className = "tr"+row ;
for(col=1;col<=n_columns;col++) {
new_col = document.createElement("td");
new_col.className = "td"+col ;
new_text = document.createTextNode("text:" + row + ":" + col );

new_col.appendChild(new_text);
new_row.appendChild(new_col);
}
new_tbody.appendChild(new_row);
}

new_table.appendChild(new_tbody);
new_table.setAttribute("border", "2");

the_body.appendChild(new_table);

return new_table ;

}
</script>
<body>
Example:
<script type="text/javascript">
buildTable(4,3)
</script>
</body>

There is also interesting article on oreillynet.com, discussing performance improvement for such operations.

Calculating the date of Easter

The easiest way is to use specialized package (like Date::Calc).
If You need standalone function, the following method could be used.


#!/usr/bin/perl

$year=$ARGV[ 0 ];
$isJulian=(defined($ARGV[ 1 ]) && $ARGV[ 1 ] eq 'julian');
$isOrthodox=(defined($ARGV[ 1 ]) && $ARGV[ 1 ] eq 'orthodox');

my ( $G , $C , $H, $I, $J , $L );

$G = $year % 19 ;
if ( $isJulian || $isOrthodox ) {
print ( $isJulian ? "Julian:" : "Orthodox:" );
$I = ( 19*$G + 15) % 30 ;
$J = ( $year + int($year/4) + $I) % 7 ;
}else{
print "Gregorian:" ;
$C = int( $year/100 );
$H = ($C - int($C/4) - int((8*$C+13)/25) + 19*$G + 15) % 30 ;
$I = $H - int($H/28)*(1 - int($H/28)*int(29/($H + 1))*int((21 - $G)/11)) ;
$J = ($year + int($year/4) + $I + 2 - $C + int($C/4) ) % 7 ;
}
$L = $I - $J ;
$EasterMonth = 3 + int(($L + 40)/44) ;
$EasterDay = $L + 28 - 31*int($EasterMonth/4) ;

if ( $isOrthodox ) {
( $EasterDay, $EasterMonth, $year ) = EasterOrthodox( $EasterDay, $EasterMonth, $year )
}

printf("Day=%d Mon=%d Year=%d \n", $EasterDay, $EasterMonth, $year );

sub EasterOrthodox
{
my ($pDay, $pMonth, $pYear) = @_ ;
my ( $extra , $tmp ) = ( 0, 0 );
my ( $rezDay, $rezMonth) = ( 0, 0 );

if (($pYear > 1582) && ($pYear <= 4099)) { $extra = 10; if ($pYear > 1600) {
$tmp = int($pYear/100) - 16;
$extra = $extra + $tmp - int($tmp/4);
}

$rezDay = $pDay + $extra;
$rezMonth = $pMonth;

if (($rezMonth == 3) && ($rezDay > 31)) {
$rezMonth = 4;
$rezDay = $rezDay - 31;
}

if (($rezMonth == 4) && ($rezDay > 30)) {
$rezMonth = 5;
$rezDay = $rezDay - 30;
}
}
return ( $rezDay, $rezMonth, $pYear )
}

Examples:
perl -w 2006
perl -w 2006 orthodox

Using pattern lists in Unix

Here is the small reminder about the syntax of the “case” command and the usage of the pattern lists.


#!/bin/ksh
print -n "Please enter the line: "
read line

case "$line" in
?(dog|cat) ) print "zero or one occurrence of any pattern" ;;
*(low|high) ) print "zero or more occurrences of any pattern" ;;
@(duncan|methos) ) print "exactly one occurrence of any pattern" ;;
+(rudolph|blitzen) ) print "one or more occurrence of any pattern" ;;
!(grinch|babay) ) print "everything except patterns" ;;
-@([hH?]) ) print "Some help..." ;;
-v*(erbose) ) print "Some more words..." ;;
*) print "Something else..." ;;
esac

Print the PATH directories in the readable format


echo $PATH| awk -v RS=":" '{ print $0 }'

echo $LD_LIBRARY_PATH |awk -v RS=":" '{ system ( "ls -rltd " $0 ) }'

Warning!
As far as the option ‘-v’ is used, the new awk(nawk in some systems) should be used.

To check if the new version of awk is installed:

awk 1 /dev/null

The output will be empty for new awk.
You have the old awk, if the output is something like

awk: syntax error near line 1

In this case nawk should be used instead awk

MS-DOS modification: echo %PATH% | awk -v RS=”;” “{ print $0 }”

Parsing script parameters

Quick and dirty parsing procedure for unix shell scripts


parse_command_line ()
{
typeset -i user_opt help_opt password_opt verbose_opt
typeset errmsg

arg_cou=$#

while [ "$#" -gt 0 ]
do
case "$1" in
-@([U]) ) let user_opt=user_opt+1 ;;
-P ) let password_opt=password_opt+1 ; ask_pass=0 ;;
-option1 ) ;;
-option2 ) ;;
-option3 ) ;;
-@([hH?])) help_opt=1;;
-v*(erbose)) let verbose_opt=verbose_opt+1 ;;
*)
if [ user_opt -eq 1 ];then
USER="$1"; user_opt=0
elif [ password_opt -eq 1 ];then
PASS="$1"; password_opt=0
elif [ verbose_opt -eq 1 ];then
verbose_level="$1"; verbose_opt=0
else
### help_opt=1
CMD=$* ; break
fi
;;
esac
shift
done

if [ arg_cou -eq 0 ] ; then
help_opt=1 ; errmsg="Nothing to do"
fi

for opt in user password ; do
if [ ${opt}_opt -ne 0 ] ; then
help_opt=1 ; errmsg="${errmsg}\n${opt} is absent"
fi
done

if [ verbose_opt -ne 0 ] ; then
verbose_level=$verbose_opt
fi

if [ help_opt -eq 1 ]; then
help "$errmsg"
fi
}