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. # ------------------------------------------------------------------------- # Copyright (c) 2008 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 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
4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!
Click here to subscribe via email.
Click here to subscribe via email.
- Download Script
- Email this to a friend
- Rss Feed
- Last Updated: 09/17/08






