一次shell中seq的处理

背景:用要shell 提取 文件中内容,文件名是用序列号如下生成,文件差不多有将近400多w个  如下:
  www.ahlinux.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 ...

达到预期的目标了!

一次shell中seq的处理的更多相关文章

  1. shell中的set、seq、eval、exec、&&和||

    一.set 查看set 帮助: bash -c "help set" 选项: -e:任何命令执行失败(非0 status)直接退出 -x: 打印执行过程的命令行.参数 +e:命令执 ...

  2. shell中的循环

    shell中的循环 for循环 类似于C语言的步长控制 例如: ;i<=;i++)); ); done 将1到10,依次乘以4,然后打印出来. 这里顺便提一下,shell里面表达式的计算,可以有 ...

  3. shell中for循环总结

    关于shell中的for循环用法很多,一直想总结一下,今天网上看到上一篇关于for循环用法的总结,感觉很全面,所以就转过来研究研究,嘿嘿... 1. for((i=1;i<=10;i++));d ...

  4. shell中的函数、数组、报警系统脚本

    1.shell中的函数 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这 个小单元的名字即可.格式: function f_name() {commond} ...

  5. Shell中的数组及其相关操作

    http://blog.csdn.net/jerry_1126/article/details/52027539 Shell中数据类型不多,比如说字符串,数字类型,数组.数组是其中比较重要的一种,其重 ...

  6. shell中的(),{}几种语法用法

    转自:https://www.cnblogs.com/HKUI/p/6423918.html 查看脚本语法是否有错误:bash -n modify_suffix.sh跟踪执行sh -x modify_ ...

  7. shell 中的()【】{}(())

    本文转自:https://blog.csdn.net/taiyang1987912/article/details/39551385 shell中各种括号的作用().(()).[].[[]].{} 一 ...

  8. centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课

    centos  shell脚本编程2 if 判断  case判断   shell脚本中的循环  for   while   shell中的函数  break  continue  test 命令   ...

  9. centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课

    centos   shell脚本编程1 正则  shell脚本结构  read命令  date命令的用法  shell中的逻辑判断  if 判断文件.目录属性  shell数组简单用法 $( ) 和$ ...

随机推荐

  1. description方法

    1.NSLog回顾 众所周知,我们可以用NSLog函数来输出字符串和一些基本数据类 1 int age = 11; 2 NSLog(@"age is %i", age); * 第2 ...

  2. vc++ windows 快速启动栏创建快捷方式

    创建快速启动栏 在windows软件开发中,软件安装过程中总是需要在快速启动栏创建快捷方式,下面介绍一种快速启动栏创建快捷方式的方法,具体代码如下:(该方法不支持win10,目前还没有找到win10的 ...

  3. vue router 传参 获取不到query,params

    千万要注意,获取query/params 是this.$route.query 不是this.$router.query!!!

  4. linux用netstat查看服务及监听端口

    [root@localhost ~]# netstat -nlp netstat命令各个参数说明如下: -t : 指明显示TCP端口 -u : 指明显示UDP端口 -l : 仅显示监听套接字(所谓套接 ...

  5. PostgreSQL备份

    备份与恢复 postgresql自带了两个备份工具: pg_dump:可备份一个指定的database pg_dumpall:可一次性备份所有database数据以及系统全局数据  使用pg_dump ...

  6. aop log切面

    @Aspect:描述一个切面类,定义切面类的时候需要打上这个注解 @Component:spring-boot配置类 package com.*.*.tools; import org.aspectj ...

  7. TPS和QPS区别

    TPS和QPS区别 http://blog.csdn.net/kobejayandy/article/details/9374747

  8. nginx config的多个config配置

    在我们的一台服务器上,一个nginx服务器下面可能跑着许多许多的项目; 那么就需要配置多个对应的配置 端口号 已经文件入库目录等等 那么项目多了以后,把这些项目都写到一个文件里 到后期难以查看与管理 ...

  9. shutil模块,ZipFile 和 TarFile 两个模块

    高级的文件.文件夹.压缩包处理模块 shutil.copyfileobj(fsrc, fdst[, length])将文件内容拷贝到另一个文件中,可以部分内容 shutil.copyfile(src, ...

  10. 文件操作方法大全以及文件打开的其他一些模式sys.stdout.write()就是标准输出到你当前的屏幕 sys.stdout.flush()把内存立即显示到您当前的屏幕

    read()会让你读取的光标变成最后.tell()把你现在文件的句柄的指针打印出来.文件的开头指针位置是0f.read(5)只读取5个字符串个数如果你想把光标移回去,移动到首位f.seek(0)f.d ...