一次shell中seq的处理


一次shell中seq的处理
 
背景:用要shell 提取 文件中内容,文件名是用序列号如下生成,文件差不多有将近400多w个  如下:
  www.2cto.com  
原始脚本
#! /bin/sh
#str1=""

#filecount=`ls -l /root/gjj | wc -l | awk '{print $1}'`
#echo $filecount

for n in `seq  $1 $2`
do
filename="/windows_gjj/"${n}".txt"
echo $filename

dos2unix $filename
sed -i '1,76d' $filename
sed -i '41,$d' $filename
sed -i 's/<.*">//g' $filename
sed -i 's/<.*>//g' $filename
sed -i 's/^[[:space:]]*//g' $filename
sed -i '/^$/d' $filename
#sed -i 's/;//g' $filename

#cat $filename >> /tmp/all_gjj.log

flag=`grep "&nbsp" $filename | wc -l | awk '{print $1}'`

if [ $flag -ne 10 ]; then
 cat $filename >> /tmp/all_gjj.log
 echo "********************************************************************************************" >> /tmp/all_gjj.log
 LCOUNT=`wc -l $filename | awk '{print $1}'`
 str1=""
 for i in `seq 1 10`
 do
 sed -i '1d' $filename
 str=`head -n 1 $filename`
 echo $str >> /tmp.log
 str1=${str1}${str}"|"
 echo $str1
 sed -i '1d' $filename
 done

 echo $str1 >> /root/gjj.txt
fi

done

脚本中$1,$2 代表起始的序列号。
一开始的时候,用这个脚本来提取文件内容是正常的,但当文件名上7位数的时候,就出现问题了:

如下:
[root@ALL ~]# sh tiqu.sh 2908637 2908640
/windows_gjj/2.90864e+06.txt
dos2unix: converting file /windows_gjj/2.90864e+06.txt to UNIX format ...
dos2unix: problems converting file /windows_gjj/2.90864e+06.txt
sed:无法读取 /windows_gjj/2.90864e+06.txt:没有那个文件或目录
sed:无法读取 /windows_gjj/2.90864e+06.txt:没有那个文件或目录
sed:无法读取 /windows_gjj/2.90864e+06.txt:没有那个文件或目录
sed:无法读取 /windows_gjj/2.90864e+06.txt:没有那个文件或目录
sed:无法读取 /windows_gjj/2.90864e+06.txt:没有那个文件或目录
sed:无法读取 /windows_gjj/2.90864e+06.txt:没有那个文件或目录
grep: /windows_gjj/2.90864e+06.txt: 没有那个文件或目录
cat: /windows_gjj/2.90864e+06.txt: 没有那个文件或目录
wc: /windows_gjj/2.90864e+06.txt: 没有那个文件或目录
sed:无法读取 /windows_gjj/2.90864e+06.txt:没有那个文件或目录
head: 无法打开 “/windows_gjj/2.90864e+06.txt” 读取数据: 没有那个文件或目录
|
sed:无法读取 /windows_gjj/2.90864e+06.txt:没有那个文件或目录
sed:无法读取 /windows_gjj/2.90864e+06.txt:没有那个文件或目录
head: 无法打开 “/windows_gjj/2.90864e+06.txt” 读取数据: 没有那个文件或目录
||
sed:无法读取 /windows_gjj/2.90864e+06.txt:没有那个文件或目录
sed:无法读取 /windows_gjj/2.90864e+06.txt:没有那个文件或目录
head: 无法打开 “/windows_gjj/2.90864e+06.txt” 读取数据: 没有那个文件或目录

分析:出现这种问题主要是 shell 把7位数字用指数的形式在表示了,从而造成了找不到对应的文件

解决方法:为了能使 7位数仍然以数字的形式出现,试了 seq 中的-f ,-w 之类的选项都没达到预期的效果,最后采用了折中的方法,最高位用字符代替,后6为用seq 生成,用参数-w 保持位数的宽度一致,修该的脚本如下:
#! /bin/sh
#str1=""

#filecount=`ls -l /root/gjj | wc -l | awk '{print $1}'`
#echo $filecount

for n in `seq -w $1 $2`
do
n="2"${n}
filename="/windows_gjj/"${n}".txt"
echo $filename

dos2unix $filename
sed -i '1,76d' $filename
sed -i '41,$d' $filename
sed -i 's/<.*">//g' $filename
sed -i 's/<.*>//g' $filename
sed -i 's/^[[:space:]]*//g' $filename
sed -i '/^$/d' $filename
#sed -i 's/;//g' $filename

#cat $filename >> /tmp/all_gjj.log

flag=`grep "&nbsp" $filename | wc -l | awk '{print $1}'`

if [ $flag -ne 10 ]; then
 cat $filename >> /tmp/all_gjj.log
 echo "********************************************************************************************" >> /tmp/all_gjj.log
 LCOUNT=`wc -l $filename | awk '{print $1}'`
 str1=""
 for i in `seq 1 10`
 do
 sed -i '1d' $filename
 str=`head -n 1 $filename`
 echo $str >> /tmp.log
 str1=${str1}${str}"|"
 echo $str1
 sed -i '1d' $filename
 done

 echo $str1 >> /root/gjj.txt
fi

done

[root@ALL ~]# sh tiqu.sh 908636 908640
/windows_gjj/2908636.txt
dos2unix: converting file /windows_gjj/2908636.txt to UNIX format …
|
/windows_gjj/2908637.txt
dos2unix: converting file /windows_gjj/2908637.txt to UNIX format ...

达到预期的目标了!
 
 

相关内容

    暂无相关文章