| Script Examples |
|---|
| Some Simple Shell Script Examples |
|---|
| Cut a block of lines |
|---|
| BatchFtp |
|---|
| Compute Average, Maximum and Minimum |
|---|
| Spelling Related Tools |
|---|
| Scripts for HTML |
|---|
| FAQ |
|---|
| FAQ2 |
|---|
| Some Simple Shell Script Examples |
|---|
gn [options] pattern files |
|---|
grep -n $* |
|---|
tail /usr/spool/mail/uname |
|---|
tail $1 /usr/spool/mail/uname |
|---|
sed G filename |
|---|
wc -l $1 | cut -c1-7 |
|---|
count=1
while [ $count -le $1 ]
do
echo $count
count=`expr $count + 1`
done
|
|---|
head -$1 longfile|cat -n|cut -c1-5 |
|---|
head -$1 longfile|awk "{print NR}"
|
|---|
head -$1 longfile|perl -p 'print $.' |
|---|
find $HOME -name $1 -print |
|---|
find . -name $1 -print |
|---|
pr -t -l66 -w80 -2 $1 |
|---|
ex $1 <<% ___________ 1,\$s/^M\$// |1,$s/^M// | \$s/^^Z\$// |$/^Z// | w |w | % ----------- |
|---|
ls | sed 's/..*/mv & &/' > script vi script sh script |
|---|
| Cut a block of lines |
|---|
gl beginline endline file |
|---|
sed -n -e "$1,$2p" $3 |
|---|
QuitPoint=`expr $2 + 1`
sed -n -e "$1,$2p
${QuitPoint}q" $3
|
|---|
cp $3 tmp.o echo "$1,$2d w" > script ex $3 < script |
|---|
mv $3 tmp.o a=`expr $1 - 1` b=`expr $2 + 1` head -$a tmp.o > $3 cat $4 >> $3 sed -n -e "$b,\$p" tmp.o >> $3 !!!!! What if $1 is 1 or $2 too long? |
|---|
| BatchFtp |
|---|
ftp $1 <<% lien binary put $2 (OR get $2) quit % |
|---|
echo "ftp $1 <<%
lien
binary" > script
for i
do
echo "put $i"
done >> script
echo "quit
%" >> script
sh script
|
|---|
| Compute Average, Maximum and Minimum |
|---|
avg column file |
|---|
awk "
{sum += \$$1;}
END {print sum/NR} " $2
|
|---|
awk "
BEGIN { MAX = -999999}
{
if ( \$$1 > MAX)
MAX = \$$1
}
END {print MAX} " $2
|
|---|
| Spelling Related Tools |
|---|
egrep "^$1" /usr/dict/all |
|---|
mgrep pat1 pat2 pat3 ... |
|---|
case $# in
0) cat ;;
1) cat | grep $1 ;;
2) cat | grep $1 | grep $2;;
3) cat | grep $1 | grep $2 | grep $3;;
|
|---|
sphelp con v en t |
|---|
pat=$1;
shift
grep "^$pat" /usr/lib/dict/all | mgrep $*
|
|---|
pat=$1;
shift
look "$pat" | mgrep $*
|
|---|
spell $1 > 1.o comm -23 1.o excluedwords > 2.o sed 's/..*/1,$s\/&\/&\/g' 2.o > script vi script ex $1 < script |
|---|

1,$s/th/the/g w |
|---|
1,$s/systen/system/g 1,$s/systens/systems/g w |
|---|
(while read w do echo "1,\$s/$w/$w/g" done ) < 2.o > script |
|---|
| Scripts for HTML |
|---|
echo ""
for R in 00 33 66 99 cc ff
do
echo "<TABLE>"
for G in 00 33 66 99 cc ff
do
echo "<TR>"
for B in 00 33 66 99 cc ff
do
echo "<TH bgcolor=#$R$G$B>
<font size=3 color=BLACK>$R $G $B<br>"
<font size=4 color=#$R$G$B>$R $G $B</TH>"
done
echo "</TR>"
done
echo "</TABLE>"
done
|
|---|
| FAQ |
|---|
split -b 1m filename |
|---|
for i in *.foo
do
mv $i `basename $i .foo`.bar
done
|
|---|
for i in foo.*
do
tailname=`echo $i | sed -e 's/^foo\.//'`
mv $i bar.$tailname
done
|
|---|
awk '/start-here/,/end-here/' data.rec |
|---|
awk '{if ( $0=="start-here") \
n=1 \
else { if($0=="end-here") \
n=0 \
if (n==1) \
print}}' data.rec
|
|---|
find / -size 0 \(-name \*.dat -o -name sh\*.dat \) -exec rm -f {} \;
|
|---|
#!/bin/sh for i in 000??.txt.all do sort -r $i > `echo $i | sed 's/\.all$/.sort/'` done |
|---|
nohup command [ arguments ] & |
|---|
echo $SHELL |
|---|
grep -v "string" filename > new_filename |
|---|
sed -e 's/^/\./' |
|---|
| FAQ2 |
|---|
for file in *
do
lcfile=`echo $file | tr "[A-Z]" "[a-z]"`
mv $file $lcfile
done
|
|---|
9600012301FDTF02FTDT03FFTF04TFDD05TDFF06TTTT* |
|---|
96000123 0p1FDTF 0p2FTDT 0p3FFTF 0p4TFDD 0p5TDFF 0p6TTTT |
|---|
sed 's/\([0-9][0-9][A-Z][A-Z][A-Z][A-Z]\)/\ \1/g' |
|---|
egrep -v '(^$)' test.txt |
|---|
:%s/^\([^ ]*\) \([^ ]*\)/\2 \1/ |
|---|
awk '{print $2 $1}'
|
|---|
BEGIN { FS = "," }
$3 ~ /046\*/ && $6 == "TOWER GROUP" {
while( (getline < FILENAME) > 0)
print
close(FILENAME)
}
|
|---|
#!/bin/sh num=date" +%d" echo "\n $num" if expr "$num" \> 10 then echo "larger" else echo "smaller" fi |
|---|
I want to filter out: is,this,a Input: this is a dumb question Desired Output: dumb question |
|---|
perl -p -e 's/(^|\s+)is(\s+|$)//g;s/(^|\s+)this(\s+|$)//g;' |
|---|
perl -p -e 's/(^|\s+)(is|this)(\s+|$)//g;' |
|---|
find / -amin +120 ... |
|---|
find . -newer thefile -print |
|---|
find . -mmin +120 -type f -exec rm -r {} \;
|
|---|