postheadericon unix grep command


Definition : Write a script that deletes all leading and trailing spaces in all lines in a file. Also remove blank lines from a file. Locate lines containing only printf but not fprintf

File Name : ex27.sh


clear
echo "Enter file name : "
read fileName
if [ -f $fileName ]
then
echo "____________________________________"
        echo " Trim File .... "
        echo "____________________________________"

        # Delete leading & trailing whitespace from each line and store it tempFile.txt
        sed 's/^[ \t]*//;s/[ \t]*$//;' $fileName >tempFile.txt

        # Remove all blank lines from tempFile.txt
        sed '/^$/d' tempFile.txt

        # Replace content of this two file
        mv tempFile.txt $fileName
else
        echo "File Not Found"
fi

echo "____________________________________"
echo "  Located Lines .... "
echo "____________________________________"

# Locate lines containing only printf but not fprintf
grep -e "printf" $fileName | grep -v "fprint"

postheadericon Write a script to learn command line argument


Definition : Write a script that behaves both in interactive and non-interactive mode. When no arguments are supplied, it picks up each C program from current directory and lists the first 10 lines. It then prompts for deletion of the file. If the user supplies arguments with the script, then it works on those files only.

File Name : ex26.sh


flag=1
if [ $# -ne 0 ]
then
            # if user provide arguments
             echo $* >temp
            flag=1
else
             # if user not provide arguments
            ans=`ls *.c`
            if [ "" == "$ans" ]
            then
                        echo "There are no c file"
                        flag=0
            else
                        ls *.c>temp
                        flag=1
            fi
fi
if [ $flag -eq 1 ]
then
            for filename in `cat temp`
            do
                        if [ -f $filename ]
                        then
                                    echo "_______________ $filename _______________"
                                    sed -n -e "1,10 p" $filename
                                    rm -i $filename
                        fi
            done
rm temp
fi


postheadericon Write a script to display the last modified file


File Name : ex24.sh


clear
echo "Enter Directory Name :\c"
read dir1

cd $dir1

ls -lt | head -2 | tail -1 |cut -d " " -f 10


postheadericon Write a script to display all lines of a file in ascending order


File Name : ex23.sh


clear
echo "Enter File Name :\c"
read filename
sort $filename


postheadericon Write a script to display all words of a file in ascending order.


File Name : ex22.sh


echo "Enter File name:\c"
read fn
 for i in `cat $fn`
do
echo $i >> f2.txt
done
sort f2.txt
rm f2.txt


Total Pageviews

© BipinRupadiya.com. Powered by Blogger.