Chinaunix首页 | 论坛 | 博客
  • 博客访问: 582558
  • 博文数量: 57
  • 博客积分: 877
  • 博客等级: 准尉
  • 技术积分: 1275
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-24 16:16
文章分类

全部博文(57)

文章存档

2014年(2)

2013年(15)

2012年(20)

2011年(20)

我的朋友

分类: 系统运维

2013-03-01 13:01:21

背景:用要shell 提取 文件中内容,文件名是用序列号如下生成,文件差不多有将近400多w个  如下:


原始脚本
#! /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 " " $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 " " $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 ...

达到预期的目标了!

阅读(5891) | 评论(3) | 转发(0) |
给主人留下些什么吧!~~

jjg62563082013-03-01 19:02:08

fbl_haiou:还有个方法,我试过了,也能用:

用-f输出浮点式,然后用awk取第一段(以“.”分割)

[root@appdear tmp]# seq -f "%f" 1111111111 1111111113| awk -F\. '{print $1}'
1111111111
1111111112
1111111113

恩,你这样也是可行的

回复 | 举报

fbl_haiou2013-03-01 18:10:10

还有个方法,我试过了,也能用:

用-f输出浮点式,然后用awk取第一段(以“.”分割)

[root@appdear tmp]# seq -f "%f" 1111111111 1111111113| awk -F\. '{print $1}'
1111111111
1111111112
1111111113