shell下十二种读文件的方法

来源:互联网 发布:xp连接网络打印机设置 编辑:程序博客网 时间:2024/05/20 06:27

作者:Webmaster   来源:Linux-cn.com   点击:   日期:2007-05-09 [收藏] [投稿]

  IE是否经常中毒?推荐您 <script type="text/javascript"><!--google_ad_client = "pub-5499099499828942";google_ad_output = "textlink";google_ad_format = "ref_text";google_cpa_choice = "CAAQscq1_wEaCH4xELoJYEcfKOm84YcB";google_ad_channel = "";//--></script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script> <script language="JavaScript1.1" src="http://pagead2.googlesyndication.com/cpa/ads?client=ca-pub-5499099499828942&amp;cpa_choice=CAAQscq1_wEaCH4xELoJYEcfKOm84YcB&amp;oe=gb2312&amp;dt=1185608432437&amp;lmt=1178707059&amp;prev_fmts=468x60_as_rimg&amp;format=ref_text&amp;output=textlink&amp;correlator=1185608431843&amp;url=http%3A%2F%2Fwww.linuxdby.com%2Fhtml%2Flinux%2Fother%2F20070509%2F39188.html&amp;region=_google_cpa_region_&amp;ref=http%3A%2F%2Fwww.baidu.com%2Fs%3Fwd%3Dshell%2B%25B6%25C1%25CE%25C4%25BC%25FE%26lm%3D0%26si%3D%26rn%3D10%26ie%3Dgb2312%26ct%3D0%26cl%3D3%26f%3D1%26rsp%3D1&amp;cc=319&amp;flash=9&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_java=true" type="text/javascript"></script> 下载带有 Google 工具栏的 Firefox, 上网冲浪更惬意
<script type="text/javascript"><!--google_ad_client = "pub-5499099499828942";google_ad_width = 468;google_ad_height = 60;google_ad_format = "468x60_as";google_ad_type = "text_image";//2007-04-29: 查看内容页面内容上面google_ad_channel = "4763637743";google_color_border = "FFFFFF";google_color_bg = "FFFFFF";google_color_link = "000000";google_color_text = "000000";google_color_url = "000000";//--></script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script> <iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5499099499828942&amp;dt=1185608433015&amp;lmt=1178707059&amp;prev_fmts=120x90_0ads_al_s&amp;format=468x60_as&amp;output=html&amp;correlator=1185608431843&amp;channel=4763637743&amp;url=http%3A%2F%2Fwww.linuxdby.com%2Fhtml%2Flinux%2Fother%2F20070509%2F39188.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=000000&amp;color_url=000000&amp;color_border=FFFFFF&amp;ad_type=text_image&amp;ref=http%3A%2F%2Fwww.baidu.com%2Fs%3Fwd%3Dshell%2B%25B6%25C1%25CE%25C4%25BC%25FE%26lm%3D0%26si%3D%26rn%3D10%26ie%3Dgb2312%26ct%3D0%26cl%3D3%26f%3D1%26rsp%3D1&amp;cc=319&amp;flash=9&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_java=true" frameborder="0" width="468" scrolling="no" height="60" allowtransparency="allowtransparency"></iframe>
以KSH为例:


#!/usr/bin/ksh
#
# SCRIPT: 12_ways_to_parse.ksh.ksh
#
#
# REV: 1.2.A
#
# PURPOSE: This script shows the different ways of reading
# a file line by line. Again there is not just one way
# to read a file line by line and some are faster than
# others and some are more intuitive than others.
#
# REV LIST:
#
# 03/15/2002 - Randy Michael
# Set each of the while loops up as functions and the timing
# of each function to see which one is the fastest.
#
#######################################################################
#
# NOTE: To output the timing to a file use the following syntax:
#
# 12_ways_to_parse.ksh file_to_process > output_file_name 2>&1
#
# The actaul timing data is sent to standard error, file
# descriptor (2), and the function name header is sent
# to standard output, file descriptor (1).
#
#######################################################################
#
# set -n # Uncomment to check command syntax without any execution
# set -x # Uncomment to debug this script
#

FILENAME="$1"
TIMEFILE="/tmp/loopfile.out"
>$TIMEFILE
THIS_SCRIPT=$(basename $0)

