Hi,
Firstly I would like to acknowledge ScriptOmatic for all his input.
Is there a way to extend the following CSH script to handle optional
arguments
in combination with grep. -Need to match a argument as either a prefix or
suffix to input from stdin.
Similar to stsrstr and strncmp in C.
Could anyone please help with how to handle the options and Grep.
I would just do this in C: if (strncmp(abcdef, abc, 3)==0){...similar for
strstr for suffix.(using strlen of both strings) I have no idea how to
handle the args or how to
assign a "grepped" value to a variable then do the string comparisons.
for example: (findme is name of script)
to look for a suffix of "son" from a line of input
$ findme -s son
blah nil nothing --input not echoed back as valid suffix not found
fred johnson
fred johnson --line echoed back as suffix "son" found
also would like to handle prefix option:
$ findme -p mart
blah nil nothing --input not echoed back as valid prefix not found
sally martin
sally martin --line echoed back as prefix "mart" found
regards
Ian
#!/bin/csh -f
set NAME=`basename ${0}`;
set TAB=' ';
set USAGE="\
usage: ${NAME} word\
\
word find all lines on standard input that match word\
\
"
# if no args are given, echo usage and exit
if ( ${#argv} != 1 ) then
echo ${USAGE:q};
exit 0;
else
set word="${1:q}";
endif
while ( 1 )
set noglob
set line = ( $< )
unset noglob
#echo DEBUG: line=${line}
set p = 0;
# we got the line and the word, find it
foreach i ( ${line} )
echo DEBUG: i: \"${i:q}\"
# $i must contain word, I think
if ( ${i:q} =~ "${word}" ) then
set p = 1;
break;
endif
end
if ($p == 1) then
echo ${line:q}
endif
end
bottomout: # goto point when hit with interrupt
exit 0;