shell下十二種讀檔案的方法(轉)
shell下十二種讀檔案的方法(轉)[@more@]以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 " USAGE: $THIS_SCRIPT file_to_process "
echo "OR - To send the output to a file use: "
echo " $THIS_SCRIPT file_to_process > output_file_name 2>&1 "
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 3exec 0< $FILENAME
while read LINE
do
echo "$LINE"
:
done
exec 0}
######################################
function while_LINE_line_FD
{
exec 3exec 0< $FILENAME
while LINE=`line`
do
echo "$LINE"
:
done
exec 0}
######################################
function while_LINE_line_cmdsub2_FD
{
exec 3exec 0< $FILENAME
while LINE=$(line)
do
print "$LINE"
:
done
exec 0}
######################################
function while_line_LINE_FD
{
exec 3exec 0< $FILENAME
while line LINE
do
echo "$LINE"
:
done
exec 0}
######################################
########### 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 " Starting File Processing of each Method "
echo "Method 1:"
echo " function while_read_LINE " >> $TIMEFILE
echo "function while_read_LINE"
time while_read_LINE >> $TIMEFILE
echo " Method 2:"
echo " function while_read_LINE_bottom " >> $TIMEFILE
echo "function while_read_LINE_bottom"
time while_read_LINE_bottom >> $TIMEFILE
echo " Method 3:"
echo " function while_line_LINE_bottom " >> $TIMEFILE
echo "function while_line_LINE_bottom"
time while_line_LINE_bottom >> $TIMEFILE
echo " Method 4:"
echo " function cat_while_LINE_line " >> $TIMEFILE
echo "function cat_while_LINE_line"
time cat_while_LINE_line >> $TIMEFILE
echo " Method 5:"
echo " function while_line_LINE " >> $TIMEFILE
echo "function while_line_LINE"
time while_line_LINE >> $TIMEFILE
echo " Method 6:"
echo " function while_LINE_line_bottom " >> $TIMEFILE
echo "function while_LINE_line_bottom"
time while_LINE_line_bottom >> $TIMEFILE
echo " Method 7:"
echo " function while_LINE_line_cmdsub2 " >> $TIMEFILE
echo "function while_LINE_line_cmdsub2"
time while_LINE_line_cmdsub2 >> $TIMEFILE
echo " Method 8:"
echo " function while_LINE_line_bottom_cmdsub2 " >> $TIMEFILE
echo "function while_LINE_line_bottom_cmdsub2"
time while_LINE_line_bottom_cmdsub2 >> $TIMEFILE
echo " Method 9:"
echo " function while_read_LINE_FD " >> $TIMEFILE
echo "function while_read_LINE_FD"
time while_read_LINE_FD >> $TIMEFILE
echo " Method 10:"
echo " function while_LINE_line_FD " >> $TIMEFILE
echo "function while_LINE_line_FD"
time while_LINE_line_FD >> $TIMEFILE
echo " Method 11:"
echo " function while_LINE_line_cmdsub2_FD " >> $TIMEFILE
echo "function while_LINE_line_cmdsub2_FD"
time while_LINE_line_cmdsub2_FD >> $TIMEFILE
echo " Method 12:"
echo " function while_line_LINE_FD " >> $TIMEFILE
echo "function while_line_LINE_FD"
time while_line_LINE_FD >> $TIMEFILE
#!/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 " USAGE: $THIS_SCRIPT file_to_process "
echo "OR - To send the output to a file use: "
echo " $THIS_SCRIPT file_to_process > output_file_name 2>&1 "
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 3exec 0< $FILENAME
while read LINE
do
echo "$LINE"
:
done
exec 0}
######################################
function while_LINE_line_FD
{
exec 3exec 0< $FILENAME
while LINE=`line`
do
echo "$LINE"
:
done
exec 0}
######################################
function while_LINE_line_cmdsub2_FD
{
exec 3exec 0< $FILENAME
while LINE=$(line)
do
print "$LINE"
:
done
exec 0}
######################################
function while_line_LINE_FD
{
exec 3exec 0< $FILENAME
while line LINE
do
echo "$LINE"
:
done
exec 0}
######################################
########### 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 " Starting File Processing of each Method "
echo "Method 1:"
echo " function while_read_LINE " >> $TIMEFILE
echo "function while_read_LINE"
time while_read_LINE >> $TIMEFILE
echo " Method 2:"
echo " function while_read_LINE_bottom " >> $TIMEFILE
echo "function while_read_LINE_bottom"
time while_read_LINE_bottom >> $TIMEFILE
echo " Method 3:"
echo " function while_line_LINE_bottom " >> $TIMEFILE
echo "function while_line_LINE_bottom"
time while_line_LINE_bottom >> $TIMEFILE
echo " Method 4:"
echo " function cat_while_LINE_line " >> $TIMEFILE
echo "function cat_while_LINE_line"
time cat_while_LINE_line >> $TIMEFILE
echo " Method 5:"
echo " function while_line_LINE " >> $TIMEFILE
echo "function while_line_LINE"
time while_line_LINE >> $TIMEFILE
echo " Method 6:"
echo " function while_LINE_line_bottom " >> $TIMEFILE
echo "function while_LINE_line_bottom"
time while_LINE_line_bottom >> $TIMEFILE
echo " Method 7:"
echo " function while_LINE_line_cmdsub2 " >> $TIMEFILE
echo "function while_LINE_line_cmdsub2"
time while_LINE_line_cmdsub2 >> $TIMEFILE
echo " Method 8:"
echo " function while_LINE_line_bottom_cmdsub2 " >> $TIMEFILE
echo "function while_LINE_line_bottom_cmdsub2"
time while_LINE_line_bottom_cmdsub2 >> $TIMEFILE
echo " Method 9:"
echo " function while_read_LINE_FD " >> $TIMEFILE
echo "function while_read_LINE_FD"
time while_read_LINE_FD >> $TIMEFILE
echo " Method 10:"
echo " function while_LINE_line_FD " >> $TIMEFILE
echo "function while_LINE_line_FD"
time while_LINE_line_FD >> $TIMEFILE
echo " Method 11:"
echo " function while_LINE_line_cmdsub2_FD " >> $TIMEFILE
echo "function while_LINE_line_cmdsub2_FD"
time while_LINE_line_cmdsub2_FD >> $TIMEFILE
echo " Method 12:"
echo " function while_line_LINE_FD " >> $TIMEFILE
echo "function while_line_LINE_FD"
time while_line_LINE_FD >> $TIMEFILE
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10617542/viewspace-949427/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 15種下載檔案的方法
- 下載檔案的15種方法
- ServletOutputStream在nginx轉發下輸出檔案下載的一種方法ServletNginx
- python讀取大檔案的幾種方法Python
- python讀取檔案指定行的三種方法Python
- Python逐行讀取檔案常用的三種方法!Python
- 下載GitHub上檔案的兩種方法Github
- 在.net中讀寫config檔案的各種方法
- shell讀取配置檔案-sed命令
- 大神教你python 讀取檔案並把矩陣轉成numpy的兩種方法Python矩陣
- 再談檔案讀寫:判斷檔案的幾種方法及其優劣對比
- Linux系統下建立檔案最常見的8種方法!Linux
- Python中4種方法實現 xls 檔案轉 xlsxPython
- ArcEngine 開啟AutoCAD檔案的幾種方法與讀取CAD資料的方法
- Win10系統下將excel檔案轉換為dbf檔案的方法Win10Excel
- Python 高階技巧:深入解析讀取 Excel 檔案的多種方法PythonExcel
- php讀取檔案的幾種方式PHP
- Android讀取配置檔案的方法Android
- CentOS下OpenCV無法讀取影片檔案的解決方法CentOSOpenCV
- (Python基礎教程之十二)Python讀寫CSV檔案Python
- python讀取文字檔案內容的方法主要分為哪三種?Python
- PDF檔案轉HTML方法HTML
- 如何在Shell指令碼中逐行讀取檔案指令碼
- python儲存檔案的幾種方法Python
- 流量中提取檔案的若干種方法
- 監聽檔案修改的四種方法
- 計算檔案Checksum的幾種方法
- 大檔案傳輸的9種方法
- 前端接受後端檔案流並下載的幾種方法前端後端
- windows下ftp定時執行批次下載檔案,windows下ftp定時執行批次下載檔案的一種方法WindowsFTP
- CentOS下OpenCV無法讀取視訊檔案的解決方法CentOSOpenCV
- Python中讀寫Parquet檔案的方法Python
- python讀取yaml配置檔案的方法PythonYAML
- JavaScript~檔案下載的幾種方式JavaScript
- Shell 檔案包含
- Json檔案轉換為Excel檔案!涉及讀檔案,時間戳轉化,寫文件JSONExcel時間戳
- 6.1檔案下載、讀取
- shell讀取構建檔案資訊生成json字串JSON字串
- Linux下把sra檔案轉成fastq檔案LinuxAST