#!/bin/sh
echo -e "original file name"
read file
echo -e "target file name"
read file
cat "$file" | grep "xxx" -n > index.txt
sed "s/\([0-9]\)":fix_string.*/\1/g" index.txt > linerecord.txt # fix_string为待字符串的特殊字段,这个提取含有匹配字段的行号。
sed "s/.*\(regular exp\)/\1/g" index.txt > filename.txt #提取格式固定中,不固定的后缀部分,如果非固定部分为穿插模式,需要多指定字段。
sed "s/.*\(regular exp\)/\1/g" index.txt | sort -n > filename.txt #对提取内容排序
dos2unix.....#转换生成文件,详细略
index_linenum=$(cat index.txt | wc -l)
orilinenum=$(cat "$file" | wc -l)
for(i=1;i<=index_linenum;i++)
{
sed_para="${i}p"
cmdline="sed -n "$sed_para" linerecord.txt"
cmdfilename="sed -n "$sed_para" filename.txt"
if [ "$i" -eq "$index_linenum" ]
then Cur_NextLine=$orilinenum
else
let j=i+1
sed_para="${j}p"
cmdnextline="sed -n "$sed_para" linerecord.txt"
Cur_NextLine=$($cmdnextline)
let Cur_NextLine=Cur_NextLine-1
fi
Cur_line=$($cmdline)
Cur_name=$($cmdfilename)
sed -n "$(Cur_line), $(Cur_NextLine)p" "$file" > $(Cur_name).txt
}