【发布时间】:2025-12-14 07:50:01
【问题描述】:
我有一个 bash 脚本,我需要在其中逐行读取文件。我知道通常的while read line 是如何工作的,但我的程序不能很好地适应while 循环。我有两个文件,需要在某些条件下逐行比较它们(不是diff:条件是一个文件中的行是否以另一个文件中的行开头)。目前我有一个Java版本的程序,它有三个嵌套循环,两个文件的循环交织在一起,我需要打破嵌套循环(我知道怎么做)。所以我想要一个优雅的解决方案来在 bash 中完成以下基本任务(以下代码是我的 Java 程序):
BufferedReader reader = new BufferedReader(new FileReader(inputFile)); // initialize a file pointer
reader.ready(); // whether the pointer is at the end of the file (used in while and if conditions)
lineStr = reader.readLine(); // read next line
我在网上找到的所有解决方案都使用规范的 while read line 结构,但我的程序无法适应它。所以我想用更多的控制来操作文件。
【问题讨论】: