file - 如何从 bash 脚本中的文件中逐行读取?

标签 file bash scripting line

我在网上搜索过这个问题,到目前为止我找到了两种方法:

   while read line; do
      commands
   done < "$filename"

    for $line in $(cat $filename); do
       commands
    done

如果行中有空格,这些都不起作用,例如,如果我们有这样的行

  textextext text

它不会打印textextext 文本

但是

  textextext
  text

它将这些事情算作不同的行,我怎样才能避免这种情况发生?

最佳答案

像这样?

while IFS= read line ; do
   something "$line"
done <"$file"

这是一个简短的测试:

while IFS= read line ; do echo "$line"; done <<<"$(echo -e "a b\nc d")"
a b
c d

关于file - 如何从 bash 脚本中的文件中逐行读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7957361/

相关文章:

asp.net - 如何更新 asp.net 网站(只是更改了文件)

Java - 无法找到肯定存在的文件路径

linux - 如何在 bash 命令参数中指定一组字符串

linux - 读取指定的文件行并根据该文件的字创建新目录

c++ - 有没有办法请求在 C++ 中读取文本文件的某个位置?

c++ - 在 C++ 中使用 ifstream 打开和关闭后,.txt 文件不再由 snmptrapd 守护进程写入

后台 Linux 进程 - 作业中的 "Stopped"?

linux - 命令 : tail file. txt '

c# - 在 C# 中实例化实现 .NET 接口(interface)的 IronPython 类型

shell - 使用 bash 连接到开放端口