本节分享一个在文件中删除多余行的shell/ target=_blank class=infotextkey>shell脚本。
注意,本脚本采用ksh编写,如果是运行在其它shell环境中的话,可能需要作细微的修改。
代码:
#!/usr/bin/ksh
#filename:delete_blank.sh
#desc: 删除空行
#edit:www.jb200.com
file=$1
# Check the file name is passed as argument or not, if not take the file name
if [ -n "${file}" ]
then
echo "File Name -> " $file
else
echo " [40m [1;37m "
echo "Please Enter the Filename ->"
read line
file=$line
echo "File Name -> " $file
fi
# Confirm the existence of the file.
if [ -n "${file}" ]
then
ls $file > /dev/null 2>&1
if [ $? -eq 0 ]
then
linuxjishu/13830.html target=_blank class=infotextkey>awk 1 $file | sed -e '/^...$/d' -e '/^$/d' -e '/^[ ]/d' > temp1.txt
# Move file to original filename
cp temp1.txt $file
# Remove the Temporary file
rm temp1.txt
else
echo "File " $file " not found"
fi
fi
执行脚本: