Shell Script Utility To Read a File Line By Line
来源:互联网 发布:网络安全法考试题库 编辑:程序博客网 时间:2024/05/15 11:38
#!/bin/bash
# Shell script utility to read a file line line.
# Once line is read it can be process in processLine() function
# You can call script as follows, to read myfile.txt:
# ./readline myfile.txt
# Following example will read line from standard input device aka keyboard:
# ./readline
# -----------------------------------------------
# Copyright (c) 2005 nixCraft <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.
# -------------------------------------------------------------------------
# User define Function (UDF)
processLine(){
line="$@" # get all args
# just echo them, but you may need to customize it according to your need
# for example, F1 will store first field of $line, see readline2 script
# for more examples
# F1=$(echo $line | awk '{ print $1 }')
echo $line
}
### Main script stars here ###
# Store file name
FILE=""
# Make sure we get file name as command line argument
# Else read it from standard input device
if [ "$1" == "" ]; then
FILE="/dev/stdin"
else
FILE="$1"
# make sure file exist and readable
if [ ! -f $FILE ]; then
echo "$FILE : does not exists"
exit 1
elif [ ! -r $FILE ]; then
echo "$FILE: can not read"
exit 2
fi
fi
# read $FILE using the file descriptors
# Set loop separator to end of line
BAKIFS=$IFS
IFS=$(echo -en "/n/b")
exec 3<&0
exec 0<"$FILE"
while read -r line
do
# use $line variable to process line in processLine() function
processLine $line
done
exec 0<&3
# restore $IFS which was used to determine what the field separators are
IFS=$BAKIFS
exit 0
The following example script takes a regular expression as its first argument and one or more strings to match against. It then cycles through the strings and outputs the results of the match process: Assuming the script is saved in "bashre.sh", the following sample shows its output: #!/bin.bashif [[ $# -lt 2 ]]; then echo "Usage: $0 PATTERN STRINGS..." exit 1firegex=$1shiftecho "regex: $regex"echowhile [[ $1 ]]do if [[ $1 =~ $regex ]]; then echo "$1 matches" i=1 n=${#BASH_REMATCH[*]} while [[ $i -lt $n ]] do echo " capture[$i]: ${BASH_REMATCH[$i]}" let i++ done else echo "$1 does not match" fi shiftdone
# sh bashre.sh 'aa(b{2,3}[xyz])cc' aabbxcc aabbcc regex: aa(b{2,3}[xyz])cc aabbxcc matches capture[1]: bbx aabbcc does not match
- Shell Script Utility To Read a File Line By Line
- Read file line by line (Bash shell script, sed usage)
- Expect script to read file line by line
- How to read a file line by line?
- How to read file line by line?
- Java 8 Stream – Read a file line by line
- read line from file with bash shell script
- Golang Read File line by line
- [shell]while read line do done < file
- Read the Last line in a file
- Shell script - while read line后面的行读不出来
- awk check text file line-by-line
- Shell split character line by line
- While it is not the end of a file, output file line by line
- How to remove a line from a file in java?
- A. Line to Cashier
- A. Line to Cashier
- skip # and blank line read file
- 几点想法,权作网志
- HTML特殊字符显示
- 开源软件推介(二)
- sb的类型重定义错误 redefinition of ‘class
- 程序员特有的9个坏习惯
- Shell Script Utility To Read a File Line By Line
- Java串口通信总结
- JAVA读写串口Bean范例
- 虚拟函数实现的机制
- JavaScript学习(7)利用表单获取数据
- 今天装上了VS2010
- 我的激情 怎么了!
- 一位软件工程师的6年总结_I'm a C fans!(转载)
- 定制 SWT/RCP 界面:如何编写一个漂亮的 SWT/RCP 界面