Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

7.

Shell Script to Delete All Even Numbered Line from a Text File
#!/bin/bash
# Write a shell script which deletes all even numbered line from a text file.
# ------------------------------------------------------------------------file=$1
counter=0
out="oddfile.$$" # odd file name
if [ $# -eq 0 ]
then
echo "$(basename $0) file"
exit 1
fi
if [ ! -f $file ]
then
echo "$file not a file!"
exit 2
fi
while read line
do
# find out odd or even line number
isEvenNo=$( expr $counter % 2 )
if [ $isEvenNo -eq 0 ]
then
# odd match; copy all odd lines $out file
echo $line >> $out
fi
# increase counter by 1
(( counter ++ ))
done < $file
# remove input file
/bin/rm -f $file
# rename temp out file
/bin/mv $out $file
http://bash.cyberciti.biz/academic/display-logged-in-users-your-usernameand-date-time/

5. Write a shell script that accepts a name from the user and
displays whether it is a file, directory or something else.
echo " enter file"
read str
if test -f $str
then echo "file exists n it is an ordinary file"
elif test -d $str
then echo "directory file"
else
echo "not exists"
fi
if test -c $str
then echo "character device files"
fi

Shell Script To Count Vowels, Blank Spaces, Characters, Number of


line and Symbols
#!/bin/bash
# Shell program to count
# Vowels, blank spaces, characters, number of line and symbols
# ------------------------------------------------------------------------file=$1
v=0
if [ $# -ne 1 ]
then
echo "$0 fileName"
exit 1
fi
if [ ! -f $file ]
then
echo "$file not a file"

exit 2
fi
# read vowels
exec 3<&0
while read -n 1 c
do
l="$(echo $c | tr '[A-Z]' '[a-z]')"
[ "$l" == "a" -o "$l" == "e" -o "$l" == "i" -o "$l" == "o" -o "$l" == "u" ]
&& (( v++ )) || :
done < $file
echo
echo
echo
echo

"Vowels : $v"
"Characters : $(cat $file | wc -c)"
"Blank lines : $(grep -c '^$' $file)"
"Lines : $(cat $file|wc -l )"

You might also like