######################################
function usage
{
echo "/nUSAGE: $THIS_SCRIPT file_to_process/n"
echo "OR - To send the output to a file use: "
echo "/n$THIS_SCRIPT file_to_process > output_file_name 2>&1 /n"
exit 1
}
######################################
function while_read_LINE
{
cat $FILENAME | while read LINE
do
echo "$LINE"
:
done
}
######################################
function while_read_LINE_bottom
{
while read LINE
do
echo "$LINE"
:

done < $FILENAME
}
######################################
function while_line_LINE_bottom
{
while line LINE
do
echo $LINE
:
done < $FILENAME
}
######################################
function cat_while_LINE_line
{
cat $FILENAME | while LINE=`line`
do
echo "$LINE"
:
done
}
######################################
function while_line_LINE
{
cat $FILENAME | while line LINE
do
echo "$LINE"
:
done
}
######################################
function while_LINE_line_bottom
{
while LINE=`line`
do
echo "$LINE"
:

done < $FILENAME
}
######################################
function while_LINE_line_cmdsub2
{
cat $FILENAME | while LINE=$(line)
do
echo "$LINE"
:
done
}
######################################
function while_LINE_line_bottom_cmdsub2
{
while LINE=$(line)
do
echo "$LINE"
:

done < $FILENAME
}
######################################
function while_read_LINE_FD
{
exec 3<&0
exec 0< $FILENAME
while read LINE
do
echo "$LINE"
:
done
exec 0<&3
}
######################################
function while_LINE_line_FD
{
exec 3<&0
exec 0< $FILENAME
while LINE=`line`
do
echo "$LINE"
:
done
exec 0<&3
}
######################################
function while_LINE_line_cmdsub2_FD
{
exec 3<&0
exec 0< $FILENAME
while LINE=$(line)
do
print "$LINE"
:
done
exec 0<&3
}
######################################
function while_line_LINE_FD
{
exec 3<&0
exec 0< $FILENAME

while line LINE
do
echo "$LINE"
:
done

exec 0<&3
}
######################################
########### START OF MAIN ############
######################################

# Test the Input

# Looking for exactly one parameter
(( $# == 1 )) || usage

# Does the file exist as a regular file?
[[ -f $1 ]] || usage

echo "/nStarting File Processing of each Method/n"

echo "Method 1:"
echo "/nfunction while_read_LINE/n" >> $TIMEFILE
echo "function while_read_LINE"
time while_read_LINE >> $TIMEFILE
echo "/nMethod 2:"
echo "/nfunction while_read_LINE_bottom/n" >> $TIMEFILE
echo "function while_read_LINE_bottom"
time while_read_LINE_bottom >> $TIMEFILE
echo "/nMethod 3:"
echo "/nfunction while_line_LINE_bottom/n" >> $TIMEFILE
echo "function while_line_LINE_bottom"
time while_line_LINE_bottom >> $TIMEFILE
echo "/nMethod 4:"
echo "/nfunction cat_while_LINE_line/n" >> $TIMEFILE
echo "function cat_while_LINE_line"
time cat_while_LINE_line >> $TIMEFILE
echo "/nMethod 5:"
echo "/nfunction while_line_LINE/n" >> $TIMEFILE
echo "function while_line_LINE"
time while_line_LINE >> $TIMEFILE
echo "/nMethod 6:"
echo "/nfunction while_LINE_line_bottom/n" >> $TIMEFILE
echo "function while_LINE_line_bottom"
time while_LINE_line_bottom >> $TIMEFILE
echo "/nMethod 7:"
echo "/nfunction while_LINE_line_cmdsub2/n" >> $TIMEFILE
echo "function while_LINE_line_cmdsub2"
time while_LINE_line_cmdsub2 >> $TIMEFILE
echo "/nMethod 8:"
echo "/nfunction while_LINE_line_bottom_cmdsub2/n" >> $TIMEFILE
echo "function while_LINE_line_bottom_cmdsub2"
time while_LINE_line_bottom_cmdsub2 >> $TIMEFILE
echo "/nMethod 9:"
echo "/nfunction while_read_LINE_FD/n" >> $TIMEFILE
echo "function while_read_LINE_FD"
time while_read_LINE_FD >> $TIMEFILE
echo "/nMethod 10:"
echo "/nfunction while_LINE_line_FD/n" >> $TIMEFILE
echo "function while_LINE_line_FD"
time while_LINE_line_FD >> $TIMEFILE
echo "/nMethod 11:"
echo "/nfunction while_LINE_line_cmdsub2_FD/n" >> $TIMEFILE
echo "function while_LINE_line_cmdsub2_FD"
time while_LINE_line_cmdsub2_FD >> $TIMEFILE
echo "/nMethod 12:"
echo "/nfunction while_line_LINE_FD/n" >> $TIMEFILE
echo "function while_line_LINE_FD"
time while_line_LINE_FD >> $TIMEFILE  
原创粉丝点击