Board logo

标题: [问题求助] Shell脚本 for 循环中变量无法在sed命令中使用? [打印本页]

作者: xdahoufang    时间: 2014-12-28 16:48     标题: Shell脚本 for 循环中变量无法在sed命令中使用?

如下代码
  1. for  i  in  `cat file_list.txt`
  2. do
  3. echo $i
  4. sed -n -e '/Copyright/=' $i
  5. done
复制代码
查找file_list.txt记录文件中的copyright字样在第几行。执行结果如下:
  1. inc/os/mtos_int1.h
  2. : No such file or directory_int1.h
  3. inc/os/mtos_task.h
  4. : No such file or directory_task.h
  5. inc/kware/record/record_seq.h
  6. : No such file or directoryecord/record_seq.h
复制代码
求教:为什么变量$i可以正常输出,但无法在sed中使用。
作者: Batcher    时间: 2014-12-28 19:22

如果你的 file_list.txt 是从 Windows 复制到 Linux 里面的,需要先把它转换格式,执行命令:
  1. dos2unix file_list.txt
复制代码

作者: xdahoufang    时间: 2014-12-29 09:45

回复 2# Batcher


    这个可以保证的。 echo $i 可以正常输出
作者: xdahoufang    时间: 2014-12-29 10:01

sorry,又检查了一遍,确实为dos格式的。  
文件新建是在linux上建的,可能是在samba里面查看的时候不小心转成了dos了
作者: Batcher    时间: 2014-12-29 10:56

回复 4# xdahoufang


    用 cat -A file_list.txt 可以很方便的确认格式
作者: Linuxer    时间: 2014-12-29 12:00

学习了,,,刚安装了个dos2unix,,确实很好用。




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2