Shell Script To Count English Language Articles Such As 'A', 'An' and 'The'

in Categories Decision Making, File-management last updated April 21, 2008

This script also covers following techniques:
=> Reading input text file line by line
=> Reading and processing words in input
=> Convert word to lowercase

#!/bin/bash
# Write a shell script that counts English language articles (a, an, the) 
# in a given text file.
#
# --------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2005 nixCraft project.
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
echo -n "Enter a file name : "
read file
a=0
the=0
an=0
 
# make sure file exist
if  [ ! -f $file ]
then
	echo "$file not a file!"
	exit 1
fi
 
# put while loop to read a $file
while read line
do
       #process each word 
	for w in $line 
	do
		# convert word to lowercase; so that we can count ThE, THE, the, THe etc all
		lword="$(echo $w | tr '[A-Z]' '[a-z]')"
 
		# is it 'a' article?
		[ $lword = "a" ] && (( a++ )) || :
		[ $lword = "the" ] && (( the++ )) || :
		[ $lword = "an" ] && (( an++ )) || :
	done
done < $file
 
# display stats
echo "a article occured $a times"
echo "the article occured $the times"
echo "an article occured $an times"

Shell Script to send HTTP Code 301 Moved Permanently Followed by Actual URL Redirection

in Categories File-management last updated April 10, 2008

This script depends upon shell array.

#!/bin/bash
# Shell script to update old php files to point out to new CMS url
# it placed http code 301.
# You must create an array in following format:
# path/to/old/url1.php  path/to/new/url/slug/
# --------------------------------------------------------------------------
# Copyright (C) 2008 nixCraft project <http://www.cyberciti.biz/tips/contact-us>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
declare -a t=("http://domain.com/1.php" "http://domain.com/new/path/slug/"
"http://domain.com/2.php" "http://domain.com/new/path/slug/"
"http://domain.com/3.php" "http://domain.com/new/path/slug/"
"http://domain.com/4.php" "http://domain.com/new/path/slug/"
"http://domain.com/5.php" "http://domain.com/new/path/slug/")
# set array total array size -1 ; if you have 5 item it should set to 4
for i in `seq 0 4` 
do
	#only work on zero or even element  
	if [ $i -eq 0 -o  $( expr $i % 2 ) -eq 0  ]; then
		s="${t[$i]}"
                d="${t[` expr $i + 1 ` ]}"
                file=$(basename $s)
 		echo "Patching $file...."
		if [ -f $file ]; then
		# if file exists, create new php redirection code
			echo '<?php'>$file
			echo 'header("HTTP/1.1 301 Moved Permanently");'>>$file
			echo 'header("Location: ' $d '");'>>$file
			echo '?>'>>$file
		fi
	fi
done

Bash shell script to reverse text file contain using Shell array

in Categories File-management last updated July 24, 2008

This script also demonstrate how to use arrays under bash shell script.

#!/bin/bash
# Bash shell script to reverse text file contain i.e. concatenate files and
# print on the standard output in reverse. This script also demonstrate how 
# to use arrays under bash shell script.
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
FILE="$1"
if [ $# -eq 0 ]; then
  echo "$(basename $0) - file-name"
  exit 1
fi
 
textArray[0]="" # hold text
c=0 # counter 
# read whole file in loop
while read line
do
  textArray[$c]=$line # store line
  c=$(expr $c + 1) # increase counter by 1
done < $FILE
# get length of array 
len=$(expr $c - 1 )
 
# use for loop to reverse the array
for (( i=$len; i>=0; i-- ));
do
  echo ${textArray[$i]}
done