Saturday, 23 May 2020

Shell Script to Delete All Even Numbered Lines from a Text File



Program
echo "enter a file name"
read fname
lc=0
if test -f $fname
then
   while read line
   do
      lc=$((lc+1))
      even=$((lc%2))
      if test $even -ne 0
      then
         echo $line>>newfile.txt
      fi
   done < $fname
   rm $fname
   mv newfile.txt $fname
else
   echo "$fname file not exist"
fi


OUTPUT
csc@csc-SVE1513CYNB ~ $ cp avg ttavg
csc@csc-SVE1513CYNB ~ $ bash avg
enter a file name
ttavg


echo "enter a file name"
lc=0
then
do
even=$((lc%2))
then
fi
rm $fname
cat $fname
echo "$fname file not exist"

No comments:

Post a Comment