Professional Documents
Culture Documents
Write a shell script that accepts a file name, starting and ending line numbers as arguments and
displays all the lines between the given line numbers.
#!/bin/bash
echo " Enter the file name"
read fname
echo “enter starting line number”
read sl
echo “enter ending line number”
read el
if [ -f $fname ]
then
echo "the lines between $sl and $el of given file are"
sed -n $sl,$el\p $fname|sed -e '1d;$d'
else
echo "file doesnt exist"
fi
Write a shell script that deletes all lines containing a specified word in one or more files supplied as
arguments to it.
#!/bin/bash
if [ $# -ne 0 ]
then
echo enter the word
read word
for fname in $*
do
if [ -f $fname ]
then
echo the given input filename is:$fname
grep -v "$word" $fname
else
echo its not a file
fi
done
else
echo "enter atleast one argument as input"
fi
Write a shell script that displays a list of all the files in the current directory to which the user has
read, write and execute permissions.
#!/bin/bash
echo "List of Files which have Read, Write and Execute Permissions in Current Directory"
for file in *
do
if [ -r$fname-a-w$fname-a-x$fname ]
then
echo $file
fi
done
Write a shell script that receives any number of file names as arguments checks if every argument
supplied is a file or a directory and reports accordingly. Whenever the argument is a file, the number
of lines on it is also reported.
#!/bin/bash
for fname in $*
do
if test -f $fname
then
echo $fname "is a ordinary file"
echo "number of lines:"
cat $fname|wc -l
Write a shell script that accepts a list of file names as its arguments, counts and reports
theoccurrence of each word that is present in the first argument file on other argument files.
#!/bin/bash
if [ $# -ne 2 ]
then
echo "Error : Invalid number of arguments."
exit
fi
str=`cat $1 | tr '\n' ' '`
for a in $str
do
echo "Word = $a, Count = `grep -c "$a" $2`"
done
Write an awk script to count the number of lines in a file that do not contain vowels.
BEGIN{
total=0;}
{if($0!~/[aeiouAEIOU]/)
total=total + 1}
END{print "The total lines in a file that do not contain vowels:",total}
Write an awk script to find the number of characters, words and lines in a file.